-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2020 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <gtest/gtest.h> | ||
#include <test/utils/floating_point_utils.hpp> | ||
|
||
using namespace fizzy::test; | ||
|
||
TEST(floating_point_utils, double_as_uint64) | ||
{ | ||
using Limits = std::numeric_limits<double>; | ||
|
||
EXPECT_EQ(as_uint64(0.0), 0x0000000000000000); | ||
EXPECT_EQ(as_uint64(-0.0), 0x8000000000000000); | ||
EXPECT_EQ(as_uint64(Limits::infinity()), 0x7FF'0000000000000); | ||
EXPECT_EQ(as_uint64(-Limits::infinity()), 0xFFF'0000000000000); | ||
EXPECT_EQ(as_uint64(Limits::max()), 0x7FE'FFFFFFFFFFFFF); | ||
EXPECT_EQ(as_uint64(-Limits::max()), 0xFFE'FFFFFFFFFFFFF); | ||
EXPECT_EQ(as_uint64(Limits::min()), 0x001'0000000000000); | ||
EXPECT_EQ(as_uint64(-Limits::min()), 0x801'0000000000000); | ||
EXPECT_EQ(as_uint64(Limits::denorm_min()), 0x000'0000000000001); | ||
EXPECT_EQ(as_uint64(-Limits::denorm_min()), 0x800'0000000000001); | ||
EXPECT_EQ(as_uint64(1.0), 0x3FF'0000000000000); | ||
EXPECT_EQ(as_uint64(-1.0), 0xBFF'0000000000000); | ||
EXPECT_EQ(as_uint64(std::nextafter(1.0, 0.0)), 0x3FE'FFFFFFFFFFFFF); | ||
EXPECT_EQ(as_uint64(std::nextafter(-1.0, 0.0)), 0xBFE'FFFFFFFFFFFFF); | ||
EXPECT_EQ(as_uint64(Limits::quiet_NaN()), 0x7FF'8000000000000); | ||
EXPECT_EQ(as_uint64(-Limits::quiet_NaN()), 0xFFF'8000000000000); | ||
} | ||
|
||
TEST(floating_point_utils, float_as_uint64) | ||
{ | ||
using Limits = std::numeric_limits<float>; | ||
|
||
EXPECT_EQ(as_uint64(0.0f), 0x00000000); | ||
EXPECT_EQ(as_uint64(-0.0f), 0x80000000); | ||
EXPECT_EQ(as_uint64(Limits::infinity()), 0x7F800000); | ||
EXPECT_EQ(as_uint64(-Limits::infinity()), 0xFF800000); | ||
EXPECT_EQ(as_uint64(Limits::max()), 0x7F7FFFFF); | ||
EXPECT_EQ(as_uint64(-Limits::max()), 0xFF7FFFFF); | ||
EXPECT_EQ(as_uint64(Limits::min()), 0x00800000); | ||
EXPECT_EQ(as_uint64(-Limits::min()), 0x80800000); | ||
EXPECT_EQ(as_uint64(Limits::denorm_min()), 0x00000001); | ||
EXPECT_EQ(as_uint64(-Limits::denorm_min()), 0x80000001); | ||
EXPECT_EQ(as_uint64(1.0f), 0x3F800000); | ||
EXPECT_EQ(as_uint64(-1.0f), 0xBF800000); | ||
EXPECT_EQ(as_uint64(std::nextafter(1.0f, 0.0f)), 0x3F7FFFFF); | ||
EXPECT_EQ(as_uint64(std::nextafter(-1.0f, 0.0f)), 0xBF7FFFFF); | ||
EXPECT_EQ(as_uint64(Limits::quiet_NaN()), 0x7FC00000); | ||
EXPECT_EQ(as_uint64(-Limits::quiet_NaN()), 0xFFC00000); | ||
} | ||
|
||
TEST(floating_point_utils, double_nan_payload) | ||
{ | ||
using Limits = std::numeric_limits<double>; | ||
constexpr auto qnan = Limits::quiet_NaN(); | ||
|
||
EXPECT_EQ(get_nan_payload(0.0), 0); | ||
EXPECT_EQ(get_nan_payload(Limits::signaling_NaN()), 0x4000000000000); | ||
EXPECT_EQ(get_nan_payload(qnan), 0x8000000000000); // Wasm canonical nan. | ||
EXPECT_EQ(get_nan_payload(qnan + 1.0), 0x8000000000000); | ||
EXPECT_EQ(get_nan_payload(Limits::infinity() - Limits::infinity()), 0x8000000000000); | ||
EXPECT_EQ(get_nan_payload(0.0 / 0.0), 0x8000000000000); | ||
} | ||
|
||
TEST(floating_point_utils, float_nan_payload) | ||
{ | ||
using Limits = std::numeric_limits<float>; | ||
constexpr auto qnan = Limits::quiet_NaN(); | ||
|
||
EXPECT_EQ(get_nan_payload(0.0f), 0); | ||
EXPECT_EQ(get_nan_payload(Limits::signaling_NaN()), 0x200000); | ||
EXPECT_EQ(get_nan_payload(qnan), 0x400000); // Wasm canonical nan. | ||
EXPECT_EQ(get_nan_payload(qnan + 1.0f), 0x400000); | ||
EXPECT_EQ(get_nan_payload(Limits::infinity() - Limits::infinity()), 0x400000); | ||
EXPECT_EQ(get_nan_payload(0.0f / 0.0f), 0x400000); | ||
} | ||
|
||
TEST(floating_point_utils, double_nan) | ||
{ | ||
EXPECT_TRUE(std::isnan(nan<double>())); | ||
EXPECT_TRUE(std::isnan(nan<double>(1))); | ||
EXPECT_TRUE(std::isnan(nan<double>(0xdeadbeef))); | ||
EXPECT_TRUE(std::isnan(nan<double>(0xdeadbeefbeeef))); | ||
EXPECT_FALSE(std::isnan(nan<double>(0))); | ||
|
||
EXPECT_EQ(get_nan_payload(nan<double>()), FPUtils<double>::CanonicalNaNPayload); | ||
|
||
EXPECT_EQ(as_uint64(nan<double>()), 0x7FF'8000000000000); | ||
EXPECT_EQ(as_uint64(nan<double>(0xdeadbeef)), 0x7FF'00000deadbeef); | ||
EXPECT_EQ(as_uint64(nan<double>(0xdeadbeefbeeef)), 0x7FF'deadbeefbeeef); | ||
} | ||
|
||
TEST(floating_point_utils, float_nan) | ||
{ | ||
EXPECT_TRUE(std::isnan(nan<float>())); | ||
EXPECT_TRUE(std::isnan(nan<float>(1))); | ||
EXPECT_TRUE(std::isnan(nan<float>(0x7fffff))); | ||
EXPECT_TRUE(std::isnan(nan<float>(0x400001))); | ||
EXPECT_FALSE(std::isnan(nan<float>(0))); | ||
|
||
EXPECT_EQ(get_nan_payload(nan<float>()), FPUtils<float>::CanonicalNaNPayload); | ||
|
||
EXPECT_EQ(as_uint64(nan<float>()), 0x7FC00000); | ||
EXPECT_EQ(as_uint64(nan<float>(0x7fffff)), 0x7FFFFFFF); | ||
EXPECT_EQ(as_uint64(nan<float>(0x400001)), 0x7FC00001); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2020 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <cstdint> | ||
#include <limits> | ||
#include <type_traits> | ||
|
||
namespace fizzy::test | ||
{ | ||
template <typename T, typename = std::enable_if<std::is_floating_point_v<T>>> | ||
struct FPUtils | ||
{ | ||
using Limits = std::numeric_limits<T>; | ||
|
||
static constexpr auto NumMantissaBits = Limits::digits - 1; | ||
|
||
static constexpr auto CanonicalNaNPayload = uint64_t{1} << (NumMantissaBits - 1); | ||
}; | ||
|
||
template <typename T, typename = std::enable_if<std::is_floating_point_v<T>>> | ||
uint64_t as_uint64(T value) noexcept | ||
{ | ||
constexpr auto num_bytes = sizeof(T); | ||
uint8_t bytes[num_bytes]; | ||
__builtin_memcpy(bytes, &value, num_bytes); | ||
|
||
uint64_t u = 0; | ||
for (size_t i = 0; i < num_bytes; ++i) | ||
u |= uint64_t{bytes[i]} << (i * 8); // little-endian | ||
return u; | ||
} | ||
|
||
template <typename T, typename = std::enable_if<std::is_floating_point_v<T>>> | ||
T from_uint64(uint64_t value) noexcept | ||
{ | ||
constexpr auto num_bytes = sizeof(T); | ||
uint8_t bytes[num_bytes]; | ||
|
||
for (size_t i = 0; i < num_bytes; ++i) | ||
bytes[i] = static_cast<uint8_t>(value >> (i * 8)); // little-endian | ||
|
||
T result; | ||
__builtin_memcpy(&result, bytes, num_bytes); | ||
return result; | ||
} | ||
|
||
template <typename T, typename = std::enable_if<std::is_floating_point_v<T>>> | ||
uint64_t get_nan_payload(T value) noexcept | ||
{ | ||
if (!std::isnan(value)) | ||
return 0; // NaN payload is never 0. | ||
|
||
constexpr auto mask = (uint64_t{1} << FPUtils<T>::NumMantissaBits) - 1; | ||
return as_uint64(value) & mask; | ||
} | ||
|
||
template <typename T, typename = std::enable_if<std::is_floating_point_v<T>>> | ||
T nan(uint64_t payload = FPUtils<T>::CanonicalNaNPayload) noexcept | ||
{ | ||
using Utils = FPUtils<T>; | ||
const auto nan_exponent = (uint64_t{Utils::Limits::max_exponent} << 1) - 1; | ||
return from_uint64<T>(((nan_exponent << Utils::NumMantissaBits)) | payload); | ||
} | ||
} // namespace fizzy::test |