v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
boxed-float.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_UTILS_BOXED_FLOAT_H_
6#define V8_UTILS_BOXED_FLOAT_H_
7
8#include <cmath>
9
10#include "src/base/hashing.h"
11#include "src/base/macros.h"
13#include "src/common/globals.h"
14
15namespace v8 {
16namespace internal {
17
18// TODO(ahaas): Make these classes with the one in double.h
19
20// Safety wrapper for a 32-bit floating-point value to make sure we don't lose
21// the exact bit pattern during deoptimization when passing this value.
22class Float32 {
23 public:
24 Float32() = default;
25
26 // This constructor does not guarantee that bit pattern of the input value
27 // is preserved if the input is a NaN.
28 explicit Float32(float value)
29 : bit_pattern_(base::bit_cast<uint32_t>(value)) {
30 // Check that the provided value is not a NaN, because the bit pattern of a
31 // NaN may be changed by a base::bit_cast, e.g. for signalling NaNs on
32 // ia32.
33 DCHECK(!std::isnan(value));
34 }
35
36 uint32_t get_bits() const { return bit_pattern_; }
37
39
40 bool is_nan() const {
41 // Even though {get_scalar()} might flip the quiet NaN bit, it's ok here,
42 // because this does not change the is_nan property.
43 return std::isnan(get_scalar());
44 }
45
46 // Return a pointer to the field storing the bit pattern. Used in code
47 // generation tests to store generated values there directly.
48 uint32_t* get_bits_address() { return &bit_pattern_; }
49
50 static constexpr Float32 FromBits(uint32_t bits) { return Float32(bits); }
51
52 private:
53 uint32_t bit_pattern_ = 0;
54
55 explicit constexpr Float32(uint32_t bit_pattern)
56 : bit_pattern_(bit_pattern) {}
57};
58
60
61// Safety wrapper for a 64-bit floating-point value to make sure we don't lose
62// the exact bit pattern during deoptimization when passing this value.
63// TODO(ahaas): Unify this class with Double in double.h
64class Float64 {
65 public:
66 Float64() = default;
67
68 // This constructor does not guarantee that bit pattern of the input value
69 // is preserved if the input is a NaN.
70 explicit Float64(double value)
71 : bit_pattern_(base::bit_cast<uint64_t>(value)) {
72 // Check that the provided value is not a NaN, because the bit pattern of a
73 // NaN may be changed by a base::bit_cast, e.g. for signalling NaNs on
74 // ia32.
75 DCHECK(!std::isnan(value));
76 }
77
78 explicit Float64(base::Double value) : bit_pattern_(value.AsUint64()) {}
79
80 uint64_t get_bits() const { return bit_pattern_; }
82 bool is_hole_nan() const { return bit_pattern_ == kHoleNanInt64; }
83 bool is_nan() const {
84 // Even though {get_scalar()} might flip the quiet NaN bit, it's ok here,
85 // because this does not change the is_nan property.
86 return std::isnan(get_scalar());
87 }
88
89 // Return a pointer to the field storing the bit pattern. Used in code
90 // generation tests to store generated values there directly.
91 uint64_t* get_bits_address() { return &bit_pattern_; }
92
93 static constexpr Float64 FromBits(uint64_t bits) { return Float64(bits); }
94
95 // Unlike doubles, equality is defined as equally behaving as far as the
96 // optimizers are concerned. I.e., two NaN's are equal as long as they are
97 // both the hole nor not.
98 bool operator==(const Float64& other) const {
99 if (is_nan() && other.is_nan()) {
100 return is_hole_nan() == other.is_hole_nan();
101 }
102 return get_scalar() == other.get_scalar();
103 }
104
105 friend size_t hash_value(internal::Float64 f64) { return f64.bit_pattern_; }
106
107 private:
108 uint64_t bit_pattern_ = 0;
109
110 explicit constexpr Float64(uint64_t bit_pattern)
111 : bit_pattern_(bit_pattern) {}
112};
113
115
116} // namespace internal
117
118namespace base {
119
120inline size_t hash_value(const i::Float64& f64) {
121 return f64.is_nan() ? hash_value(f64.is_hole_nan())
122 : hash_value(f64.get_bits());
123}
124
125} // namespace base
126} // namespace v8
127
128#endif // V8_UTILS_BOXED_FLOAT_H_
constexpr Float32(uint32_t bit_pattern)
Definition boxed-float.h:55
uint32_t * get_bits_address()
Definition boxed-float.h:48
bool is_nan() const
Definition boxed-float.h:40
Float32(float value)
Definition boxed-float.h:28
float get_scalar() const
Definition boxed-float.h:38
static constexpr Float32 FromBits(uint32_t bits)
Definition boxed-float.h:50
uint32_t get_bits() const
Definition boxed-float.h:36
bool operator==(const Float64 &other) const
Definition boxed-float.h:98
bool is_hole_nan() const
Definition boxed-float.h:82
Float64(base::Double value)
Definition boxed-float.h:78
friend size_t hash_value(internal::Float64 f64)
uint64_t * get_bits_address()
Definition boxed-float.h:91
bool is_nan() const
Definition boxed-float.h:83
Float64(double value)
Definition boxed-float.h:70
constexpr Float64(uint64_t bit_pattern)
uint64_t get_bits() const
Definition boxed-float.h:80
static constexpr Float64 FromBits(uint64_t bits)
Definition boxed-float.h:93
double get_scalar() const
Definition boxed-float.h:81
V8_INLINE size_t hash_value(unsigned int v)
Definition hashing.h:205
V8_INLINE Dest bit_cast(Source const &source)
Definition macros.h:95
constexpr uint64_t kHoleNanInt64
Definition globals.h:1960
#define DCHECK(condition)
Definition logging.h:482
#define ASSERT_TRIVIALLY_COPYABLE(T)
Definition macros.h:267