From 829ac72f87658053f0d004e8803a3d3eca80dbea Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Sun, 15 Nov 2020 22:38:21 +0000 Subject: [PATCH 01/25] re #33: 32bit version. gcc compiles successfully, fails tests. --- .github/workflows/ubuntu18.yml | 12 +++++++----- .github/workflows/ubuntu20.yml | 12 +++++++----- .github/workflows/vs16-ci.yml | 10 ++++++++-- .github/workflows/vs16-clang-ci.yml | 8 +++++++- .gitignore | 2 ++ include/fast_float/float_common.h | 24 ++++++++++++++++++++---- tests/long_random64.cpp | 2 +- tests/random64.cpp | 2 +- 8 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 .gitignore diff --git a/.github/workflows/ubuntu18.yml b/.github/workflows/ubuntu18.yml index 23850cc..bf1f2d0 100644 --- a/.github/workflows/ubuntu18.yml +++ b/.github/workflows/ubuntu18.yml @@ -9,9 +9,10 @@ jobs: fail-fast: false matrix: include: - - {cxx: -DCMAKE_CXX_COMPILER=g++-5} - - {cxx: -DCMAKE_CXX_COMPILER=g++-6} - - {cxx: } # default compiler 7 + - {cxx: -DCMAKE_CXX_COMPILER=g++-5, arch: } + - {cxx: -DCMAKE_CXX_COMPILER=g++-6, arch: } + - {cxx: , arch: } # default=gcc7 + - {cxx: , arch: -DCMAKE_CXX_FLAGS="-m32"} # default=gcc7 steps: - uses: actions/checkout@v2 - name: Setup cmake @@ -20,12 +21,13 @@ jobs: cmake-version: '3.9.x' - name: Install older compilers run: | + sudo -E dpkg --add-architecture i386 sudo -E apt-get update - sudo -E apt-get install -y --force-yes g++-5 g++-6 + sudo -E apt-get install -y --force-yes g++-5 g++-6 g++-5-multilib g++-6-multilib g++-multilib linux-libc-dev:i386 libc6:i386 libc6-dev:i386 libc6-dbg:i386 - name: Prepare build dir run: mkdir build - name: Configure - run: cd build && cmake ${{matrix.cxx}} -DFASTFLOAT_TEST=ON .. + run: cd build && cmake ${{matrix.cxx}} ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. - name: Build run: cmake --build build - name: Run basic tests diff --git a/.github/workflows/ubuntu20.yml b/.github/workflows/ubuntu20.yml index 68aa1ee..451c9b4 100644 --- a/.github/workflows/ubuntu20.yml +++ b/.github/workflows/ubuntu20.yml @@ -9,8 +9,9 @@ jobs: fail-fast: false matrix: include: - - {cxx: -DCMAKE_CXX_COMPILER=g++-8} - - {cxx: } # default compiler 9 + - {cxx: -DCMAKE_CXX_COMPILER=g++-8, arch: } + - {cxx: , arch: } # default=gcc9 + - {cxx: , arch: -DCMAKE_CXX_FLAGS="-m32"} # default=gcc9 steps: - uses: actions/checkout@v2 - name: Setup cmake @@ -19,12 +20,13 @@ jobs: cmake-version: '3.9.x' - name: install older compilers run: | + sudo -E dpkg --add-architecture i386 sudo -E apt-get update - sudo -E apt-get install -y --force-yes g++-8 g++-7 - - name: Prepare build dir + sudo -E apt-get install -y g++-8 g++-8-multilib g++-multilib linux-libc-dev:i386 libc6:i386 libc6-dev:i386 libc6-dbg:i386 + - name: Prepare build dir run: mkdir build - name: Configure - run: cd build && cmake ${{matrix.cxx}} -DFASTFLOAT_TEST=ON .. + run: cd build && cmake ${{matrix.cxx}} ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. - name: Build run: cmake --build build - name: Run basic tests diff --git a/.github/workflows/vs16-ci.yml b/.github/workflows/vs16-ci.yml index 5a7071a..4f237fd 100644 --- a/.github/workflows/vs16-ci.yml +++ b/.github/workflows/vs16-ci.yml @@ -6,6 +6,12 @@ jobs: ci: name: windows-vs16 runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - vs: VS16Win64 + - vs: VS16Win32 steps: - uses: actions/checkout@v2 - name: 'Run CMake with VS16' @@ -16,9 +22,9 @@ jobs: buildDirectory: "${{ github.workspace }}/../../_temp/windows" cmakeBuildType: Release buildWithCMake: true - cmakeGenerator: VS16Win64 + cmakeGenerator: ${{matrix.vs}} cmakeAppendedArgs: -DFASTFLOAT_TEST=ON - buildWithCMakeArgs: --config Release + buildWithCMakeArgs: --config Release - name: 'Run CTest' run: ctest -C Release --output-on-failure -R basictest diff --git a/.github/workflows/vs16-clang-ci.yml b/.github/workflows/vs16-clang-ci.yml index 08afa25..2bf8e2b 100644 --- a/.github/workflows/vs16-clang-ci.yml +++ b/.github/workflows/vs16-clang-ci.yml @@ -6,6 +6,12 @@ jobs: ci: name: windows-vs16 runs-on: windows-latest + strategy: + fail-fast: false + matrix: + include: + - vs: VS16Win64 + - vs: VS16Win32 steps: - uses: actions/checkout@v2 - name: 'Run CMake with VS16' @@ -16,7 +22,7 @@ jobs: buildDirectory: "${{ github.workspace }}/../../_temp/windows" cmakeBuildType: Release buildWithCMake: true - cmakeGenerator: VS16Win64 + cmakeGenerator: ${{matrix.vs}} cmakeAppendedArgs: -T ClangCL -DFASTFLOAT_TEST=ON buildWithCMakeArgs: --config Release diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5a3ead --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/* +Testing/* \ No newline at end of file diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 60e5f6e..a98a54b 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -139,15 +139,31 @@ fastfloat_really_inline value128 full_multiplication(uint64_t value1, return answer; } -#else +#else // gcc // compute value1 * value2 -fastfloat_really_inline value128 full_multiplication(uint64_t value1, - uint64_t value2) { +fastfloat_really_inline value128 full_multiplication(uint64_t a, + uint64_t b) { value128 answer; - __uint128_t r = ((__uint128_t)value1) * value2; +#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__arm__) + static constexpr const uint64_t lo32 = 0xffffffffu; + // https://stackoverflow.com/questions/28868367/getting-the-high-part-of-64-bit-integer-multiplication + uint64_t a_lo = a & lo32; + uint64_t a_hi = a >> 32; + uint64_t b_lo = b & lo32; + uint64_t b_hi = b >> 32; + uint64_t ab_hi = a_hi * b_hi; + uint64_t ab_mid = a_hi * b_lo; + uint64_t ba_mid = b_hi * a_lo; + uint64_t ab_lo = a_lo * b_lo; + uint64_t carry_bit = ((ab_mid & lo32) + (ba_mid & lo32) + (ab_lo >> 32)) >> 32; + answer.high = ab_hi + (ab_mid >> 32) + (ba_mid >> 32) + carry_bit; + answer.low = ab_lo + (ab_mid & lo32) + (ba_mid & lo32); +#else // if defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64) + __uint128_t r = ((__uint128_t)a) * b; answer.low = uint64_t(r); answer.high = uint64_t(r >> 64); +#endif return answer; } diff --git a/tests/long_random64.cpp b/tests/long_random64.cpp index b8a6b05..47c4d0c 100644 --- a/tests/long_random64.cpp +++ b/tests/long_random64.cpp @@ -90,7 +90,7 @@ void random_values(size_t N) { int main() { errors = 0; - size_t N = size_t(1) << 32; + size_t N = size_t(1) << (sizeof(size_t) * 4); // shift: 32 for 64bit, 16 for 32bit random_values(N); if (errors == 0) { std::cout << std::endl; diff --git a/tests/random64.cpp b/tests/random64.cpp index 3f37975..d169fbb 100644 --- a/tests/random64.cpp +++ b/tests/random64.cpp @@ -92,7 +92,7 @@ void random_values(size_t N) { int main() { errors = 0; - size_t N = size_t(1) << 32; + size_t N = size_t(1) << (sizeof(size_t) * 4); // shift: 32 for 64bit, 16 for 32bit random_values(N); if (errors == 0) { std::cout << std::endl; From c4693cc86f5e8b4de5c30263954dc55b866fc9cb Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 11:58:03 +0000 Subject: [PATCH 02/25] re #33: win32 is working --- include/fast_float/float_common.h | 18 +++++++++++++++--- tests/basictest.cpp | 24 +++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index a98a54b..93782e9 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -90,6 +90,7 @@ struct value128 { /* result might be undefined when input_num is zero */ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #ifdef FASTFLOAT_VISUAL_STUDIO +#if defined(_M_X64) || defined(_M_ARM64) unsigned long leading_zero = 0; // Search the mask data from most significant bit (MSB) // to least significant bit (LSB) for a set bit (1). @@ -97,6 +98,17 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { return (int)(63 - leading_zero); else return 64; +#else + if(!input_num) return 64; + int n = 0; + if(input_num & uint64_t(0xffffffff00000000)) input_num >>= 32, n |= 32; + if(input_num & uint64_t( 0xffff0000)) input_num >>= 16, n |= 16; + if(input_num & uint64_t( 0xff00)) input_num >>= 8, n |= 8; + if(input_num & uint64_t( 0xf0)) input_num >>= 4, n |= 4; + if(input_num & uint64_t( 0xc)) input_num >>= 2, n |= 2; + if(input_num & uint64_t( 0x2)) input_num >>= 1, n |= 1; + return 63 - n; +#endif #else return __builtin_clzll(input_num); #endif @@ -109,9 +121,9 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #if !defined(_M_X64) && !defined(_M_ARM64) // _umul128 for x86, arm // this is a slow emulation routine for 32-bit Windows // -fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { - return x * (uint64_t)y; -} +//fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { +// return x * (uint64_t)y; +//} fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi) { uint64_t ad = __emulu((uint32_t)(ab >> 32), (uint32_t)cd); diff --git a/tests/basictest.cpp b/tests/basictest.cpp index 8acb9ca..8d9d750 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -1,9 +1,14 @@ #include "fast_float/fast_float.h" #include -inline void Assert(bool Assertion) { - if (!Assertion) - throw std::runtime_error("bug"); +#define Q(x) #x +#define QUOTE(x) Q(x) +#define Assert(assertion) \ + if (!(assertion)) {\ + AssertionFailed(__FILE__ ":" QUOTE(__LINE__) ": assertion failed: " #assertion);\ + } +inline void AssertionFailed(const char *msg) { + throw std::runtime_error(msg); } template std::string to_string(T d) { @@ -327,7 +332,20 @@ bool test_fixed_only() { return true; } + void test_leading_zeroes() + { + constexpr const uint64_t bit = 1; + Assert(fast_float::leading_zeroes(0) == 64); + Assert(fast_float::leading_zeroes(bit << 0) == 63); + Assert(fast_float::leading_zeroes(bit << 1) == 62); + Assert(fast_float::leading_zeroes(bit << 2) == 61); + Assert(fast_float::leading_zeroes(bit << 61) == 2); + Assert(fast_float::leading_zeroes(bit << 62) == 1); + Assert(fast_float::leading_zeroes(bit << 63) == 0); + } + int main() { + test_leading_zeroes(); Assert(test_fixed_only()); Assert(test_scientific_only()); Assert(issue8()); From bb03cb615ca807e3f7c2be783aed57880675c0f4 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 11:58:38 +0000 Subject: [PATCH 03/25] make sure CI runs Win32 compilations --- .github/workflows/vs16-ci.yml | 33 +++++++++++++---------------- .github/workflows/vs16-clang-ci.yml | 33 +++++++++++++---------------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/.github/workflows/vs16-ci.yml b/.github/workflows/vs16-ci.yml index 4f237fd..e486a74 100644 --- a/.github/workflows/vs16-ci.yml +++ b/.github/workflows/vs16-ci.yml @@ -10,22 +10,19 @@ jobs: fail-fast: false matrix: include: - - vs: VS16Win64 - - vs: VS16Win32 + - {gen: Visual Studio 16 2019, arch: Win32} + - {gen: Visual Studio 16 2019, arch: x64} steps: - - uses: actions/checkout@v2 - - name: 'Run CMake with VS16' - uses: lukka/run-cmake@v2 - with: - cmakeListsOrSettingsJson: CMakeListsTxtAdvanced - cmakeListsTxtPath: '${{ github.workspace }}/CMakeLists.txt' - buildDirectory: "${{ github.workspace }}/../../_temp/windows" - cmakeBuildType: Release - buildWithCMake: true - cmakeGenerator: ${{matrix.vs}} - cmakeAppendedArgs: -DFASTFLOAT_TEST=ON - buildWithCMakeArgs: --config Release - - - name: 'Run CTest' - run: ctest -C Release --output-on-failure -R basictest - working-directory: "${{ github.workspace }}/../../_temp/windows" + - uses: actions/checkout@v2 + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.0 + with: + cmake-version: '3.9.x' + - name: Prepare build dir + run: mkdir build + - name: Configure + run: cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. + - name: Build + run: cmake --build build --configuration Release + - name: Run basic tests + run: cd build && ctest -C Release --output-on-failure -R basictest diff --git a/.github/workflows/vs16-clang-ci.yml b/.github/workflows/vs16-clang-ci.yml index 2bf8e2b..d3858bd 100644 --- a/.github/workflows/vs16-clang-ci.yml +++ b/.github/workflows/vs16-clang-ci.yml @@ -10,22 +10,19 @@ jobs: fail-fast: false matrix: include: - - vs: VS16Win64 - - vs: VS16Win32 + - {gen: Visual Studio 16 2019, arch: Win32} + - {gen: Visual Studio 16 2019, arch: x64} steps: - - uses: actions/checkout@v2 - - name: 'Run CMake with VS16' - uses: lukka/run-cmake@v2 - with: - cmakeListsOrSettingsJson: CMakeListsTxtAdvanced - cmakeListsTxtPath: '${{ github.workspace }}/CMakeLists.txt' - buildDirectory: "${{ github.workspace }}/../../_temp/windows" - cmakeBuildType: Release - buildWithCMake: true - cmakeGenerator: ${{matrix.vs}} - cmakeAppendedArgs: -T ClangCL -DFASTFLOAT_TEST=ON - buildWithCMakeArgs: --config Release - - - name: 'Run CTest' - run: ctest -C Release -R basictest --output-on-failure - working-directory: "${{ github.workspace }}/../../_temp/windows" + - uses: actions/checkout@v2 + - name: Setup cmake + uses: jwlawson/actions-setup-cmake@v1.0 + with: + cmake-version: '3.9.x' + - name: Prepare build dir + run: mkdir build + - name: Configure + run: cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -T ClangCL -DFASTFLOAT_TEST=ON .. + - name: Build + run: cmake --build build --configuration Release + - name: Run basic tests + run: cd build && ctest -C Release --output-on-failure -R basictest From 7da4b05645425649ef772b488914157795ce8573 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 11:59:18 +0000 Subject: [PATCH 04/25] add unit test for full multiplication --- tests/basictest.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/basictest.cpp b/tests/basictest.cpp index 8d9d750..2a2ab92 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -344,8 +344,33 @@ bool test_fixed_only() { Assert(fast_float::leading_zeroes(bit << 63) == 0); } + void test_full_multiplication(uint64_t lhs, uint64_t rhs, uint64_t expected_lo, uint64_t expected_hi) + { + fast_float::value128 v; + v = fast_float::full_multiplication(lhs, rhs); + Assert(v.low == expected_lo); + Assert(v.high == expected_hi); + v = fast_float::full_multiplication(rhs, lhs); + Assert(v.low == expected_lo); + Assert(v.high == expected_hi); + } + + void test_full_multiplication() + { + constexpr const uint64_t bit = 1; + // lhs rhs lo hi + test_full_multiplication(bit << 0 , bit << 0, 1u , 0u); + test_full_multiplication(bit << 0 , bit << 63, bit << 63, 0u); + test_full_multiplication(bit << 1 , bit << 63, 0u , 1u); + test_full_multiplication(bit << 63, bit << 0, bit << 63, 0u); + test_full_multiplication(bit << 63, bit << 1, 0u , 1u); + test_full_multiplication(bit << 63, bit << 2, 0u , 2u); + test_full_multiplication(bit << 63, bit << 63, 0u , bit << 62); + } + int main() { test_leading_zeroes(); + test_full_multiplication(); Assert(test_fixed_only()); Assert(test_scientific_only()); Assert(issue8()); From 83a29d4eab7511f121213d128cb0865ded00e055 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 13:32:36 +0000 Subject: [PATCH 05/25] windows CI: cmake 3.9 is too old --- .github/workflows/vs16-ci.yml | 13 +++++-------- .github/workflows/vs16-clang-ci.yml | 13 +++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/.github/workflows/vs16-ci.yml b/.github/workflows/vs16-ci.yml index e486a74..3be2ba7 100644 --- a/.github/workflows/vs16-ci.yml +++ b/.github/workflows/vs16-ci.yml @@ -13,15 +13,12 @@ jobs: - {gen: Visual Studio 16 2019, arch: Win32} - {gen: Visual Studio 16 2019, arch: x64} steps: - - uses: actions/checkout@v2 - - name: Setup cmake - uses: jwlawson/actions-setup-cmake@v1.0 - with: - cmake-version: '3.9.x' - - name: Prepare build dir - run: mkdir build + - name: checkout + uses: actions/checkout@v2 - name: Configure - run: cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. + run: | + mkdir build + cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. - name: Build run: cmake --build build --configuration Release - name: Run basic tests diff --git a/.github/workflows/vs16-clang-ci.yml b/.github/workflows/vs16-clang-ci.yml index d3858bd..0c4faa7 100644 --- a/.github/workflows/vs16-clang-ci.yml +++ b/.github/workflows/vs16-clang-ci.yml @@ -13,15 +13,12 @@ jobs: - {gen: Visual Studio 16 2019, arch: Win32} - {gen: Visual Studio 16 2019, arch: x64} steps: - - uses: actions/checkout@v2 - - name: Setup cmake - uses: jwlawson/actions-setup-cmake@v1.0 - with: - cmake-version: '3.9.x' - - name: Prepare build dir - run: mkdir build + - name: checkout + uses: actions/checkout@v2 - name: Configure - run: cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -T ClangCL -DFASTFLOAT_TEST=ON .. + run: | + mkdir build + cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -T ClangCL -DFASTFLOAT_TEST=ON .. - name: Build run: cmake --build build --configuration Release - name: Run basic tests From 449c6286454f0e8f864f2b67ef3dbadab3db6bef Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 13:33:11 +0000 Subject: [PATCH 06/25] __emulu() is needed for mingw32 --- include/fast_float/float_common.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 93782e9..2c0c18e 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -120,10 +120,13 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #if !defined(_M_X64) && !defined(_M_ARM64) // _umul128 for x86, arm // this is a slow emulation routine for 32-bit Windows -// -//fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { -// return x * (uint64_t)y; -//} + +#ifdef __MINGW32__ +fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { + return x * (uint64_t)y; +} +#endif + fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi) { uint64_t ad = __emulu((uint32_t)(ab >> 32), (uint32_t)cd); From 11a1c143a38819be3b094ff5322994a18972c264 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 13:38:19 +0000 Subject: [PATCH 07/25] windows CI: fix call to cmake --build --- .github/workflows/vs16-ci.yml | 2 +- .github/workflows/vs16-clang-ci.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/vs16-ci.yml b/.github/workflows/vs16-ci.yml index 3be2ba7..8548aba 100644 --- a/.github/workflows/vs16-ci.yml +++ b/.github/workflows/vs16-ci.yml @@ -20,6 +20,6 @@ jobs: mkdir build cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. - name: Build - run: cmake --build build --configuration Release + run: cmake --build build --config Release --parallel - name: Run basic tests run: cd build && ctest -C Release --output-on-failure -R basictest diff --git a/.github/workflows/vs16-clang-ci.yml b/.github/workflows/vs16-clang-ci.yml index 0c4faa7..559630d 100644 --- a/.github/workflows/vs16-clang-ci.yml +++ b/.github/workflows/vs16-clang-ci.yml @@ -20,6 +20,6 @@ jobs: mkdir build cd build && cmake -G "${{matrix.gen}}" -A ${{matrix.arch}} -T ClangCL -DFASTFLOAT_TEST=ON .. - name: Build - run: cmake --build build --configuration Release + run: cmake --build build --config Release --parallel - name: Run basic tests run: cd build && ctest -C Release --output-on-failure -R basictest From 27345da2e2a83556fe72246de158ae5b1af38671 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 16 Nov 2020 16:00:03 +0000 Subject: [PATCH 08/25] ubuntu20 CI: fix typo --- .github/workflows/ubuntu20.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu20.yml b/.github/workflows/ubuntu20.yml index 451c9b4..6bf0dcc 100644 --- a/.github/workflows/ubuntu20.yml +++ b/.github/workflows/ubuntu20.yml @@ -23,7 +23,7 @@ jobs: sudo -E dpkg --add-architecture i386 sudo -E apt-get update sudo -E apt-get install -y g++-8 g++-8-multilib g++-multilib linux-libc-dev:i386 libc6:i386 libc6-dev:i386 libc6-dbg:i386 - - name: Prepare build dir + - name: Prepare build dir run: mkdir build - name: Configure run: cd build && cmake ${{matrix.cxx}} ${{matrix.arch}} -DFASTFLOAT_TEST=ON .. From 1e795800656553ed5cb7b35484bcbf136bd7b208 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Tue, 17 Nov 2020 01:49:58 +0000 Subject: [PATCH 09/25] basictest: move to doctest benefits: * shows all failures instead of throwing * every failure shows detailed information * more concise (~400 lines -> ~300 lines) --- tests/CMakeLists.txt | 10 +- tests/basictest.cpp | 695 ++++++++++++++++++------------------------- 2 files changed, 302 insertions(+), 403 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0b32b52..cce9438 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,9 @@ +include(FetchContent) + +FetchContent_Declare(doctest + GIT_REPOSITORY https://github.com/onqtam/doctest.git + GIT_TAG master) +FetchContent_MakeAvailable(doctest) function(fast_float_add_cpp_test TEST_NAME) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) @@ -6,7 +12,7 @@ function(fast_float_add_cpp_test TEST_NAME) target_compile_options(${TEST_NAME} PUBLIC -Werror -Wall -Wextra -Weffc++) target_compile_options(${TEST_NAME} PUBLIC -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wsign-conversion) endif() - target_link_libraries(${TEST_NAME} PUBLIC fast_float) + target_link_libraries(${TEST_NAME} PUBLIC fast_float doctest) endfunction(fast_float_add_cpp_test) fast_float_add_cpp_test(short_random_string) fast_float_add_cpp_test(exhaustive32_midpoint) @@ -19,4 +25,4 @@ fast_float_add_cpp_test(long_exhaustive32_64) fast_float_add_cpp_test(long_random64) fast_float_add_cpp_test(random64) fast_float_add_cpp_test(basictest) -fast_float_add_cpp_test(example_test) \ No newline at end of file +fast_float_add_cpp_test(example_test) diff --git a/tests/basictest.cpp b/tests/basictest.cpp index 2a2ab92..a699198 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -1,125 +1,51 @@ #include "fast_float/fast_float.h" #include +#define DOCTEST_CONFIG_SUPER_FAST_ASSERTS +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include -#define Q(x) #x -#define QUOTE(x) Q(x) -#define Assert(assertion) \ - if (!(assertion)) {\ - AssertionFailed(__FILE__ ":" QUOTE(__LINE__) ": assertion failed: " #assertion);\ - } -inline void AssertionFailed(const char *msg) { - throw std::runtime_error(msg); + +TEST_CASE("leading_zeroes") { + constexpr const uint64_t bit = 1; + CHECK(fast_float::leading_zeroes(0) == 64); + CHECK(fast_float::leading_zeroes(bit << 0) == 63); + CHECK(fast_float::leading_zeroes(bit << 1) == 62); + CHECK(fast_float::leading_zeroes(bit << 2) == 61); + CHECK(fast_float::leading_zeroes(bit << 61) == 2); + CHECK(fast_float::leading_zeroes(bit << 62) == 1); + CHECK(fast_float::leading_zeroes(bit << 63) == 0); } -template std::string to_string(T d) { - std::string s(64, '\0'); - auto written = std::snprintf(&s[0], s.size(), "%.*e", - std::numeric_limits::max_digits10 - 1, d); - s.resize(size_t(written)); - return s; +#define iHexAndDec(v) std::hex << "0x" << (v) << " (" << std::dec << (v) << ")" +#define fHexAndDec(v) std::hexfloat << "0x" << (v) << " (" << std::defaultfloat << (v) << ")" + +void test_full_multiplication(uint64_t lhs, uint64_t rhs, uint64_t expected_lo, uint64_t expected_hi) { + fast_float::value128 v; + v = fast_float::full_multiplication(lhs, rhs); + INFO("lhs=" << iHexAndDec(lhs) << " " << "rhs=" << iHexAndDec(rhs) + << "\n actualLo=" << iHexAndDec(v.low) << " " << "actualHi=" << iHexAndDec(v.high) + << "\n expectedLo=" << iHexAndDec(expected_lo) << " " << "expectedHi=" << iHexAndDec(expected_hi)); + CHECK_EQ(v.low, expected_lo); + CHECK_EQ(v.high, expected_hi); + v = fast_float::full_multiplication(rhs, lhs); + CHECK_EQ(v.low, expected_lo); + CHECK_EQ(v.high, expected_hi); } -template std::string to_long_string(T d) { - std::string s(4096, '\0'); - auto written = std::snprintf(&s[0], s.size(), "%.*e", - std::numeric_limits::max_digits10 * 10, d); - s.resize(size_t(written)); - return s; -} - -bool basic_test_32bit(std::string vals) { - std::cout << " parsing " << vals << std::endl; - float result_value; - auto result = fast_float::from_chars(vals.data(), vals.data() + vals.size(), - result_value); - if (result.ec != std::errc()) { - std::cerr << " I could not parse " << vals << std::endl; - return false; - } - - std::cout << std::hexfloat << result_value << std::endl; - std::cout << std::dec; - return true; -} - -bool basic_test_32bit(std::string vals, float val) { - std::cout << " parsing " << vals << std::endl; - float result_value; - auto result = fast_float::from_chars(vals.data(), vals.data() + vals.size(), - result_value); - if (result.ec != std::errc()) { - std::cerr << " I could not parse " << vals << std::endl; - return false; - } - if(copysign(1,result_value) != copysign(1,val)) { - std::cerr << vals << std::endl; - std::cerr << "I got " << std::hexfloat << result_value << " but I was expecting " << val - << std::endl; - return false; - } else if (std::isnan(val)) { - if (!std::isnan(result_value)) { - std::cerr << vals << std::endl; - std::cerr << "not nan" << result_value << std::endl; - return false; - } - } else if (result_value != val) { - std::cerr << vals << std::endl; - std::cerr << "I got " << std::hexfloat << result_value << " but I was expecting " << val - << std::endl; - std::cerr << std::dec; - uint32_t word; - memcpy(&word, &result_value, sizeof(word)); - std::cout << "got mantissa = " << (word & ((1<<23)-1)) << std::endl; - memcpy(&word, &val, sizeof(word)); - std::cout << "wanted mantissa = " << (word & ((1<<23)-1)) << std::endl; - std::cerr << "string: " << vals << std::endl; - return false; - } - std::cout << std::hexfloat << result_value << " == " << val << std::endl; - std::cout << std::dec; - return true; -} - -bool basic_test_32bit(float val) { - std::string long_vals = to_long_string(val); - std::string vals = to_string(val); - return basic_test_32bit(long_vals, val) && basic_test_32bit(vals, val); -} - -bool basic_test_64bit(std::string vals, double val) { - std::cout << " parsing " << vals << std::endl; - double result_value; - auto result = fast_float::from_chars(vals.data(), vals.data() + vals.size(), - result_value); - if (result.ec != std::errc()) { - std::cerr << " I could not parse " << vals << std::endl; - return false; - } - if(copysign(1,result_value) != copysign(1,val)) { - std::cerr << "I got " << std::hexfloat << result_value << " but I was expecting " << val - << std::endl; - return false; - } else if (std::isnan(val)) { - if (!std::isnan(result_value)) { - std::cerr << vals << std::endl; - std::cerr << "not nan" << result_value << std::endl; - return false; - } - } else if (result_value != val) { - std::cerr << vals << std::endl; - std::cerr << "I got " << std::hexfloat << result_value << " but I was expecting " << val - << std::endl; - std::cerr << std::dec; - std::cerr << "string: " << vals << std::endl; - return false; - } - std::cout << std::hexfloat << result_value << " == " << val << std::endl; - return true; +TEST_CASE("full_multiplication") { + constexpr const uint64_t bit = 1; + // lhs rhs lo hi + test_full_multiplication(bit << 0 , bit << 0, 1u , 0u); + test_full_multiplication(bit << 0 , bit << 63, bit << 63, 0u); + test_full_multiplication(bit << 1 , bit << 63, 0u , 1u); + test_full_multiplication(bit << 63, bit << 0, bit << 63, 0u); + test_full_multiplication(bit << 63, bit << 1, 0u , 1u); + test_full_multiplication(bit << 63, bit << 2, 0u , 2u); + test_full_multiplication(bit << 63, bit << 63, 0u , bit << 62); } -bool issue8() { - std::cout << __func__ << std::endl; +TEST_CASE("issue8") { const char* s = "3." "141592653589793238462643383279502884197169399375105820974944592307816406" @@ -137,98 +63,62 @@ bool issue8() { // Parse all but the last i chars. We should still get 3.141ish. double d = 0.0; auto answer = fast_float::from_chars(s, s + strlen(s) - i, d); - if(answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return false; } - if(d != 0x1.921fb54442d18p+1) { - printf("%.*s\n", int(strlen(s) - size_t(i)), s); - std::cout << std::hexfloat << d << std::endl; - std::cout << std::defaultfloat << d << std::endl; - return false; - } - + CHECK_MESSAGE(answer.ec == std::errc(), "i=" << i); + CHECK_MESSAGE(d == 0x1.921fb54442d18p+1, "i=" << i << "\n" + << std::string(s, strlen(s) - size_t(i)) << "\n" + << std::hexfloat << d << "\n" + << std::defaultfloat << "\n"); } - return true; } -bool check_behavior() { - std::cout << __func__ << std::endl; - const std::string input = "abc"; - double result; - auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); - if(answer.ec != std::errc()) { - std::cerr << "parsing failure as expected\n"; - // specification says ptr should point at first - if(answer.ptr != input.data()) { - std::cerr << "If there is no pattern match, we should have ptr equals first\n"; - return false; - } - return true; - } - std::cout << "parsed the number " << result << std::endl; - return false; -} - -bool issue19() { - std::cout << __func__ << std::endl; - const std::string input = "3.14e"; - double result; - auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); - if(answer.ec != std::errc()) { - std::cerr << "We want to parse up to 3.14\n"; - return false; - } - std::cout << "parsed the number " << result << std::endl; - if(answer.ptr == input.data() + 4) { - std::cout << "Parsed the number and stopped at the right character." << result << std::endl; - return true; - } - std::cout << "stopped after " << (answer.ptr - input.data()) << " characters" << std::endl; - return false; +TEST_CASE("check_behavior") { + const std::string input = "abc"; + double result; + auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); + CHECK_MESSAGE(answer.ec != std::errc(), "expected parse failure"); + CHECK_MESSAGE(answer.ptr == input.data(), "If there is no pattern match, we should have ptr equals first"); } -bool test_scientific_only() { - std::cout << __func__ << std::endl; +TEST_CASE("issue19") { + const std::string input = "3.14e"; + double result; + auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); + CHECK_MESSAGE(answer.ec == std::errc(), "We want to parse up to 3.14\n"); + CHECK_MESSAGE(answer.ptr == input.data() + 4, + "Parsed the number " << result + << " and stopped at the wrong character: after " << (answer.ptr - input.data()) << " characters"); +} + + +TEST_CASE("scientific_only") { // first, we try with something that should fail... std::string input = "3.14"; double result; auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::scientific); - if(answer.ec == std::errc()) { - std::cerr << "It is not scientific!\n"; - return false; - } + CHECK_MESSAGE(answer.ec != std::errc(), "It is not scientific! Parsed: " << result); + input = "3.14e10"; answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::scientific); - if(answer.ec != std::errc()) { - std::cerr << "It is scientific!\n"; - return false; - } - std::cout << "parsed the number " << result << std::endl; - if(answer.ptr == input.data() + input.size()) { - std::cout << "Parsed the number and stopped at the right character." << result << std::endl; - return true; - } - return false; + CHECK_MESSAGE(answer.ec == std::errc(), "It is scientific! Parsed: " << result); + CHECK_MESSAGE(answer.ptr == input.data() + input.size(), + "Parsed the number " << result + << " and stopped at the wrong character: after " << (answer.ptr - input.data()) << " characters"); } -bool test_fixed_only() { - std::cout << __func__ << std::endl; + +TEST_CASE("test_fixed_only") { const std::string input = "3.14e10"; double result; auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result, fast_float::chars_format::fixed); - if(answer.ec != std::errc()) { - std::cerr << "We want to parse up to 3.14\n"; - return false; - } - std::cout << "parsed the number " << result << std::endl; - if(answer.ptr == input.data() + 4) { - std::cout << "Parsed the number and stopped at the right character." << result << std::endl; - return true; - } - return false; + CHECK_MESSAGE(answer.ec == std::errc(), "We want to parse up to 3.14; parsed: " << result); + CHECK_MESSAGE(answer.ptr == input.data() + 4, + "Parsed the number " << result + << " and stopped at the wrong character: after " << (answer.ptr - input.data()) << " characters"); } - static const double testing_power_of_ten[] = { +static const double testing_power_of_ten[] = { 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301, 1e-300, 1e-299, 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291, 1e-290, 1e-289, 1e-288, 1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281, @@ -300,228 +190,231 @@ bool test_fixed_only() { 1e305, 1e306, 1e307, 1e308}; - - bool powers_of_ten() { - std::cout << __func__ << std::endl; +TEST_CASE("powers_of_ten") { char buf[1024]; - + CHECK_MESSAGE(1e-308 == std::pow(10, -308), "On your system, the pow function is busted. Sorry about that."); bool is_pow_correct{1e-308 == std::pow(10,-308)}; + // large negative values should be zero. int start_point = is_pow_correct ? -1000 : -307; - if(!is_pow_correct) { - std::cout << "On your system, the pow function is busted. Sorry about that. " << std::endl; - } - for (int i = start_point; i <= 308; ++i) {// large negative values should be zero. - std::cout << "."; - std::cout.flush(); + for (int i = start_point; i <= 308; ++i) { + INFO("i=" << i); size_t n = size_t(snprintf(buf, sizeof(buf), "1e%d", i)); - if (n >= sizeof(buf)) { abort(); } + REQUIRE(n < sizeof(buf)); // if false, fails the test and exits double actual; auto result = fast_float::from_chars(buf, buf + 1000, actual); - if (result.ec != std::errc()) { - std::cerr << " I could not parse " << buf << std::endl; - return false; - } - double expected = ((i >= -307) ? testing_power_of_ten[i + 307]: std::pow(10, i)); - if(actual!=expected) { - std::cerr << "String '" << buf << " parsed to " << actual; - return false; - } + CHECK_MESSAGE(result.ec == std::errc(), " I could not parse " << buf); + double expected = ((i >= -307) ? testing_power_of_ten[i + 307] : std::pow(10, i)); + CHECK_MESSAGE(actual == expected, "String '" << buf << "'parsed to " << actual); } - std::cout << std::endl; - std::cout << "Powers of 10 can be parsed exactly.\n"; - return true; - } - - void test_leading_zeroes() - { - constexpr const uint64_t bit = 1; - Assert(fast_float::leading_zeroes(0) == 64); - Assert(fast_float::leading_zeroes(bit << 0) == 63); - Assert(fast_float::leading_zeroes(bit << 1) == 62); - Assert(fast_float::leading_zeroes(bit << 2) == 61); - Assert(fast_float::leading_zeroes(bit << 61) == 2); - Assert(fast_float::leading_zeroes(bit << 62) == 1); - Assert(fast_float::leading_zeroes(bit << 63) == 0); - } - - void test_full_multiplication(uint64_t lhs, uint64_t rhs, uint64_t expected_lo, uint64_t expected_hi) - { - fast_float::value128 v; - v = fast_float::full_multiplication(lhs, rhs); - Assert(v.low == expected_lo); - Assert(v.high == expected_hi); - v = fast_float::full_multiplication(rhs, lhs); - Assert(v.low == expected_lo); - Assert(v.high == expected_hi); - } - - void test_full_multiplication() - { - constexpr const uint64_t bit = 1; - // lhs rhs lo hi - test_full_multiplication(bit << 0 , bit << 0, 1u , 0u); - test_full_multiplication(bit << 0 , bit << 63, bit << 63, 0u); - test_full_multiplication(bit << 1 , bit << 63, 0u , 1u); - test_full_multiplication(bit << 63, bit << 0, bit << 63, 0u); - test_full_multiplication(bit << 63, bit << 1, 0u , 1u); - test_full_multiplication(bit << 63, bit << 2, 0u , 2u); - test_full_multiplication(bit << 63, bit << 63, 0u , bit << 62); - } - -int main() { - test_leading_zeroes(); - test_full_multiplication(); - Assert(test_fixed_only()); - Assert(test_scientific_only()); - Assert(issue8()); - Assert(issue19()); - Assert(check_behavior()); - std::cout << "======= 64 bits " << std::endl; - Assert(basic_test_64bit("1.1920928955078125e-07",1.1920928955078125e-07)); - Assert(basic_test_64bit("INF",std::numeric_limits::infinity())); - Assert(basic_test_64bit("-INF",-std::numeric_limits::infinity())); - Assert(basic_test_64bit("INFINITY",std::numeric_limits::infinity())); - Assert(basic_test_64bit("-INFINITY",-std::numeric_limits::infinity())); - Assert(basic_test_64bit("infinity",std::numeric_limits::infinity())); - Assert(basic_test_64bit("-infinity",-std::numeric_limits::infinity())); - Assert(basic_test_64bit("inf",std::numeric_limits::infinity())); - Assert(basic_test_64bit("-inf",-std::numeric_limits::infinity())); - Assert(basic_test_64bit("9355950000000000000.00000000000000000000000000000000001844674407370955161600000184467440737095516161844674407370955161407370955161618446744073709551616000184467440737095516166000001844674407370955161618446744073709551614073709551616184467440737095516160001844674407370955161601844674407370955674451616184467440737095516140737095516161844674407370955161600018446744073709551616018446744073709551611616000184467440737095001844674407370955161600184467440737095516160018446744073709551168164467440737095516160001844073709551616018446744073709551616184467440737095516160001844674407536910751601611616000184467440737095001844674407370955161600184467440737095516160018446744073709551616184467440737095516160001844955161618446744073709551616000184467440753691075160018446744073709",0x1.03ae05e8fca1cp+63)); - Assert(basic_test_64bit("-0",-0.0)); - Assert(basic_test_64bit("2.22507385850720212418870147920222032907240528279439037814303133837435107319244194686754406432563881851382188218502438069999947733013005649884107791928741341929297200970481951993067993290969042784064731682041565926728632933630474670123316852983422152744517260835859654566319282835244787787799894310779783833699159288594555213714181128458251145584319223079897504395086859412457230891738946169368372321191373658977977723286698840356390251044443035457396733706583981055420456693824658413747607155981176573877626747665912387199931904006317334709003012790188175203447190250028061277777916798391090578584006464715943810511489154282775041174682194133952466682503431306181587829379004205392375072083366693241580002758391118854188641513168478436313080237596295773983001708984375e-308", 0x1.0000000000002p-1022)); - Assert(basic_test_64bit("1.0000000000000006661338147750939242541790008544921875",1.0000000000000007)); - Assert(basic_test_64bit("1090544144181609348835077142190",0x1.b8779f2474dfbp+99)); - Assert(basic_test_64bit("2.2250738585072013e-308",2.2250738585072013e-308)); - Assert(basic_test_64bit("-92666518056446206563E3", -92666518056446206563E3)); - Assert(basic_test_64bit("-92666518056446206563E3", -92666518056446206563E3)); - Assert(basic_test_64bit("-42823146028335318693e-128",-42823146028335318693e-128)); - Assert(basic_test_64bit("90054602635948575728E72",90054602635948575728E72)); - Assert(basic_test_64bit("1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309", 1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309)); - Assert(basic_test_64bit("0e9999999999999999999999999999", 0)); - Assert(basic_test_32bit("1234456789012345678901234567890e9999999999999999999999999999", std::numeric_limits::infinity())); - Assert(basic_test_64bit("-2139879401095466344511101915470454744.9813888656856943E+272", -std::numeric_limits::infinity())); - Assert(basic_test_64bit("-2402844368454405395.2", -2402844368454405395.2)); - Assert(basic_test_64bit("2402844368454405395.2", 2402844368454405395.2)); - Assert(basic_test_64bit("7.0420557077594588669468784357561207962098443483187940792729600000e+59", 7.0420557077594588669468784357561207962098443483187940792729600000e+59)); - Assert(basic_test_64bit("7.0420557077594588669468784357561207962098443483187940792729600000e+59", 7.0420557077594588669468784357561207962098443483187940792729600000e+59)); - Assert(basic_test_64bit("-1.7339253062092163730578609458683877051596800000000000000000000000e+42", -1.7339253062092163730578609458683877051596800000000000000000000000e+42)); - Assert(basic_test_64bit("-2.0972622234386619214559824785284023792871122537545728000000000000e+52", -2.0972622234386619214559824785284023792871122537545728000000000000e+52)); - Assert(basic_test_64bit("-1.0001803374372191849407179462120053338028379051879898808320000000e+57", -1.0001803374372191849407179462120053338028379051879898808320000000e+57)); - Assert(basic_test_64bit("-1.8607245283054342363818436991534856973992070520151142825984000000e+58", -1.8607245283054342363818436991534856973992070520151142825984000000e+58)); - Assert(basic_test_64bit("-1.9189205311132686907264385602245237137907390376574976000000000000e+52", -1.9189205311132686907264385602245237137907390376574976000000000000e+52)); - Assert(basic_test_64bit("-2.8184483231688951563253238886553506793085187889855201280000000000e+54", -2.8184483231688951563253238886553506793085187889855201280000000000e+54)); - Assert(basic_test_64bit("-1.7664960224650106892054063261344555646357024359107788800000000000e+53", -1.7664960224650106892054063261344555646357024359107788800000000000e+53)); - Assert(basic_test_64bit("-2.1470977154320536489471030463761883783915110400000000000000000000e+45", -2.1470977154320536489471030463761883783915110400000000000000000000e+45)); - Assert(basic_test_64bit("-4.4900312744003159009338275160799498340862630046359789166919680000e+61", -4.4900312744003159009338275160799498340862630046359789166919680000e+61)); - Assert(basic_test_64bit("+1", 1)); - Assert(basic_test_64bit("1.8e308", std::numeric_limits::infinity())); - Assert(basic_test_64bit("1.797693134862315700000000000000001e308", 1.7976931348623157e308)); - Assert(basic_test_64bit("1.832312213213213232132132143451234453123412321321312e308", std::numeric_limits::infinity())); - Assert(basic_test_64bit("2e30000000000000000", std::numeric_limits::infinity())); - Assert(basic_test_64bit("2e3000", std::numeric_limits::infinity())); - Assert(basic_test_64bit("1.9e308", std::numeric_limits::infinity())); - Assert(basic_test_64bit("3e-324", 0x0.0000000000001p-1022)); - Assert(basic_test_64bit("1.00000006e+09", 0x1.dcd651ep+29)); - Assert(basic_test_64bit("4.9406564584124653e-324", 0x0.0000000000001p-1022)); - Assert(basic_test_64bit("4.9406564584124654e-324", 0x0.0000000000001p-1022)); - Assert(basic_test_64bit("2.2250738585072009e-308", 0x0.fffffffffffffp-1022)); - Assert(basic_test_64bit("2.2250738585072014e-308", 0x1p-1022)); - Assert(basic_test_64bit("1.7976931348623157e308", 0x1.fffffffffffffp+1023)); - Assert(basic_test_64bit("1.7976931348623158e308", 0x1.fffffffffffffp+1023)); - Assert(basic_test_64bit("4503599627370496.5", 4503599627370496.5)); - Assert(basic_test_64bit("4503599627475352.5", 4503599627475352.5)); - Assert(basic_test_64bit("4503599627475353.5", 4503599627475353.5)); - Assert(basic_test_64bit("2251799813685248.25", 2251799813685248.25)); - Assert(basic_test_64bit("1125899906842624.125", 1125899906842624.125)); - Assert(basic_test_64bit("1125899906842901.875", 1125899906842901.875)); - Assert(basic_test_64bit("2251799813685803.75", 2251799813685803.75)); - Assert(basic_test_64bit("4503599627370497.5", 4503599627370497.5)); - Assert(basic_test_64bit("45035996.273704995", 45035996.273704995)); - Assert(basic_test_64bit("45035996.273704985", 45035996.273704985)); - Assert(basic_test_64bit("0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375)); - Assert(basic_test_64bit("0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375)); - Assert(basic_test_64bit("1438456663141390273526118207642235581183227845246331231162636653790368152091394196930365828634687637948157940776599182791387527135353034738357134110310609455693900824193549772792016543182680519740580354365467985440183598701312257624545562331397018329928613196125590274187720073914818062530830316533158098624984118889298281371812288789537310599037529113415438738954894752124724983067241108764488346454376699018673078404751121414804937224240805993123816932326223683090770561597570457793932985826162604255884529134126396282202126526253389383421806727954588525596114379801269094096329805054803089299736996870951258573010877404407451953846698609198213926882692078557033228265259305481198526059813164469187586693257335779522020407645498684263339921905227556616698129967412891282231685504660671277927198290009824680186319750978665734576683784255802269708917361719466043175201158849097881370477111850171579869056016061666173029059588433776015644439705050377554277696143928278093453792803846252715966016733222646442382892123940052441346822429721593884378212558701004356924243030059517489346646577724622498919752597382095222500311124181823512251071356181769376577651390028297796156208815375089159128394945710515861334486267101797497111125909272505194792870889617179758703442608016143343262159998149700606597792535574457560429226974273443630323818747730771316763398572110874959981923732463076884528677392654150010269822239401993427482376513231389212353583573566376915572650916866553612366187378959554983566712767093372906030188976220169058025354973622211666504549316958271880975697143546564469806791358707318873075708383345004090151974068325838177531266954177406661392229801349994695941509935655355652985723782153570084089560139142231.738475042362596875449154552392299548947138162081694168675340677843807613129780449323363759027012972466987370921816813162658754726545121090545507240267000456594786540949605260722461937870630634874991729398208026467698131898691830012167897399682179601734569071423681e-733", std::numeric_limits::infinity())); - - std::cout << std::endl; - - std::cout << "======= 32 bits " << std::endl; - Assert(basic_test_32bit("INF",std::numeric_limits::infinity())); - Assert(basic_test_32bit("-INF",-std::numeric_limits::infinity())); - Assert(basic_test_32bit("INFINITY",std::numeric_limits::infinity())); - Assert(basic_test_32bit("-INFINITY",-std::numeric_limits::infinity())); - Assert(basic_test_32bit("infinity",std::numeric_limits::infinity())); - Assert(basic_test_32bit("-infinity",-std::numeric_limits::infinity())); - Assert(basic_test_32bit("inf",std::numeric_limits::infinity())); - Assert(basic_test_32bit("-inf",-std::numeric_limits::infinity())); - Assert(basic_test_32bit("-0",-0.0f)); - Assert(basic_test_32bit("1090544144181609348835077142190",0x1.b877ap+99f)); - Assert(basic_test_32bit("1.1754943508e-38",1.1754943508e-38f)); - Assert(basic_test_32bit("30219.0830078125",30219.0830078125f)); - Assert(basic_test_32bit("16252921.5",16252921.5f)); - Assert(basic_test_32bit("5322519.25",5322519.25f)); - Assert(basic_test_32bit("3900245.875",3900245.875f)); - Assert(basic_test_32bit("1510988.3125",1510988.3125f)); - Assert(basic_test_32bit("782262.28125",782262.28125f)); - Assert(basic_test_32bit("328381.484375",328381.484375f)); - Assert(basic_test_32bit("156782.0703125",156782.0703125f)); - Assert(basic_test_32bit("85003.24609375",85003.24609375f)); - Assert(basic_test_32bit("43827.048828125",43827.048828125f)); - Assert(basic_test_32bit("17419.6494140625",17419.6494140625f)); - Assert(basic_test_32bit("15498.36376953125",15498.36376953125f)); - Assert(basic_test_32bit("6318.580322265625",6318.580322265625f)); - Assert(basic_test_32bit("2525.2840576171875",2525.2840576171875f)); - Assert(basic_test_32bit("1370.9265747070312",1370.9265747070312f)); - Assert(basic_test_32bit("936.3702087402344",936.3702087402344f)); - Assert(basic_test_32bit("411.88682556152344",411.88682556152344f)); - Assert(basic_test_32bit("206.50310516357422",206.50310516357422f)); - Assert(basic_test_32bit("124.16878890991211",124.16878890991211f)); - Assert(basic_test_32bit("50.811574935913086",50.811574935913086f)); - Assert(basic_test_32bit("17.486443519592285",17.486443519592285f)); - Assert(basic_test_32bit("13.91745138168335",13.91745138168335f)); - Assert(basic_test_32bit("7.5464513301849365",0x1.e2f90ep+2f)); - Assert(basic_test_32bit("2.687217116355896",2.687217116355896f)); - Assert(basic_test_32bit("1.1877630352973938",0x1.30113ep+0f)); - Assert(basic_test_32bit("0.7622503340244293",0.7622503340244293f)); - Assert(basic_test_32bit("0.30531780421733856",0x1.38a53ap-2f)); - Assert(basic_test_32bit("0.21791061013936996",0x1.be47eap-3f)); - Assert(basic_test_32bit("0.09289376810193062",0x1.7c7e2ep-4f)); - Assert(basic_test_32bit("0.03706067614257336",0.03706067614257336f)); - Assert(basic_test_32bit("0.028068351559340954",0.028068351559340954f)); - Assert(basic_test_32bit("0.012114629615098238",0x1.8cf8e2p-7f)); - Assert(basic_test_32bit("0.004221370676532388",0x1.14a6dap-8f)); - Assert(basic_test_32bit("0.002153817447833717",0.002153817447833717f)); - Assert(basic_test_32bit("0.0015924838953651488",0x1.a175cap-10f)); - Assert(basic_test_32bit("0.0008602388261351734",0.0008602388261351734f)); - Assert(basic_test_32bit("0.00036393293703440577",0x1.7d9c82p-12f)); - Assert(basic_test_32bit("0.00013746770127909258",0.00013746770127909258f)); - Assert(basic_test_32bit("16407.9462890625", 16407.9462890625f)); - Assert(basic_test_32bit("1.1754947011469036e-38", 0x1.000006p-126f)); - Assert(basic_test_32bit("7.0064923216240854e-46", 0x1p-149f)); - Assert(basic_test_32bit("8388614.5", 8388614.5f)); - Assert(basic_test_32bit("0e9999999999999999999999999999", 0)); - Assert(basic_test_32bit("1234456789012345678901234567890e9999999999999999999999999999", std::numeric_limits::infinity())); - Assert(basic_test_32bit("4.7019774032891500318749461488889827112746622270883500860350068251e-38",4.7019774032891500318749461488889827112746622270883500860350068251e-38f)); - Assert(basic_test_32bit("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679", 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f)); - Assert(basic_test_32bit("2.3509887016445750159374730744444913556373311135441750430175034126e-38", 2.3509887016445750159374730744444913556373311135441750430175034126e-38f)); - Assert(basic_test_32bit("+1", 1)); - Assert(basic_test_32bit("2e3000", std::numeric_limits::infinity())); - Assert(basic_test_32bit("3.5028234666e38", std::numeric_limits::infinity())); - Assert(basic_test_32bit("7.0060e-46", 0)); - Assert(basic_test_32bit(1.00000006e+09f)); - Assert(basic_test_32bit(1.4012984643e-45f)); - Assert(basic_test_32bit(1.1754942107e-38f)); - Assert(basic_test_32bit(1.1754943508e-45f)); - Assert(basic_test_32bit("3.4028234664e38", 0x1.fffffep+127f)); - Assert(basic_test_32bit("3.4028234665e38", 0x1.fffffep+127f)); - Assert(basic_test_32bit("3.4028234666e38", 0x1.fffffep+127f)); - Assert(basic_test_32bit("0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625", 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625)); - Assert(basic_test_32bit("0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125", 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125f)); - Assert(basic_test_32bit("0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875", 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875f)); - Assert(basic_test_32bit("0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875", 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f)); - std::cout << std::endl; - - Assert(powers_of_ten()); - - std::cout << "All ok" << std::endl; - return EXIT_SUCCESS; +} + + +template std::string to_string(T d) { + std::string s(64, '\0'); + auto written = std::snprintf(&s[0], s.size(), "%.*e", + std::numeric_limits::max_digits10 - 1, d); + s.resize(size_t(written)); + return s; +} + +template std::string to_long_string(T d) { + std::string s(4096, '\0'); + auto written = std::snprintf(&s[0], s.size(), "%.*e", + std::numeric_limits::max_digits10 * 10, d); + s.resize(size_t(written)); + return s; +} + +uint32_t get_mantissa(float f) { + uint32_t m; + memcpy(&m, &f, sizeof(f)); + return (m & ((uint32_t(1)<<23)-1)); +} + +uint64_t get_mantissa(double f) { + uint64_t m; + memcpy(&m, &f, sizeof(f)); + return (m & ((uint64_t(1)<<57)-1)); +} + + +template +void basic_test(std::string str, T expected) { + T actual; + auto result = fast_float::from_chars(str.data(), str.data() + str.size(), actual); + INFO("str=" << str << "\n" + << " expected=" << fHexAndDec(expected) << "\n" + << " ..actual=" << fHexAndDec(actual) << "\n" + << " expected mantissa=" << iHexAndDec(get_mantissa(expected)) << "\n" + << " ..actual mantissa=" << iHexAndDec(get_mantissa(actual))); + CHECK_EQ(result.ec, std::errc()); + CHECK_EQ(copysign(1, actual), copysign(1, expected)); + CHECK_EQ(std::isnan(actual), std::isnan(expected)); + CHECK_EQ(actual, expected); +} + +void basic_test(float val) { + { + std::string long_vals = to_long_string(val); + INFO("long vals: " << long_vals); + basic_test(long_vals, val); + } + { + std::string vals = to_string(val); + INFO("vals: " << vals); + basic_test(vals, val); + } +} + +#define verify(lhs, rhs) { INFO(lhs); basic_test(lhs, rhs); } +#define verify32(val) { INFO(#val); basic_test(val); } + +TEST_CASE("64bit.inf") { + verify("INF", std::numeric_limits::infinity()); + verify("-INF", -std::numeric_limits::infinity()); + verify("INFINITY", std::numeric_limits::infinity()); + verify("-INFINITY", -std::numeric_limits::infinity()); + verify("infinity", std::numeric_limits::infinity()); + verify("-infinity", -std::numeric_limits::infinity()); + verify("inf", std::numeric_limits::infinity()); + verify("-inf", -std::numeric_limits::infinity()); + verify("1234456789012345678901234567890e9999999999999999999999999999", std::numeric_limits::infinity()); + verify("-2139879401095466344511101915470454744.9813888656856943E+272", -std::numeric_limits::infinity()); + verify("1.8e308", std::numeric_limits::infinity()); + verify("1.832312213213213232132132143451234453123412321321312e308", std::numeric_limits::infinity()); + verify("2e30000000000000000", std::numeric_limits::infinity()); + verify("2e3000", std::numeric_limits::infinity()); + verify("1.9e308", std::numeric_limits::infinity()); +} + +TEST_CASE("64bit.general") { + verify("1.1920928955078125e-07", 1.1920928955078125e-07); + verify("9355950000000000000.00000000000000000000000000000000001844674407370955161600000184467440737095516161844674407370955161407370955161618446744073709551616000184467440737095516166000001844674407370955161618446744073709551614073709551616184467440737095516160001844674407370955161601844674407370955674451616184467440737095516140737095516161844674407370955161600018446744073709551616018446744073709551611616000184467440737095001844674407370955161600184467440737095516160018446744073709551168164467440737095516160001844073709551616018446744073709551616184467440737095516160001844674407536910751601611616000184467440737095001844674407370955161600184467440737095516160018446744073709551616184467440737095516160001844955161618446744073709551616000184467440753691075160018446744073709",0x1.03ae05e8fca1cp+63); + verify("-0",-0.0); + verify("2.22507385850720212418870147920222032907240528279439037814303133837435107319244194686754406432563881851382188218502438069999947733013005649884107791928741341929297200970481951993067993290969042784064731682041565926728632933630474670123316852983422152744517260835859654566319282835244787787799894310779783833699159288594555213714181128458251145584319223079897504395086859412457230891738946169368372321191373658977977723286698840356390251044443035457396733706583981055420456693824658413747607155981176573877626747665912387199931904006317334709003012790188175203447190250028061277777916798391090578584006464715943810511489154282775041174682194133952466682503431306181587829379004205392375072083366693241580002758391118854188641513168478436313080237596295773983001708984375e-308", 0x1.0000000000002p-1022); + verify("1.0000000000000006661338147750939242541790008544921875",1.0000000000000007); + verify("1090544144181609348835077142190",0x1.b8779f2474dfbp+99); + verify("2.2250738585072013e-308", 2.2250738585072013e-308); + verify("-92666518056446206563E3", -92666518056446206563E3); + verify("-92666518056446206563E3", -92666518056446206563E3); + verify("-42823146028335318693e-128",-42823146028335318693e-128); + verify("90054602635948575728E72",90054602635948575728E72); + verify("1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309", 1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309); + verify("0e9999999999999999999999999999", 0.0); + verify("-2402844368454405395.2", -2402844368454405395.2); + verify("2402844368454405395.2", 2402844368454405395.2); + verify("7.0420557077594588669468784357561207962098443483187940792729600000e+59", 7.0420557077594588669468784357561207962098443483187940792729600000e+59); + verify("7.0420557077594588669468784357561207962098443483187940792729600000e+59", 7.0420557077594588669468784357561207962098443483187940792729600000e+59); + verify("-1.7339253062092163730578609458683877051596800000000000000000000000e+42", -1.7339253062092163730578609458683877051596800000000000000000000000e+42); + verify("-2.0972622234386619214559824785284023792871122537545728000000000000e+52", -2.0972622234386619214559824785284023792871122537545728000000000000e+52); + verify("-1.0001803374372191849407179462120053338028379051879898808320000000e+57", -1.0001803374372191849407179462120053338028379051879898808320000000e+57); + verify("-1.8607245283054342363818436991534856973992070520151142825984000000e+58", -1.8607245283054342363818436991534856973992070520151142825984000000e+58); + verify("-1.9189205311132686907264385602245237137907390376574976000000000000e+52", -1.9189205311132686907264385602245237137907390376574976000000000000e+52); + verify("-2.8184483231688951563253238886553506793085187889855201280000000000e+54", -2.8184483231688951563253238886553506793085187889855201280000000000e+54); + verify("-1.7664960224650106892054063261344555646357024359107788800000000000e+53", -1.7664960224650106892054063261344555646357024359107788800000000000e+53); + verify("-2.1470977154320536489471030463761883783915110400000000000000000000e+45", -2.1470977154320536489471030463761883783915110400000000000000000000e+45); + verify("-4.4900312744003159009338275160799498340862630046359789166919680000e+61", -4.4900312744003159009338275160799498340862630046359789166919680000e+61); + verify("+1", 1.0); + verify("1.797693134862315700000000000000001e308", 1.7976931348623157e308); + verify("3e-324", 0x0.0000000000001p-1022); + verify("1.00000006e+09", 0x1.dcd651ep+29); + verify("4.9406564584124653e-324", 0x0.0000000000001p-1022); + verify("4.9406564584124654e-324", 0x0.0000000000001p-1022); + verify("2.2250738585072009e-308", 0x0.fffffffffffffp-1022); + verify("2.2250738585072014e-308", 0x1p-1022); + verify("1.7976931348623157e308", 0x1.fffffffffffffp+1023); + verify("1.7976931348623158e308", 0x1.fffffffffffffp+1023); + verify("4503599627370496.5", 4503599627370496.5); + verify("4503599627475352.5", 4503599627475352.5); + verify("4503599627475353.5", 4503599627475353.5); + verify("2251799813685248.25", 2251799813685248.25); + verify("1125899906842624.125", 1125899906842624.125); + verify("1125899906842901.875", 1125899906842901.875); + verify("2251799813685803.75", 2251799813685803.75); + verify("4503599627370497.5", 4503599627370497.5); + verify("45035996.273704995", 45035996.273704995); + verify("45035996.273704985", 45035996.273704985); + verify("0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375); + verify("0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375); + verify("1438456663141390273526118207642235581183227845246331231162636653790368152091394196930365828634687637948157940776599182791387527135353034738357134110310609455693900824193549772792016543182680519740580354365467985440183598701312257624545562331397018329928613196125590274187720073914818062530830316533158098624984118889298281371812288789537310599037529113415438738954894752124724983067241108764488346454376699018673078404751121414804937224240805993123816932326223683090770561597570457793932985826162604255884529134126396282202126526253389383421806727954588525596114379801269094096329805054803089299736996870951258573010877404407451953846698609198213926882692078557033228265259305481198526059813164469187586693257335779522020407645498684263339921905227556616698129967412891282231685504660671277927198290009824680186319750978665734576683784255802269708917361719466043175201158849097881370477111850171579869056016061666173029059588433776015644439705050377554277696143928278093453792803846252715966016733222646442382892123940052441346822429721593884378212558701004356924243030059517489346646577724622498919752597382095222500311124181823512251071356181769376577651390028297796156208815375089159128394945710515861334486267101797497111125909272505194792870889617179758703442608016143343262159998149700606597792535574457560429226974273443630323818747730771316763398572110874959981923732463076884528677392654150010269822239401993427482376513231389212353583573566376915572650916866553612366187378959554983566712767093372906030188976220169058025354973622211666504549316958271880975697143546564469806791358707318873075708383345004090151974068325838177531266954177406661392229801349994695941509935655355652985723782153570084089560139142231.738475042362596875449154552392299548947138162081694168675340677843807613129780449323363759027012972466987370921816813162658754726545121090545507240267000456594786540949605260722461937870630634874991729398208026467698131898691830012167897399682179601734569071423681e-733", std::numeric_limits::infinity()); +} + + +TEST_CASE("32bit.inf") { + verify("INF", std::numeric_limits::infinity()); + verify("-INF", -std::numeric_limits::infinity()); + verify("INFINITY", std::numeric_limits::infinity()); + verify("-INFINITY", -std::numeric_limits::infinity()); + verify("infinity", std::numeric_limits::infinity()); + verify("-infinity", -std::numeric_limits::infinity()); + verify("inf", std::numeric_limits::infinity()); + verify("-inf", -std::numeric_limits::infinity()); + verify("1234456789012345678901234567890e9999999999999999999999999999", std::numeric_limits::infinity()); + verify("2e3000", std::numeric_limits::infinity()); + verify("3.5028234666e38", std::numeric_limits::infinity()); +} + +TEST_CASE("32bit.general") { + verify32(1.00000006e+09f); + verify32(1.4012984643e-45f); + verify32(1.1754942107e-38f); + verify32(1.1754943508e-45f); + verify("-0", -0.0f); + verify("1090544144181609348835077142190", 0x1.b877ap+99f); + verify("1.1754943508e-38", 1.1754943508e-38f); + verify("30219.0830078125", 30219.0830078125f); + verify("16252921.5", 16252921.5f); + verify("5322519.25", 5322519.25f); + verify("3900245.875", 3900245.875f); + verify("1510988.3125", 1510988.3125f); + verify("782262.28125", 782262.28125f); + verify("328381.484375", 328381.484375f); + verify("156782.0703125", 156782.0703125f); + verify("85003.24609375", 85003.24609375f); + verify("43827.048828125", 43827.048828125f); + verify("17419.6494140625", 17419.6494140625f); + verify("15498.36376953125", 15498.36376953125f); + verify("6318.580322265625", 6318.580322265625f); + verify("2525.2840576171875", 2525.2840576171875f); + verify("1370.9265747070312", 1370.9265747070312f); + verify("936.3702087402344", 936.3702087402344f); + verify("411.88682556152344", 411.88682556152344f); + verify("206.50310516357422", 206.50310516357422f); + verify("124.16878890991211", 124.16878890991211f); + verify("50.811574935913086", 50.811574935913086f); + verify("17.486443519592285", 17.486443519592285f); + verify("13.91745138168335", 13.91745138168335f); + verify("7.5464513301849365", 0x1.e2f90ep+2f); + verify("2.687217116355896", 2.687217116355896f); + verify("1.1877630352973938", 0x1.30113ep+0f); + verify("0.7622503340244293", 0.7622503340244293f); + verify("0.30531780421733856", 0x1.38a53ap-2f); + verify("0.21791061013936996", 0x1.be47eap-3f); + verify("0.09289376810193062", 0x1.7c7e2ep-4f); + verify("0.03706067614257336", 0.03706067614257336f); + verify("0.028068351559340954", 0.028068351559340954f); + verify("0.012114629615098238", 0x1.8cf8e2p-7f); + verify("0.004221370676532388", 0x1.14a6dap-8f); + verify("0.002153817447833717", 0.002153817447833717f); + verify("0.0015924838953651488", 0x1.a175cap-10f); + verify("0.0008602388261351734", 0.0008602388261351734f); + verify("0.00036393293703440577", 0x1.7d9c82p-12f); + verify("0.00013746770127909258", 0.00013746770127909258f); + verify("16407.9462890625", 16407.9462890625f); + verify("1.1754947011469036e-38", 0x1.000006p-126f); + verify("7.0064923216240854e-46", 0x1p-149f); + verify("8388614.5", 8388614.5f); + verify("0e9999999999999999999999999999", 0.f); + verify("4.7019774032891500318749461488889827112746622270883500860350068251e-38",4.7019774032891500318749461488889827112746622270883500860350068251e-38f); + verify("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679", 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f); + verify("2.3509887016445750159374730744444913556373311135441750430175034126e-38", 2.3509887016445750159374730744444913556373311135441750430175034126e-38f); + verify("+1", 1.f); + verify("7.0060e-46", 0.f); + verify("3.4028234664e38", 0x1.fffffep+127f); + verify("3.4028234665e38", 0x1.fffffep+127f); + verify("3.4028234666e38", 0x1.fffffep+127f); + verify("0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625", 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625); + verify("0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125", 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125f); + verify("0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875", 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875f); + verify("0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875", 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f); } From aca3ca782848f4937c51644cdda0611b45bf2981 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Tue, 17 Nov 2020 02:07:31 +0000 Subject: [PATCH 10/25] fix CI files: ubuntu required a more recent cmake, vs had a typo --- .github/workflows/ubuntu18.yml | 2 +- .github/workflows/ubuntu20.yml | 2 +- .github/workflows/vs16-ci.yml | 4 +++- .github/workflows/vs16-clang-ci.yml | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ubuntu18.yml b/.github/workflows/ubuntu18.yml index bf1f2d0..2636550 100644 --- a/.github/workflows/ubuntu18.yml +++ b/.github/workflows/ubuntu18.yml @@ -18,7 +18,7 @@ jobs: - name: Setup cmake uses: jwlawson/actions-setup-cmake@v1.0 with: - cmake-version: '3.9.x' + cmake-version: '3.12.x' - name: Install older compilers run: | sudo -E dpkg --add-architecture i386 diff --git a/.github/workflows/ubuntu20.yml b/.github/workflows/ubuntu20.yml index 6bf0dcc..883aab4 100644 --- a/.github/workflows/ubuntu20.yml +++ b/.github/workflows/ubuntu20.yml @@ -17,7 +17,7 @@ jobs: - name: Setup cmake uses: jwlawson/actions-setup-cmake@v1.0 with: - cmake-version: '3.9.x' + cmake-version: '3.12.x' - name: install older compilers run: | sudo -E dpkg --add-architecture i386 diff --git a/.github/workflows/vs16-ci.yml b/.github/workflows/vs16-ci.yml index 8548aba..eb4330b 100644 --- a/.github/workflows/vs16-ci.yml +++ b/.github/workflows/vs16-ci.yml @@ -22,4 +22,6 @@ jobs: - name: Build run: cmake --build build --config Release --parallel - name: Run basic tests - run: cd build && ctest -C Release --output-on-failure -R basictest + run: | + cd build + ctest -C Release --output-on-failure -R basictest diff --git a/.github/workflows/vs16-clang-ci.yml b/.github/workflows/vs16-clang-ci.yml index 559630d..a32c6b8 100644 --- a/.github/workflows/vs16-clang-ci.yml +++ b/.github/workflows/vs16-clang-ci.yml @@ -22,4 +22,6 @@ jobs: - name: Build run: cmake --build build --config Release --parallel - name: Run basic tests - run: cd build && ctest -C Release --output-on-failure -R basictest + run: | + cd build + ctest -C Release --output-on-failure -R basictest From 3566d303f4af292e1d7695b2141ea11ebf995b65 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Tue, 17 Nov 2020 02:13:25 +0000 Subject: [PATCH 11/25] cmake: fetch content requires v3.11+ --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6be510f..70c4b19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.9) +cmake_minimum_required(VERSION 3.11 REQUIRED) project(fast_float VERSION 0.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) @@ -31,4 +31,4 @@ endif() if(FASTFLOAT_TEST) enable_testing() add_subdirectory(tests) -endif(FASTFLOAT_TEST) \ No newline at end of file +endif(FASTFLOAT_TEST) From 2bc8dcc734047125941829976e8e545f88c8604f Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Tue, 17 Nov 2020 02:14:24 +0000 Subject: [PATCH 12/25] fix: jwlawson/actions-setup-cmake uses deprecated github commands see https://github.com/lemire/fast_float/pull/36/checks?check_run_id=1409838155#step:3:13 --- .github/workflows/ubuntu18.yml | 4 ---- .github/workflows/ubuntu20.yml | 4 ---- CMakeLists.txt | 2 +- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/ubuntu18.yml b/.github/workflows/ubuntu18.yml index 2636550..4f06fa5 100644 --- a/.github/workflows/ubuntu18.yml +++ b/.github/workflows/ubuntu18.yml @@ -15,10 +15,6 @@ jobs: - {cxx: , arch: -DCMAKE_CXX_FLAGS="-m32"} # default=gcc7 steps: - uses: actions/checkout@v2 - - name: Setup cmake - uses: jwlawson/actions-setup-cmake@v1.0 - with: - cmake-version: '3.12.x' - name: Install older compilers run: | sudo -E dpkg --add-architecture i386 diff --git a/.github/workflows/ubuntu20.yml b/.github/workflows/ubuntu20.yml index 883aab4..11ac3a4 100644 --- a/.github/workflows/ubuntu20.yml +++ b/.github/workflows/ubuntu20.yml @@ -14,10 +14,6 @@ jobs: - {cxx: , arch: -DCMAKE_CXX_FLAGS="-m32"} # default=gcc9 steps: - uses: actions/checkout@v2 - - name: Setup cmake - uses: jwlawson/actions-setup-cmake@v1.0 - with: - cmake-version: '3.12.x' - name: install older compilers run: | sudo -E dpkg --add-architecture i386 diff --git a/CMakeLists.txt b/CMakeLists.txt index 70c4b19..4be7a45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11 REQUIRED) +cmake_minimum_required(VERSION 3.11 FATAL_ERROR) project(fast_float VERSION 0.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) From 216d191384f913703ce77295523692e23fa90bca Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Thu, 19 Nov 2020 21:54:13 +0000 Subject: [PATCH 13/25] basictest: do not print double 0x --- tests/basictest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/basictest.cpp b/tests/basictest.cpp index a699198..fc42681 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -17,7 +17,7 @@ TEST_CASE("leading_zeroes") { } #define iHexAndDec(v) std::hex << "0x" << (v) << " (" << std::dec << (v) << ")" -#define fHexAndDec(v) std::hexfloat << "0x" << (v) << " (" << std::defaultfloat << (v) << ")" +#define fHexAndDec(v) std::hexfloat << (v) << " (" << std::defaultfloat << (v) << ")" void test_full_multiplication(uint64_t lhs, uint64_t rhs, uint64_t expected_lo, uint64_t expected_hi) { fast_float::value128 v; From 8a04a06a88997c624a746eaf81c0b6e9e183884b Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Thu, 19 Nov 2020 22:55:48 +0000 Subject: [PATCH 14/25] leading_zeroes(): 0 is not a valid input --- include/fast_float/float_common.h | 30 ++++++++++++++---------------- tests/basictest.cpp | 1 - 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 2c0c18e..0f17329 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -3,6 +3,7 @@ #include #include +#include #if defined(_MSC_VER) && !defined(__clang__) #define FASTFLOAT_VISUAL_STUDIO 1 @@ -89,26 +90,23 @@ struct value128 { /* result might be undefined when input_num is zero */ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { + assert(input_num > 0); #ifdef FASTFLOAT_VISUAL_STUDIO -#if defined(_M_X64) || defined(_M_ARM64) + #if defined(_M_X64) || defined(_M_ARM64) unsigned long leading_zero = 0; // Search the mask data from most significant bit (MSB) // to least significant bit (LSB) for a set bit (1). - if (_BitScanReverse64(&leading_zero, input_num)) - return (int)(63 - leading_zero); - else - return 64; -#else - if(!input_num) return 64; - int n = 0; - if(input_num & uint64_t(0xffffffff00000000)) input_num >>= 32, n |= 32; - if(input_num & uint64_t( 0xffff0000)) input_num >>= 16, n |= 16; - if(input_num & uint64_t( 0xff00)) input_num >>= 8, n |= 8; - if(input_num & uint64_t( 0xf0)) input_num >>= 4, n |= 4; - if(input_num & uint64_t( 0xc)) input_num >>= 2, n |= 2; - if(input_num & uint64_t( 0x2)) input_num >>= 1, n |= 1; - return 63 - n; -#endif + return (int)(_BitScanReverse64(&leading_zero, input_num) - leading_zero); + #else + int n = 0; + if(input_num & uint64_t(0xffffffff00000000)) input_num >>= 32, n |= 32; + if(input_num & uint64_t( 0xffff0000)) input_num >>= 16, n |= 16; + if(input_num & uint64_t( 0xff00)) input_num >>= 8, n |= 8; + if(input_num & uint64_t( 0xf0)) input_num >>= 4, n |= 4; + if(input_num & uint64_t( 0xc)) input_num >>= 2, n |= 2; + if(input_num & uint64_t( 0x2)) input_num >>= 1, n |= 1; + return 63 - n; + #endif #else return __builtin_clzll(input_num); #endif diff --git a/tests/basictest.cpp b/tests/basictest.cpp index fc42681..a2f3110 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -7,7 +7,6 @@ TEST_CASE("leading_zeroes") { constexpr const uint64_t bit = 1; - CHECK(fast_float::leading_zeroes(0) == 64); CHECK(fast_float::leading_zeroes(bit << 0) == 63); CHECK(fast_float::leading_zeroes(bit << 1) == 62); CHECK(fast_float::leading_zeroes(bit << 2) == 61); From 5ce64de5243d0cfbded8ec6b66804db50ff786b2 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Thu, 19 Nov 2020 23:22:55 +0000 Subject: [PATCH 15/25] fix: full 64bit multiplication working on 32bit gcc/clang --- .gitignore | 4 +- include/fast_float/float_common.h | 74 +++++++++++++------------------ 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index a5a3ead..1566557 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/* -Testing/* \ No newline at end of file +Testing/* +.cache/ +compile_commands.json diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 0f17329..7e84b01 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -112,19 +112,29 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #endif } -#if defined(_WIN32) && !defined(__clang__) -// Note MinGW falls here too -#include -#if !defined(_M_X64) && !defined(_M_ARM64) // _umul128 for x86, arm -// this is a slow emulation routine for 32-bit Windows - -#ifdef __MINGW32__ -fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { - return x * (uint64_t)y; -} +#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) \ + || defined(__arm__) || defined(__MINGW32__)) +#define FASTFLOAT_32BIT +#elif (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \ + || defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64)) +#define FASTFLOAT_64BIT +#else +#error Unknown platform #endif + +#ifdef FASTFLOAT_32BIT +#if (defined(_WIN32) && !defined(__clang__)) +#include +#endif + +// slow emulation routine for 32-bit +fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { + return x * (uint64_t)y; +} + +// slow emulation routine for 32-bit fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi) { uint64_t ad = __emulu((uint32_t)(ab >> 32), (uint32_t)cd); @@ -136,51 +146,29 @@ fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, (adbc_carry << 32) + !!(lo < bd); return lo; } -#endif +#endif // FASTFLOAT_32BIT -fastfloat_really_inline value128 full_multiplication(uint64_t value1, - uint64_t value2) { - value128 answer; -#ifdef _M_ARM64 - // ARM64 has native support for 64-bit multiplications, no need to emultate - answer.high = __umulh(value1, value2); - answer.low = value1 * value2; -#else - answer.low = - _umul128(value1, value2, &answer.high); // _umul128 not available on ARM64 -#endif // _M_ARM64 - return answer; -} -#else // gcc - -// compute value1 * value2 +// compute 64-bit a*b fastfloat_really_inline value128 full_multiplication(uint64_t a, uint64_t b) { value128 answer; -#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__arm__) - static constexpr const uint64_t lo32 = 0xffffffffu; - // https://stackoverflow.com/questions/28868367/getting-the-high-part-of-64-bit-integer-multiplication - uint64_t a_lo = a & lo32; - uint64_t a_hi = a >> 32; - uint64_t b_lo = b & lo32; - uint64_t b_hi = b >> 32; - uint64_t ab_hi = a_hi * b_hi; - uint64_t ab_mid = a_hi * b_lo; - uint64_t ba_mid = b_hi * a_lo; - uint64_t ab_lo = a_lo * b_lo; - uint64_t carry_bit = ((ab_mid & lo32) + (ba_mid & lo32) + (ab_lo >> 32)) >> 32; - answer.high = ab_hi + (ab_mid >> 32) + (ba_mid >> 32) + carry_bit; - answer.low = ab_lo + (ab_mid & lo32) + (ba_mid & lo32); -#else // if defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64) +#ifdef _M_ARM64 + // ARM64 has native support for 64-bit multiplications, no need to emulate + answer.high = __umulh(a, b); + answer.low = a * b; +#elif defined(FASTFLOAT_32BIT) + answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64 +#elif defined(FASTFLOAT_64BIT) __uint128_t r = ((__uint128_t)a) * b; answer.low = uint64_t(r); answer.high = uint64_t(r >> 64); +#else + #error Not implemented #endif return answer; } -#endif struct adjusted_mantissa { uint64_t mantissa{0}; From f7b13da349abf03c4f2b091f7d13ec78a650fcc6 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 00:16:10 +0000 Subject: [PATCH 16/25] fix: readjust full_multiplication() and leading_zeroes() on windows --- include/fast_float/float_common.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 7e84b01..df16ec3 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -96,16 +96,17 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { unsigned long leading_zero = 0; // Search the mask data from most significant bit (MSB) // to least significant bit (LSB) for a set bit (1). - return (int)(_BitScanReverse64(&leading_zero, input_num) - leading_zero); + _BitScanReverse64(&leading_zero, input_num); + return (int)(63 - leading_zero); #else - int n = 0; - if(input_num & uint64_t(0xffffffff00000000)) input_num >>= 32, n |= 32; - if(input_num & uint64_t( 0xffff0000)) input_num >>= 16, n |= 16; - if(input_num & uint64_t( 0xff00)) input_num >>= 8, n |= 8; - if(input_num & uint64_t( 0xf0)) input_num >>= 4, n |= 4; - if(input_num & uint64_t( 0xc)) input_num >>= 2, n |= 2; - if(input_num & uint64_t( 0x2)) input_num >>= 1, n |= 1; - return 63 - n; + int last_bit = 0; + if(input_num & uint64_t(0xffffffff00000000)) input_num >>= 32, last_bit |= 32; + if(input_num & uint64_t( 0xffff0000)) input_num >>= 16, last_bit |= 16; + if(input_num & uint64_t( 0xff00)) input_num >>= 8, last_bit |= 8; + if(input_num & uint64_t( 0xf0)) input_num >>= 4, last_bit |= 4; + if(input_num & uint64_t( 0xc)) input_num >>= 2, last_bit |= 2; + if(input_num & uint64_t( 0x2)) input_num >>= 1, last_bit |= 1; + return 63 - last_bit; #endif #else return __builtin_clzll(input_num); @@ -124,15 +125,18 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #endif -#ifdef FASTFLOAT_32BIT -#if (defined(_WIN32) && !defined(__clang__)) +#if ((defined(_WIN32) || defined(_WIN64)) && !defined(__clang__)) #include #endif +#ifdef FASTFLOAT_32BIT + +#if !defined(_WIN32) // slow emulation routine for 32-bit fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { return x * (uint64_t)y; } +#endif // slow emulation routine for 32-bit fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, @@ -157,7 +161,7 @@ fastfloat_really_inline value128 full_multiplication(uint64_t a, // ARM64 has native support for 64-bit multiplications, no need to emulate answer.high = __umulh(a, b); answer.low = a * b; -#elif defined(FASTFLOAT_32BIT) +#elif defined(FASTFLOAT_32BIT) || (defined(_WIN64)) answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64 #elif defined(FASTFLOAT_64BIT) __uint128_t r = ((__uint128_t)a) * b; From bfa33b3ed16dc831d215983e87551120197abf67 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 00:26:33 +0000 Subject: [PATCH 17/25] fix mingw compile errors --- include/fast_float/float_common.h | 5 ++++- tests/basictest.cpp | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index df16ec3..d604dec 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -131,7 +131,7 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #ifdef FASTFLOAT_32BIT -#if !defined(_WIN32) +#if (!defined(_WIN32)) || defined(__MINGW32__) // slow emulation routine for 32-bit fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { return x * (uint64_t)y; @@ -139,6 +139,7 @@ fastfloat_really_inline uint64_t __emulu(uint32_t x, uint32_t y) { #endif // slow emulation routine for 32-bit +#if !defined(__MINGW64__) fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi) { uint64_t ad = __emulu((uint32_t)(ab >> 32), (uint32_t)cd); @@ -150,6 +151,8 @@ fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd, (adbc_carry << 32) + !!(lo < bd); return lo; } +#endif // !__MINGW64__ + #endif // FASTFLOAT_32BIT diff --git a/tests/basictest.cpp b/tests/basictest.cpp index a2f3110..d0a7d48 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -1,9 +1,10 @@ -#include "fast_float/fast_float.h" -#include #define DOCTEST_CONFIG_SUPER_FAST_ASSERTS #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include +#include "fast_float/fast_float.h" +#include + TEST_CASE("leading_zeroes") { constexpr const uint64_t bit = 1; From 33b64cf0d3824d2a28e5558a7d9d1786322bfb1f Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 01:08:53 +0000 Subject: [PATCH 18/25] fix: FetchContent_MakeAvailable() was introduced only in cmake 3.14 --- .travis.yml | 5 +++++ CMakeLists.txt | 2 +- tests/CMakeLists.txt | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 616ab64..6f1b5bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,11 @@ language: cpp dist: bionic +addons: + snaps: + - name: cmake + confinement: classic + channel: 3.16/stable arch: - amd64 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4be7a45..2d10096 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11 FATAL_ERROR) +cmake_minimum_required(VERSION 3.11) project(fast_float VERSION 0.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cce9438..04129d0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,7 @@ +# FetchContent_MakeAvailable() was only introduced in 3.14 +# https://cmake.org/cmake/help/v3.14/release/3.14.html#modules +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + include(FetchContent) FetchContent_Declare(doctest From d617b97c7ce4c190a25ffb6fb574b4f7cabe8fd1 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 09:43:27 +0000 Subject: [PATCH 19/25] update authors --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 77cd15b..60c9425 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ -Daniel Lemire \ No newline at end of file +Daniel Lemire +João Paulo Magalhaes From 9afc814fb67460d064855a5734b763a2b470c167 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 09:44:27 +0000 Subject: [PATCH 20/25] tidy float_common.h: put feature test macros at the top --- include/fast_float/float_common.h | 33 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index d604dec..8791709 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -5,12 +5,25 @@ #include #include +#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) \ + || defined(__arm__) || defined(__MINGW32__)) +#define FASTFLOAT_32BIT +#elif (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \ + || defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \ + || defined(__MINGW64__)) +#define FASTFLOAT_64BIT +#else +#error Unknown platform +#endif + +#if ((defined(_WIN32) || defined(_WIN64)) && !defined(__clang__)) +#include +#endif + #if defined(_MSC_VER) && !defined(__clang__) #define FASTFLOAT_VISUAL_STUDIO 1 #endif - - #ifdef _WIN32 #define FASTFLOAT_IS_BIG_ENDIAN 0 #else @@ -113,22 +126,6 @@ fastfloat_really_inline int leading_zeroes(uint64_t input_num) { #endif } - -#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) \ - || defined(__arm__) || defined(__MINGW32__)) -#define FASTFLOAT_32BIT -#elif (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \ - || defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64)) -#define FASTFLOAT_64BIT -#else -#error Unknown platform -#endif - - -#if ((defined(_WIN32) || defined(_WIN64)) && !defined(__clang__)) -#include -#endif - #ifdef FASTFLOAT_32BIT #if (!defined(_WIN32)) || defined(__MINGW32__) From abd3c2f81e076bbf3c3376655422da5629e12d35 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 09:45:36 +0000 Subject: [PATCH 21/25] powers of ten test: do not fail when pow() is broken - merely warn --- tests/basictest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/basictest.cpp b/tests/basictest.cpp index d0a7d48..2df1b60 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -192,7 +192,7 @@ static const double testing_power_of_ten[] = { TEST_CASE("powers_of_ten") { char buf[1024]; - CHECK_MESSAGE(1e-308 == std::pow(10, -308), "On your system, the pow function is busted. Sorry about that."); + WARN_MESSAGE(1e-308 == std::pow(10, -308), "On your system, the pow function is busted. Sorry about that."); bool is_pow_correct{1e-308 == std::pow(10,-308)}; // large negative values should be zero. int start_point = is_pow_correct ? -1000 : -307; From df6e785d5c416c8f1d3f9144918fb84b78b636c2 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 20 Nov 2020 10:01:38 +0000 Subject: [PATCH 22/25] travis: use a recent cmake --- .travis.yml | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6f1b5bf..134149a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,6 @@ language: cpp dist: bionic -addons: - snaps: - - name: cmake - confinement: classic - channel: 3.16/stable arch: - amd64 @@ -168,7 +163,37 @@ before_install: - eval "${COMPILER}" install: - - sudo apt-get -qq update + # make sure we have a recent cmake version. + # fastfloat itself does not require it, but the tests require >= 3.14. + + # these architectures have a snap: + # https://gitlab.kitware.com/cmake/cmake/-/issues/20122 + - sudo -E apt-get -y update + - sudo -E apt-get -y install snapd + - sudo -E snap install cmake --beta --classic + - export PATH=/snap/cmake/current/bin:$PATH + + # ALTERNATIVE: does not work on s390 and ppc64le + ## https://github.com/kahypar/kahypar/blob/master/.travis.yml + #- echo ${TRAVIS_OS_NAME} + #- | + # if [ "${TRAVIS_OS_NAME}" == "linux" ]; then + # pwd ; + # export DEPS_DIR="${HOME}/deps" ; + # mkdir -p ${DEPS_DIR} && cd ${DEPS_DIR} ; + # export CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0-Linux-x86_64.tar.gz"; + # mkdir cmake; + # travis_retry wget --no-check-certificate -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C cmake; + # export PATH=${DEPS_DIR}/cmake/bin:${PATH}; + # cd - ; + # pwd + # else + # brew install cmake || brew upgrade cmake; + # fi + + - echo ${PATH} + - which cmake + - cmake --version script: - mkdir build From ed6664d93e284e2adabcf23143c1b99dc817865e Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Sat, 21 Nov 2020 01:12:53 +0100 Subject: [PATCH 23/25] add bitness for s390 --- include/fast_float/float_common.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index 8791709..bcff7e1 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -6,11 +6,13 @@ #include #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) \ - || defined(__arm__) || defined(__MINGW32__)) + || defined(__arm__) \ + || defined(__MINGW32__)) #define FASTFLOAT_32BIT #elif (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \ || defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \ - || defined(__MINGW64__)) + || defined(__MINGW64__) \ + || defined(__s390x__)) #define FASTFLOAT_64BIT #else #error Unknown platform From 8ba7d2e850a80ef9971b6a8b0a3cd3d1c7f66a6f Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Sat, 21 Nov 2020 00:59:13 +0100 Subject: [PATCH 24/25] travis vs FetchContent_MakeAvailable(): give up on a recent cmake ... Avoid using the modern cmake function, and go back to an earlier approach. The ppc64le travis images was not dealing well with the attempt to hoist a modern cmake into it: https://travis-ci.com/github/lemire/fast_float/jobs/444278392 --- .travis.yml | 119 +++++++++++++++++++++++++------------------ CMakeLists.txt | 2 +- tests/CMakeLists.txt | 19 +++++-- 3 files changed, 85 insertions(+), 55 deletions(-) diff --git a/.travis.yml b/.travis.yml index 134149a..c8dc04a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,6 @@ language: cpp dist: bionic -arch: - - amd64 - - ppc64le - - s390x - cache: directories: - $HOME/.dep_cache @@ -15,9 +10,30 @@ env: global: - fastfloat_DEPENDENCY_CACHE_DIR=$HOME/.dep_cache + +# the ppc64le and s390x images use cmake 3.10, but fast_float requires 3.11. +# so we compile cmake from source in those images. +# - tried the kitware ppa but that is using 3.10 as well +# - tried also using snap to get a more recent version but that failed with +# udev errors. + matrix: include: - - os: linux + - arch: ppc64le + os: linux + env: + - CMAKE_SRC="https://github.com/Kitware/CMake/releases/download/v3.11.4/cmake-3.11.4.tar.gz" + + - arch: s390x + os: linux + env: + - CMAKE_SRC="https://github.com/Kitware/CMake/releases/download/v3.11.4/cmake-3.11.4.tar.gz" + + - arch: amd64 + os: linux + + - arch: amd64 + os: linux addons: apt: sources: @@ -28,7 +44,8 @@ matrix: - COMPILER="CC=gcc-8 && CXX=g++-8" compiler: gcc-8 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -39,7 +56,8 @@ matrix: - COMPILER="CC=gcc-9 && CXX=g++-9" compiler: gcc-9 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -50,7 +68,8 @@ matrix: - COMPILER="CC=gcc-10 && CXX=g++-10" compiler: gcc-10 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -62,7 +81,8 @@ matrix: - SANITIZE="on" compiler: gcc-10-sanitize - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -72,9 +92,10 @@ matrix: env: - COMPILER="CC=gcc-10 && CXX=g++-10" - STATIC="on" - compiler: gcc-10-static + acompiler: gcc-10-static - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -85,7 +106,8 @@ matrix: - COMPILER="CC=clang-6.0 && CXX=clang++-6.0" compiler: clang-6 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -96,7 +118,8 @@ matrix: - COMPILER="CC=clang-7 && CXX=clang++-7" compiler: clang-7 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -107,7 +130,8 @@ matrix: - COMPILER="CC=clang-8 && CXX=clang++-8" compiler: clang-8 - - os: linux + - arch: amd64 + os: linux addons: apt: sources: @@ -118,7 +142,8 @@ matrix: - COMPILER="CC=clang-9 && CXX=clang++-9" compiler: clang-9 - - os: linux + - arch: amd64 + os: linux addons: apt: packages: @@ -131,7 +156,8 @@ matrix: - COMPILER="CC=clang-10 && CXX=clang++-10" compiler: clang-10 - - os: linux + - arch: amd64 + os: linux addons: apt: packages: @@ -145,7 +171,8 @@ matrix: - STATIC="on" compiler: clang-10-static - - os: linux + - arch: amd64 + os: linux addons: apt: packages: @@ -163,42 +190,36 @@ before_install: - eval "${COMPILER}" install: - # make sure we have a recent cmake version. - # fastfloat itself does not require it, but the tests require >= 3.14. - - # these architectures have a snap: - # https://gitlab.kitware.com/cmake/cmake/-/issues/20122 - - sudo -E apt-get -y update - - sudo -E apt-get -y install snapd - - sudo -E snap install cmake --beta --classic - - export PATH=/snap/cmake/current/bin:$PATH - - # ALTERNATIVE: does not work on s390 and ppc64le - ## https://github.com/kahypar/kahypar/blob/master/.travis.yml - #- echo ${TRAVIS_OS_NAME} - #- | - # if [ "${TRAVIS_OS_NAME}" == "linux" ]; then - # pwd ; - # export DEPS_DIR="${HOME}/deps" ; - # mkdir -p ${DEPS_DIR} && cd ${DEPS_DIR} ; - # export CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0-Linux-x86_64.tar.gz"; - # mkdir cmake; - # travis_retry wget --no-check-certificate -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C cmake; - # export PATH=${DEPS_DIR}/cmake/bin:${PATH}; - # cd - ; - # pwd - # else - # brew install cmake || brew upgrade cmake; - # fi - + - | + if [ "$CMAKE_SRC" != "" ] ; then + set -x + set -e + sudo -E apt remove --purge cmake + sudo -E apt-get update + sudo -E apt-get install -y build-essential libssl-dev + mkdir cmake_src + pushd cmake_src + wget "$CMAKE_SRC" + tar xfz $(basename "$CMAKE_SRC") + pushd $(basename "$CMAKE_SRC" | sed "s:.tar.gz::") + ./bootstrap + make -j2 + sudo make install + popd + popd + set +x + fi - echo ${PATH} - which cmake - cmake --version + - which ${CC} + - ${CC} --version + - which ${CXX} + - ${CXX} --version script: - mkdir build - cd build - - cmake -DFASTFLOAT_TEST=ON .. - - make + - cmake -DFASTFLOAT_TEST=ON .. + - make -j2 - ctest --output-on-failure -R basictest - diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d10096..acc0901 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.9) project(fast_float VERSION 0.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 04129d0..3a4b724 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,13 +1,22 @@ -# FetchContent_MakeAvailable() was only introduced in 3.14 -# https://cmake.org/cmake/help/v3.14/release/3.14.html#modules -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +# FetchContent requires cmake >=3.11 +# see https://cmake.org/cmake/help/v3.11/module/FetchContent.html +cmake_minimum_required(VERSION 3.11 FATAL_ERROR) include(FetchContent) FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/onqtam/doctest.git - GIT_TAG master) -FetchContent_MakeAvailable(doctest) + GIT_TAG 2.4.1) + +# FetchContent_MakeAvailable() was only introduced in 3.14 +# https://cmake.org/cmake/help/v3.14/release/3.14.html#modules +# FetchContent_MakeAvailable(doctest) +FetchContent_GetProperties(doctest) +if(NOT doctest_POPULATED) + FetchContent_Populate(doctest) + add_subdirectory(${doctest_SOURCE_DIR} ${doctest_BINARY_DIR}) +endif() + function(fast_float_add_cpp_test TEST_NAME) add_executable(${TEST_NAME} ${TEST_NAME}.cpp) From 037136a96623dfe2b49a9781729705c766b12f2c Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Sat, 21 Nov 2020 19:01:26 +0000 Subject: [PATCH 25/25] fix: add bitness for ppc64le --- include/fast_float/float_common.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/fast_float/float_common.h b/include/fast_float/float_common.h index bcff7e1..6679c64 100644 --- a/include/fast_float/float_common.h +++ b/include/fast_float/float_common.h @@ -12,7 +12,8 @@ #elif (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \ || defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \ || defined(__MINGW64__) \ - || defined(__s390x__)) + || defined(__s390x__) \ + || (defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || defined(__PPC64LE__))) #define FASTFLOAT_64BIT #else #error Unknown platform