-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathieee754_notes.cc
More file actions
71 lines (62 loc) · 2.42 KB
/
Copy pathieee754_notes.cc
File metadata and controls
71 lines (62 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "gtest/gtest.h"
#include <cinttypes>
#include <cstring>
#include <random>
namespace {
template <typename To, typename From> To transmute(From x) {
static_assert(sizeof(To) == sizeof(From), "size mismatch");
To y;
memcpy(&y, &x, sizeof(y));
return y;
}
// NB. Right shift of negative values is technically
// implementation-defined in C but practically is arithmetic shift:
uint64_t asr(uint64_t x, uint32_t shift) {
return static_cast<int64_t>(x) >> shift;
}
TEST(IEEE754Notes, Transmute) {
ASSERT_EQ(0x8000000000000000, transmute<uint64_t>(-0.0));
ASSERT_EQ(0x3ff0000000000000, transmute<uint64_t>(1.0));
}
// On x86, clang uses a trick to convert uint64_t to double:
TEST(IEEE754Notes, ClangConvert) {
const int ITERATIONS = 65536;
std::random_device urandom;
std::default_random_engine rng(urandom());
std::uniform_int_distribution<uint64_t> random_bits;
for (int iteration = 0; iteration < ITERATIONS; ++iteration) {
uint64_t value = random_bits(rng);
double expected = static_cast<double>(value);
uint64_t low32 = value & 0xffffffff;
uint64_t high32 = value >> 32;
uint64_t c1 = transmute<uint64_t>(0x1p52);
uint64_t c2 = transmute<uint64_t>(0x1p84);
// low and high are computed with punpckldq, then a single subpd:
double low = transmute<double>(c1 | low32) - 0x1p52;
double high = transmute<double>(c2 | high32) - 0x1p84;
double observed = high + low;
ASSERT_EQ(expected, observed);
}
}
// We can impose a total order on doubles that refines the comparison
// order (note that all comparisons with NaN are false):
int64_t double_key(double d) {
uint64_t x = transmute<uint64_t>(d);
return x ^ (asr(x, 63) >> 1);
}
TEST(IEEE754Notes, RefinedOrder) {
const int ITERATIONS = 65536;
std::random_device urandom;
std::default_random_engine rng(urandom());
std::uniform_int_distribution<uint64_t> random_bits;
for (int iteration = 0; iteration < ITERATIONS; ++iteration) {
uint64_t value1 = random_bits(rng);
uint64_t value2 = random_bits(rng);
ASSERT_TRUE(!(value1 < value2) || double_key(value1) < double_key(value2));
// We can also use uint64_t keys:
uint64_t key_value1 = double_key(value1) ^ 0x8000000000000000;
uint64_t key_value2 = double_key(value2) ^ 0x8000000000000000;
ASSERT_TRUE(!(value1 < value2) || key_value1 < key_value2);
}
}
} // namespace