OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #ifndef V8_A64_UTILS_A64_H_ | |
29 #define V8_A64_UTILS_A64_H_ | |
30 | |
31 #include <cmath> | |
32 #include "v8.h" | |
33 #include "a64/constants-a64.h" | |
34 | |
35 #define REGISTER_CODE_LIST(R) \ | |
36 R(0) R(1) R(2) R(3) R(4) R(5) R(6) R(7) \ | |
37 R(8) R(9) R(10) R(11) R(12) R(13) R(14) R(15) \ | |
38 R(16) R(17) R(18) R(19) R(20) R(21) R(22) R(23) \ | |
39 R(24) R(25) R(26) R(27) R(28) R(29) R(30) R(31) | |
40 | |
41 namespace v8 { | |
42 namespace internal { | |
43 | |
44 // These are global assumptions in v8. | |
45 STATIC_ASSERT((static_cast<int32_t>(-1) >> 1) == -1); | |
46 STATIC_ASSERT((static_cast<uint32_t>(-1) >> 1) == 0x7FFFFFFF); | |
47 | |
48 // Floating point representation. | |
49 static inline uint32_t float_to_rawbits(float value) { | |
50 uint32_t bits = 0; | |
51 memcpy(&bits, &value, 4); | |
52 return bits; | |
53 } | |
54 | |
55 | |
56 static inline uint64_t double_to_rawbits(double value) { | |
57 uint64_t bits = 0; | |
58 memcpy(&bits, &value, 8); | |
59 return bits; | |
60 } | |
61 | |
62 | |
63 static inline float rawbits_to_float(uint32_t bits) { | |
64 float value = 0.0; | |
65 memcpy(&value, &bits, 4); | |
66 return value; | |
67 } | |
68 | |
69 | |
70 static inline double rawbits_to_double(uint64_t bits) { | |
71 double value = 0.0; | |
72 memcpy(&value, &bits, 8); | |
73 return value; | |
74 } | |
75 | |
76 | |
77 // Bit counting. | |
78 int CountLeadingZeros(uint64_t value, int width); | |
79 int CountLeadingSignBits(int64_t value, int width); | |
80 int CountTrailingZeros(uint64_t value, int width); | |
81 int CountSetBits(uint64_t value, int width); | |
82 int MaskToBit(uint64_t mask); | |
83 | |
84 | |
85 // NaN tests. | |
86 inline bool IsSignallingNaN(double num) { | |
87 uint64_t raw = double_to_rawbits(num); | |
88 if (std::isnan(num) && ((raw & kDQuietNanMask) == 0)) { | |
89 return true; | |
90 } | |
91 return false; | |
92 } | |
93 | |
94 | |
95 inline bool IsSignallingNaN(float num) { | |
96 uint32_t raw = float_to_rawbits(num); | |
97 if (std::isnan(num) && ((raw & kSQuietNanMask) == 0)) { | |
98 return true; | |
99 } | |
100 return false; | |
101 } | |
102 | |
103 | |
104 template <typename T> | |
105 inline bool IsQuietNaN(T num) { | |
106 return std::isnan(num) && !IsSignallingNaN(num); | |
107 } | |
108 | |
109 | |
110 // Convert the NaN in 'num' to a quiet NaN. | |
111 inline double ToQuietNaN(double num) { | |
112 ASSERT(isnan(num)); | |
113 return rawbits_to_double(double_to_rawbits(num) | kDQuietNanMask); | |
114 } | |
115 | |
116 | |
117 inline float ToQuietNaN(float num) { | |
118 ASSERT(isnan(num)); | |
119 return rawbits_to_float(float_to_rawbits(num) | kSQuietNanMask); | |
120 } | |
121 | |
122 | |
123 // Fused multiply-add. | |
124 inline double FusedMultiplyAdd(double op1, double op2, double a) { | |
125 return fma(op1, op2, a); | |
126 } | |
127 | |
128 | |
129 inline float FusedMultiplyAdd(float op1, float op2, float a) { | |
130 return fmaf(op1, op2, a); | |
131 } | |
132 | |
133 } } // namespace v8::internal | |
134 | |
135 #endif // V8_A64_UTILS_A64_H_ | |
OLD | NEW |