• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <climits>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <limits>
20 #include <random>
21 #include <cmath>
22 
23 #include "libpandabase/utils/bit_utils.h"
24 #include "macros.h"
25 #include "plugins/ets/runtime/ets_coroutine.h"
26 #include "plugins/ets/runtime/ets_vm.h"
27 
28 namespace ark::ets::intrinsics {
29 
30 namespace {
31 constexpr uint64_t SIGN_MASK = 0x8000'0000'0000'0000;
32 constexpr uint64_t EXPONENT_MASK = 0x7FF0'0000'0000'0000;
33 constexpr uint64_t SIGNIFICANT_MASK = 0x000F'FFFF'FFFF'FFFF;
34 constexpr uint64_t WORD_MASK = 0xFFFF'FFFFUL;
35 constexpr uint64_t HIDDEN_BIT = 0x0010'0000'0000'0000;
36 constexpr uint64_t PHYSICAL_SIGNIFICAND_SIZE = 52;
37 constexpr int MAX_EXPONENT = 31;
38 constexpr int SIGNIFICAND_SIZE = 53;
39 constexpr int EXPONENT_BIAS = 0x3FF + PHYSICAL_SIGNIFICAND_SIZE;
40 constexpr int DENORMAL_EXPONENT = -EXPONENT_BIAS + 1;
41 
IsDenormal(uint64_t d64)42 bool IsDenormal(uint64_t d64)
43 {
44     return (d64 & EXPONENT_MASK) == 0;
45 }
46 
Sign(uint64_t d64)47 int Sign(uint64_t d64)
48 {
49     return (d64 & SIGN_MASK) == 0 ? 1 : -1;
50 }
51 
Exponent(uint64_t d64)52 int Exponent(uint64_t d64)
53 {
54     if (IsDenormal(d64)) {
55         return DENORMAL_EXPONENT;
56     }
57     int biasedE = static_cast<int>((d64 & EXPONENT_MASK) >> PHYSICAL_SIGNIFICAND_SIZE);
58     return biasedE - EXPONENT_BIAS;
59 }
60 
Significand(uint64_t d64)61 uint64_t Significand(uint64_t d64)
62 {
63     uint64_t significand = d64 & SIGNIFICANT_MASK;
64     if (!IsDenormal(d64)) {
65         return significand + HIDDEN_BIT;
66     }
67     return significand;
68 }
69 
ToInt32(double x)70 int32_t ToInt32(double x)
71 {
72     if ((std::isfinite(x)) && (x <= INT_MAX) && (x >= INT_MIN)) {
73         return static_cast<int32_t>(x);
74     }
75 
76     uint64_t d64;
77     // NOTE(kirill-mitkin): can be __builtin_bit_cast() if defined
78     if (memcpy_s(&d64, sizeof(uint64_t), &x, sizeof(uint64_t)) != 0) {
79         UNREACHABLE();
80     }
81 
82     int exponent = Exponent(d64);
83     uint64_t significand = Significand(d64);
84     uint64_t bits;
85     if (exponent < 0) {
86         if (exponent <= SIGNIFICAND_SIZE) {
87             return 0;
88         }
89         bits = significand >> static_cast<uint64_t>(-exponent);
90     } else {
91         if (exponent > MAX_EXPONENT) {
92             return 0;
93         }
94         bits = (significand << static_cast<uint64_t>(exponent)) & WORD_MASK;
95     }
96     return static_cast<int32_t>(Sign(d64) * static_cast<int64_t>(bits));
97 }
98 
ToUint32(double x)99 uint32_t ToUint32(double x)
100 {
101     return static_cast<uint32_t>(ToInt32(x));
102 }
103 }  // namespace
104 
StdMathRandom()105 extern "C" double StdMathRandom()
106 {
107     std::uniform_real_distribution<double> urd(0.0, 1.0);
108     return urd(EtsCoroutine::GetCurrent()->GetPandaVM()->GetRandomEngine());
109 }
110 
StdMathAcos(double val)111 extern "C" double StdMathAcos(double val)
112 {
113     return std::acos(val);
114 }
115 
StdMathAcosh(double val)116 extern "C" double StdMathAcosh(double val)
117 {
118     return std::acosh(val);
119 }
120 
StdMathAsin(double val)121 extern "C" double StdMathAsin(double val)
122 {
123     return std::asin(val);
124 }
125 
StdMathAsinh(double val)126 extern "C" double StdMathAsinh(double val)
127 {
128     return std::asinh(val);
129 }
130 
StdMathAtan2(double val1,double val2)131 extern "C" double StdMathAtan2(double val1, double val2)
132 {
133     return std::atan2(val1, val2);
134 }
135 
StdMathAtanh(double val)136 extern "C" double StdMathAtanh(double val)
137 {
138     return std::atanh(val);
139 }
140 
StdMathAtan(double val)141 extern "C" double StdMathAtan(double val)
142 {
143     return std::atan(val);
144 }
145 
StdMathSinh(double val)146 extern "C" double StdMathSinh(double val)
147 {
148     return std::sinh(val);
149 }
150 
StdMathCosh(double val)151 extern "C" double StdMathCosh(double val)
152 {
153     return std::cosh(val);
154 }
155 
StdMathFloor(double val)156 extern "C" double StdMathFloor(double val)
157 {
158     return std::floor(val);
159 }
160 
StdMathRound(double val)161 extern "C" double StdMathRound(double val)
162 {
163     return std::round(val);
164 }
165 
StdMathTrunc(double val)166 extern "C" double StdMathTrunc(double val)
167 {
168     return std::trunc(val);
169 }
170 
StdMathCbrt(double val)171 extern "C" double StdMathCbrt(double val)
172 {
173     return std::cbrt(val);
174 }
175 
StdMathTan(double val)176 extern "C" double StdMathTan(double val)
177 {
178     return std::tan(val);
179 }
180 
StdMathTanh(double val)181 extern "C" double StdMathTanh(double val)
182 {
183     return std::tanh(val);
184 }
185 
StdMathExp(double val)186 extern "C" double StdMathExp(double val)
187 {
188     return std::exp(val);
189 }
190 
StdMathLog10(double val)191 extern "C" double StdMathLog10(double val)
192 {
193     return std::log10(val);
194 }
195 
StdMathExpm1(double val)196 extern "C" double StdMathExpm1(double val)
197 {
198     return std::expm1(val);
199 }
200 
StdMathCeil(double val)201 extern "C" double StdMathCeil(double val)
202 {
203     return std::ceil(val);
204 }
205 
StdMathClz64(int64_t val)206 extern "C" int32_t StdMathClz64(int64_t val)
207 {
208     if (val != 0) {
209         return Clz(static_cast<uint64_t>(val));
210     }
211     return std::numeric_limits<uint64_t>::digits;
212 }
213 
StdMathClz32(int32_t val)214 extern "C" int32_t StdMathClz32(int32_t val)
215 {
216     if (val != 0) {
217         return Clz(static_cast<uint32_t>(val));
218     }
219     return std::numeric_limits<uint32_t>::digits;
220 }
221 
StdMathClz32Double(double val)222 extern "C" double StdMathClz32Double(double val)
223 {
224     auto intValue = ToUint32(val);
225     if (intValue != 0) {
226         return static_cast<double>(Clz(intValue));
227     }
228     return std::numeric_limits<uint32_t>::digits;
229 }
230 
StdMathLog(double val)231 extern "C" double StdMathLog(double val)
232 {
233     return std::log(val);
234 }
235 
StdMathRem(double val,double val2)236 extern "C" double StdMathRem(double val, double val2)
237 {
238     return std::remainder(val, val2);
239 }
240 
StdMathMod(double val,double val2)241 extern "C" double StdMathMod(double val, double val2)
242 {
243     return std::fmod(val, val2);
244 }
245 
StdMathSignbit(double val)246 extern "C" bool StdMathSignbit(double val)
247 {
248     return std::signbit(val);
249 }
250 
StdMathImul(double val,double val2)251 extern "C" int StdMathImul(double val, double val2)
252 {
253     if (!std::isfinite(val) || !std::isfinite(val2)) {
254         return 0;
255     }
256     return static_cast<int32_t>(static_cast<int64_t>(val) * static_cast<int64_t>(val2));
257 }
258 
StdMathFround(double val)259 extern "C" double StdMathFround(double val)
260 {
261     if (std::isnan(val)) {
262         return std::numeric_limits<float>::quiet_NaN();
263     }
264 
265     return static_cast<float>(val);
266 }
StdMathHypot(double val1,double val2)267 extern "C" double StdMathHypot(double val1, double val2)
268 {
269     return std::hypot(val1, val2);
270 }
271 }  // namespace ark::ets::intrinsics
272