Debian Codegraph @

js/src/vm/Uint8Clamped.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_Uint8Clamped_h
8 #define vm_Uint8Clamped_h
9
10 #include <algorithm>
11 #include <limits>
12 #include <limits.h>
13 #include <stdint.h>
14 #include <type_traits>
15
16 namespace js {
17
18 extern uint32_t ClampDoubleToUint8(const double x);
19
20 class uint8_clamped final {
21 uint8_t val;
22
23 template <typename IntT>
24 static constexpr uint8_t ClampIntToUint8(IntT x) {
25 static_assert(std::is_integral_v<IntT>);
26
27 if constexpr (std::numeric_limits<IntT>::max() < 255) {
28 return std::clamp<IntT>(x, 0, std::numeric_limits<IntT>::max());
29 } else {
30 return std::clamp<IntT>(x, 0, 255);
31 }
32 }
33
34 public:
35 // The default constructor can be 'constexpr' when we switch to C++20.
36 //
37 // C++17 requires explicit initialization of all members when using a
38 // 'constexpr' default constructor. That means `val` needs to be initialized
39 // through a member initializer. But adding a member initializer makes the
40 // class no longer trivial, which breaks memcpy/memset optimizations.
41
42 /* constexpr */ uint8_clamped() = default;
43 constexpr uint8_clamped(const uint8_clamped&) = default;
44
45 constexpr explicit uint8_clamped(uint8_t x) : val(x) {}
46 constexpr explicit uint8_clamped(uint16_t x) : val(ClampIntToUint8(x)) {}
47 constexpr explicit uint8_clamped(uint32_t x) : val(ClampIntToUint8(x)) {}
48 constexpr explicit uint8_clamped(uint64_t x) : val(ClampIntToUint8(x)) {}
49 constexpr explicit uint8_clamped(int8_t x) : val(ClampIntToUint8(x)) {}
50 constexpr explicit uint8_clamped(int16_t x) : val(ClampIntToUint8(x)) {}
51 constexpr explicit uint8_clamped(int32_t x) : val(ClampIntToUint8(x)) {}
52 constexpr explicit uint8_clamped(int64_t x) : val(ClampIntToUint8(x)) {}
53 explicit uint8_clamped(double x) : val(uint8_t(ClampDoubleToUint8(x))) {}
54
55 constexpr uint8_clamped& operator=(const uint8_clamped&) = default;
56
57 // Invoke constructors for the assignment helpers.
58
59 constexpr uint8_clamped& operator=(uint8_t x) {
60 *this = uint8_clamped{x};
61 return *this;
62 }
63
64 constexpr uint8_clamped& operator=(uint16_t x) {
65 *this = uint8_clamped{x};
66 return *this;
67 }
68
69 constexpr uint8_clamped& operator=(uint32_t x) {
70 *this = uint8_clamped{x};
71 return *this;
72 }
73
74 constexpr uint8_clamped& operator=(uint64_t x) {
75 *this = uint8_clamped{x};
76 return *this;
77 }
78
79 constexpr uint8_clamped& operator=(int8_t x) {
80 *this = uint8_clamped{x};
81 return *this;
82 }
83
84 constexpr uint8_clamped& operator=(int16_t x) {
85 *this = uint8_clamped{x};
86 return *this;
87 }
88
89 constexpr uint8_clamped& operator=(int32_t x) {
90 *this = uint8_clamped{x};
91 return *this;
92 }
93
94 constexpr uint8_clamped& operator=(int64_t x) {
95 *this = uint8_clamped{x};
96 return *this;
97 }
98
99 uint8_clamped& operator=(const double x) {
100 *this = uint8_clamped{x};
101 return *this;
102 }
103
104 constexpr operator uint8_t() const { return val; }
105 };
106
107 static_assert(sizeof(uint8_clamped) == 1,
108 "uint8_clamped must be layout-compatible with uint8_t");
109
110 static_assert(std::is_trivial_v<uint8_clamped>,
111 "uint8_clamped must be trivial to be eligible for memcpy/memset "
112 "optimizations");
113
114 } // namespace js
115
116 template <>
117 class std::numeric_limits<js::uint8_clamped> {
118 public:
119 static constexpr bool is_specialized = true;
120 static constexpr bool is_signed = false;
121 static constexpr bool is_integer = true;
122 static constexpr bool is_exact = true;
123 static constexpr bool has_infinity = false;
124 static constexpr bool has_quiet_NaN = false;
125 static constexpr bool has_signaling_NaN = false;
126 static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
127 static constexpr bool has_denorm_loss = false;
128 static constexpr std::float_round_style round_style = std::round_toward_zero;
129 static constexpr bool is_iec559 = false;
130 static constexpr bool is_bounded = true;
131 static constexpr bool is_modulo = false;
132 static constexpr int digits = CHAR_BIT;
133 static constexpr int digits10 = int(digits * /* std::log10(2) */ 0.30102999);
134 static constexpr int max_digits10 = 0;
135 static constexpr int radix = 2;
136 static constexpr int min_exponent = 0;
137 static constexpr int min_exponent10 = 0;
138 static constexpr int max_exponent = 0;
139 static constexpr int max_exponent10 = 0;
140 static constexpr bool traps = true;
141 static constexpr bool tinyness_before = false;
142
143 static constexpr auto min() noexcept { return js::uint8_clamped{0}; }
144 static constexpr auto lowest() noexcept { return min(); }
145 static constexpr auto max() noexcept { return js::uint8_clamped{255}; }
146 static constexpr auto epsilon() noexcept { return js::uint8_clamped{0}; }
147 static constexpr auto round_error() noexcept { return js::uint8_clamped{0}; }
148 static constexpr auto infinity() noexcept { return js::uint8_clamped{0}; }
149 static constexpr auto quiet_NaN() noexcept { return js::uint8_clamped{0}; }
150 static constexpr auto signaling_NaN() noexcept {
151 return js::uint8_clamped{0};
152 }
153 static constexpr auto denorm_min() noexcept { return js::uint8_clamped{0}; }
154 };
155
156 #endif // vm_Uint8Clamped_h