| 11 | #include "fl/stl/string.h" |
| 12 | |
| 13 | FL_TEST_FILE(FL_FILEPATH) { |
| 14 | |
| 15 | using namespace fl; |
| 16 | |
| 17 | // Convenience: parse a literal and reinterpret the result as a float for |
| 18 | // comparison against host strtof. The bit-cast itself does not anchor any |
| 19 | // libgcc soft-FP helper; the host `float` comparison does (cheap on host). |
| 20 | static float parse_as_float(const char* s) { |
| 21 | u32 bits = ieee754_parse_decimal(s, fl::strlen(s)); |
| 22 | return fl::bit_cast<float>(bits); |
| 23 | } |
| 24 | |
| 25 | FL_TEST_CASE("ieee754_parse_decimal -- well-known constants") { |
| 26 | FL_SUBCASE("zero") { |
| 27 | FL_CHECK(ieee754_parse_decimal("0", 1) == 0x00000000u); |
| 28 | FL_CHECK(ieee754_parse_decimal("-0", 2) == 0x80000000u); |
| 29 | FL_CHECK(ieee754_parse_decimal("0.0", 3) == 0x00000000u); |
| 30 | } |
| 31 | |
| 32 | FL_SUBCASE("one") { |
| 33 | FL_CHECK(ieee754_parse_decimal("1", 1) == 0x3F800000u); |
| 34 | FL_CHECK(ieee754_parse_decimal("1.0", 3) == 0x3F800000u); |
| 35 | FL_CHECK(ieee754_parse_decimal("-1", 2) == 0xBF800000u); |
| 36 | } |
| 37 | |
| 38 | FL_SUBCASE("ten") { |
| 39 | FL_CHECK(ieee754_parse_decimal("10", 2) == 0x41200000u); |
| 40 | FL_CHECK(ieee754_parse_decimal("10.0", 4) == 0x41200000u); |
| 41 | } |
| 42 | |
| 43 | FL_SUBCASE("half") { |
| 44 | FL_CHECK(ieee754_parse_decimal("0.5", 3) == 0x3F000000u); |
| 45 | FL_CHECK(ieee754_parse_decimal("-0.5", 4) == 0xBF000000u); |
| 46 | } |
| 47 | |
| 48 | FL_SUBCASE("two") { |
| 49 | FL_CHECK(ieee754_parse_decimal("2", 1) == 0x40000000u); |
| 50 | FL_CHECK(ieee754_parse_decimal("2.0", 3) == 0x40000000u); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | FL_TEST_CASE("ieee754_parse_decimal -- within +/-1 ULP of strtof") { |
| 55 | auto ulp_diff = [](u32 a, u32 b) -> u32 { |
| 56 | return a > b ? (a - b) : (b - a); |
| 57 | }; |
| 58 | |
| 59 | auto roundtrip_check = [&](const char* s, float expected) { |
| 60 | u32 got_bits = ieee754_parse_decimal(s, fl::strlen(s)); |
| 61 | u32 expected_bits = fl::bit_cast<u32>(expected); |
| 62 | FL_CHECK(ulp_diff(got_bits, expected_bits) <= 1u); |
| 63 | }; |
| 64 | |
| 65 | roundtrip_check("3.14", 3.14f); |
| 66 | roundtrip_check("3.14159", 3.14159f); |
| 67 | roundtrip_check("0.001", 0.001f); |
| 68 | roundtrip_check("0.0001", 0.0001f); |
| 69 | roundtrip_check("100.0", 100.0f); |
| 70 | roundtrip_check("1000.0", 1000.0f); |
nothing calls this directly
no test coverage detected