summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2022-02-05 02:23:27 +0100
committerAndreas Kling <kling@serenityos.org>2022-08-27 12:28:05 +0200
commit8483064b59f9fbcc73d93b2fda72c6103b66f461 (patch)
tree7e834937aa639419674cf3a8b8c76b1acae82f3f /Tests
parentf1cd6453ae4cbca6f0f00e601cf1210d07412827 (diff)
downloadserenity-8483064b59f9fbcc73d93b2fda72c6103b66f461.zip
AK: Add `FloatingPoint.h`
This is a set of functions that allow you to convert between arbitrary IEEE 754 floating point types, as long as they can be represented within 64 bits. Conversion methods between floats and doubles are provided, as well as a generic `float_to_float()`. Example usage: #include <AK/FloatingPoint.h> double val = 1.234; auto weird_f16 = convert_from_native_double<FloatingPointBits<0, 6, 10>>(val); Signed and unsigned floats are supported, and both NaN and +/-Inf are handled correctly. Values that do not fit in the target floating point type are clamped.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/CMakeLists.txt1
-rw-r--r--Tests/AK/TestFloatingPoint.cpp85
2 files changed, 86 insertions, 0 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt
index 38b066fcbe..ec48588c6f 100644
--- a/Tests/AK/CMakeLists.txt
+++ b/Tests/AK/CMakeLists.txt
@@ -26,6 +26,7 @@ set(AK_TEST_SOURCES
TestEnumBits.cpp
TestFind.cpp
TestFixedArray.cpp
+ TestFloatingPoint.cpp
TestFormat.cpp
TestGenericLexer.cpp
TestHashFunctions.cpp
diff --git a/Tests/AK/TestFloatingPoint.cpp b/Tests/AK/TestFloatingPoint.cpp
new file mode 100644
index 0000000000..da6a2fe4cc
--- /dev/null
+++ b/Tests/AK/TestFloatingPoint.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/FloatingPoint.h>
+#include <LibTest/TestCase.h>
+#include <math.h>
+
+TEST_CASE(f16_1_5_10_to_native_float)
+{
+ auto within_approximate = [](u16 lhs, float rhs) -> bool {
+ auto f32_lhs = convert_to_native_float(FloatingPointBits<1, 5, 10>(lhs));
+ return fabsf(f32_lhs - rhs) <= 0.00001f;
+ };
+
+ EXPECT(within_approximate(0x0000, 0.f));
+ EXPECT(within_approximate(0x03FF, 0.000061f));
+ EXPECT(within_approximate(0x3CEF, 1.23339f));
+ EXPECT(within_approximate(0xBC00, -1.f));
+ EXPECT(within_approximate(0xA266, -0.0125f));
+
+ float result;
+ result = convert_to_native_float(FloatingPointBits<1, 5, 10>(0xFC01u));
+ EXPECT(isnan(result));
+
+ result = convert_to_native_float(FloatingPointBits<1, 5, 10>(0x7C00u));
+ EXPECT(isinf(result));
+}
+
+TEST_CASE(float_to_double_roundtrips)
+{
+ auto roundtrip = [](float floatvalue1) {
+ auto doublevalue = convert_from_native_float<DoubleFloatingPointBits>(floatvalue1).as_double();
+ auto floatbits = convert_from_native_double<SingleFloatingPointBits>(doublevalue);
+ auto floatvalue2 = convert_to_native_float(floatbits);
+
+ EXPECT_APPROXIMATE(floatvalue1, floatvalue2);
+ };
+
+ roundtrip(-1.0f);
+ roundtrip(-0.1f);
+ roundtrip(0.0f);
+ roundtrip(0.000001f);
+ roundtrip(0.1f);
+ roundtrip(1.0f);
+ roundtrip(3.141592f);
+ roundtrip(16777216.0f);
+ roundtrip(33554432.0f);
+
+ roundtrip(1 / 0.0f);
+ roundtrip(1 / -0.0f);
+ roundtrip(0 / 0.0f);
+}
+
+TEST_CASE(normalize_denormalize)
+{
+ // Go from denormalized float to normalized double
+ auto denormalized_float = 6.709679e-39f;
+ auto denormalized_float_bits = SingleFloatingPointBits(denormalized_float);
+ auto normalized_double = convert_to_native_double(denormalized_float_bits);
+ EXPECT_APPROXIMATE(denormalized_float, normalized_double);
+
+ // Go back from normalized double to denormalized float
+ auto normalized_double_bits = DoubleFloatingPointBits(normalized_double);
+ auto reconstructed_denormalized_float = convert_to_native_float(normalized_double_bits);
+ EXPECT_APPROXIMATE(denormalized_float, reconstructed_denormalized_float);
+}
+
+TEST_CASE(large_exponent)
+{
+ // Make sure we support at least 62 bits of exponent
+ auto large_exponent_float = convert_from_native_double<FloatingPointBits<1, 62, 1>>(1.0);
+ auto converted_double = convert_to_native_double(large_exponent_float);
+ EXPECT_APPROXIMATE(converted_double, 1.0);
+}
+
+TEST_CASE(large_mantissa)
+{
+ // Make sure we support at least 62 bits of mantissa
+ auto large_exponent_float = convert_from_native_double<FloatingPointBits<1, 1, 62>>(1.0);
+ auto converted_double = convert_to_native_double(large_exponent_float);
+ EXPECT_APPROXIMATE(converted_double, 1.0);
+}