Skip to content

Commit e830a32

Browse files
authored
Add fxdiv (#5005)
* Add fxdiv * add tests * Use noarch --------- Co-authored-by: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com>
1 parent 5d197d7 commit e830a32

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
cmake . \
5+
-DCMAKE_BUILD_TYPE=Release \
6+
-DCMAKE_INSTALL_PREFIX=$PREFIX \
7+
-DFXDIV_BUILD_TESTS=OFF \
8+
-DFXDIV_BUILD_BENCHMARKS=OFF \
9+
-Bcmake-out-wasm
10+
11+
cmake --build cmake-out-wasm --target install
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export CFLAGS="${CFLAGS} ${EM_FORGE_SIDE_MODULE_CFLAGS}"
5+
6+
echo "Building fxdiv tests with CMake..."
7+
8+
mkdir -p build
9+
cd build
10+
11+
emcmake cmake ../tests/. \
12+
-DCMAKE_BUILD_TYPE=Release \
13+
-DCMAKE_FIND_ROOT_PATH=$PREFIX \
14+
-DCMAKE_PREFIX_PATH=$PREFIX
15+
16+
emmake make
17+
18+
echo "Running fxdiv tests..."
19+
node test_fxdiv.js
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
context:
2+
name: fxdiv
3+
version: "63058eff77e11aa15bf531df5dd34395ec3017c8"
4+
5+
package:
6+
name: ${{ name }}
7+
version: ${{ version }}
8+
9+
source:
10+
- url: https://github.com/Maratyszcza/FXdiv/archive/${{ version }}.tar.gz
11+
sha256: ec74d882a0a47cfd9c0f95bc4fae9901a4ade802a96a3b76e02671bb7340a4c5
12+
13+
build:
14+
number: 0
15+
script: build.sh
16+
noarch: generic
17+
18+
requirements:
19+
build:
20+
- cmake
21+
22+
tests:
23+
- package_contents:
24+
include:
25+
- fxdiv.h
26+
- script:
27+
- build_tests.sh
28+
requirements:
29+
build:
30+
- ${{ compiler('c') }}
31+
- cmake
32+
- make
33+
files:
34+
recipe:
35+
- build_tests.sh
36+
- tests/
37+
38+
about:
39+
homepage: https://github.com/Maratyszcza/FXdiv
40+
license: MIT
41+
license_file: LICENSE
42+
summary: Header-only library for integer division via fixed-point multiplication
43+
description: |
44+
FXdiv is a header-only C/C++ library that implements fast integer division
45+
using fixed-point multiplication. Division is replaced by a multiplication
46+
and a shift, which is significantly faster than hardware integer division.
47+
repository: https://github.com/Maratyszcza/FXdiv
48+
49+
extra:
50+
recipe-maintainers:
51+
- Alex-PLACET
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(fxdiv_test C)
3+
4+
set(CMAKE_C_STANDARD 99)
5+
set(CMAKE_C_STANDARD_REQUIRED ON)
6+
7+
find_path(FXDIV_INCLUDE_DIR fxdiv.h REQUIRED)
8+
9+
add_executable(test_fxdiv test_fxdiv.c)
10+
target_include_directories(test_fxdiv PRIVATE ${FXDIV_INCLUDE_DIR})
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <fxdiv.h>
2+
#include <stdint.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
static int failures = 0;
7+
8+
#define CHECK(cond, msg) \
9+
do { \
10+
if (!(cond)) { \
11+
fprintf(stderr, "FAIL: %s\n", msg); \
12+
failures++; \
13+
} else { \
14+
printf("PASS: %s\n", msg); \
15+
} \
16+
} while (0)
17+
18+
static void test_uint32(void) {
19+
/* Basic quotient and remainder */
20+
const struct fxdiv_divisor_uint32_t div7 = fxdiv_init_uint32_t(7);
21+
CHECK(fxdiv_quotient_uint32_t(100, div7) == 100u / 7u, "quotient 100/7");
22+
CHECK(fxdiv_remainder_uint32_t(100, div7) == 100u % 7u, "remainder 100%7");
23+
24+
/* divide returns both quotient and remainder */
25+
const struct fxdiv_result_uint32_t r = fxdiv_divide_uint32_t(1000, div7);
26+
CHECK(r.quotient == 1000u / 7u, "divide quotient 1000/7");
27+
CHECK(r.remainder == 1000u % 7u, "divide remainder 1000%7");
28+
29+
/* round_down */
30+
CHECK(fxdiv_round_down_uint32_t(13, div7) == 7u, "round_down 13 by 7");
31+
CHECK(fxdiv_round_down_uint32_t(14, div7) == 14u, "round_down 14 by 7");
32+
33+
/* divisor of 1 */
34+
const struct fxdiv_divisor_uint32_t div1 = fxdiv_init_uint32_t(1);
35+
CHECK(fxdiv_quotient_uint32_t(42, div1) == 42u, "quotient 42/1");
36+
CHECK(fxdiv_remainder_uint32_t(42, div1) == 0u, "remainder 42%1");
37+
38+
/* power-of-2 divisor */
39+
const struct fxdiv_divisor_uint32_t div8 = fxdiv_init_uint32_t(8);
40+
CHECK(fxdiv_quotient_uint32_t(256, div8) == 32u, "quotient 256/8");
41+
CHECK(fxdiv_remainder_uint32_t(255, div8) == 7u, "remainder 255%8");
42+
}
43+
44+
static void test_uint64(void) {
45+
const struct fxdiv_divisor_uint64_t div13 = fxdiv_init_uint64_t(13);
46+
CHECK(fxdiv_quotient_uint64_t(UINT64_C(1000000000), div13) ==
47+
UINT64_C(1000000000) / UINT64_C(13), "quotient 1e9/13");
48+
CHECK(fxdiv_remainder_uint64_t(UINT64_C(1000000000), div13) ==
49+
UINT64_C(1000000000) % UINT64_C(13), "remainder 1e9%13");
50+
51+
const struct fxdiv_result_uint64_t r =
52+
fxdiv_divide_uint64_t(UINT64_C(999999999999), div13);
53+
CHECK(r.quotient == UINT64_C(999999999999) / UINT64_C(13), "divide quotient large/13");
54+
CHECK(r.remainder == UINT64_C(999999999999) % UINT64_C(13), "divide remainder large%13");
55+
56+
/* divisor of 1 */
57+
const struct fxdiv_divisor_uint64_t div1 = fxdiv_init_uint64_t(1);
58+
CHECK(fxdiv_quotient_uint64_t(UINT64_C(999), div1) == UINT64_C(999), "quotient 999/1");
59+
}
60+
61+
static void test_size_t(void) {
62+
const struct fxdiv_divisor_size_t div5 = fxdiv_init_size_t(5);
63+
CHECK(fxdiv_quotient_size_t(100, div5) == (size_t)(100 / 5), "quotient 100/5 (size_t)");
64+
CHECK(fxdiv_remainder_size_t(103, div5) == (size_t)(103 % 5), "remainder 103%5 (size_t)");
65+
66+
const struct fxdiv_result_size_t r = fxdiv_divide_size_t(99, div5);
67+
CHECK(r.quotient == (size_t)(99 / 5), "divide quotient 99/5 (size_t)");
68+
CHECK(r.remainder == (size_t)(99 % 5), "divide remainder 99%5 (size_t)");
69+
}
70+
71+
int main(void) {
72+
test_uint32();
73+
test_uint64();
74+
test_size_t();
75+
76+
if (failures > 0) {
77+
fprintf(stderr, "%d test(s) FAILED\n", failures);
78+
return 1;
79+
}
80+
printf("All tests passed.\n");
81+
return 0;
82+
}

0 commit comments

Comments
 (0)