Skip to content

Commit 7e051be

Browse files
committed
Add test proving equivalence of round() and roundf()
1 parent 5d76b8e commit 7e051be

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ function(llama_add_test source)
55
add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}> ${ARGN})
66
endfunction()
77

8+
llama_add_test(test-double-float.c)
89
llama_add_test(test-quantize.c)
910
llama_add_test(test-tokenizer-0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab.bin)

tests/test-double-float.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// These tests may take a long time!
2+
// They are to prove that conversion from double to float of various functions in ggml.c doesn't affect the result.
3+
// This is done by checking all finite (non-NaN, non-infinite) floats.
4+
5+
#undef NDEBUG
6+
#include <assert.h>
7+
#include <math.h>
8+
#include <stdint.h>
9+
10+
#pragma GCC diagnostic push
11+
#pragma GCC diagnostic ignored "-Wdouble-promotion"
12+
13+
// ggml.c::quantize_row_q4_0_reference
14+
inline static uint8_t round_orig(float v0) { return ((int8_t) (round(v0))) + 8; }
15+
16+
#pragma GCC diagnostic pop
17+
18+
// ggml.c::quantize_row_q4_0_reference
19+
inline static uint8_t round_float(float v0) { return (int8_t)roundf(v0) + 8; }
20+
21+
int main(void) {
22+
uint32_t x = UINT32_MAX;
23+
do {
24+
float f = *(float *)&x;
25+
assert(!isfinite(f) || (round_orig(f) == round_float(f)));
26+
} while (x--);
27+
}

0 commit comments

Comments
 (0)