• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 <cstdlib>
17 #include <limits>
18 #include <random>
19 #include <cmath>
20 
21 #include "libpandabase/utils/bit_utils.h"
22 #include "plugins/ets/runtime/ets_coroutine.h"
23 #include "plugins/ets/runtime/ets_vm.h"
24 
25 namespace panda::ets::intrinsics {
26 
StdMathRandom()27 extern "C" double StdMathRandom()
28 {
29     std::uniform_real_distribution<double> urd(0.0, 1.0);
30     return urd(EtsCoroutine::GetCurrent()->GetPandaVM()->GetRandomEngine());
31 }
32 
StdMathAcos(double val)33 extern "C" double StdMathAcos(double val)
34 {
35     return std::acos(val);
36 }
37 
StdMathAcosh(double val)38 extern "C" double StdMathAcosh(double val)
39 {
40     return std::acosh(val);
41 }
42 
StdMathAsin(double val)43 extern "C" double StdMathAsin(double val)
44 {
45     return std::asin(val);
46 }
47 
StdMathAsinh(double val)48 extern "C" double StdMathAsinh(double val)
49 {
50     return std::asinh(val);
51 }
52 
StdMathAtan2(double val1,double val2)53 extern "C" double StdMathAtan2(double val1, double val2)
54 {
55     return std::atan2(val1, val2);
56 }
57 
StdMathAtanh(double val)58 extern "C" double StdMathAtanh(double val)
59 {
60     return std::atanh(val);
61 }
62 
StdMathAtan(double val)63 extern "C" double StdMathAtan(double val)
64 {
65     return std::atan(val);
66 }
67 
StdMathSinh(double val)68 extern "C" double StdMathSinh(double val)
69 {
70     return std::sinh(val);
71 }
72 
StdMathCosh(double val)73 extern "C" double StdMathCosh(double val)
74 {
75     return std::cosh(val);
76 }
77 
StdMathFloor(double val)78 extern "C" double StdMathFloor(double val)
79 {
80     return std::floor(val);
81 }
82 
StdMathRound(double val)83 extern "C" double StdMathRound(double val)
84 {
85     return std::round(val);
86 }
87 
StdMathTrunc(double val)88 extern "C" double StdMathTrunc(double val)
89 {
90     return std::trunc(val);
91 }
92 
StdMathCeil(double val)93 extern "C" double StdMathCeil(double val)
94 {
95     return std::ceil(val);
96 }
97 
StdMathClz64(int64_t val)98 extern "C" int32_t StdMathClz64(int64_t val)
99 {
100     if (val != 0) {
101         return Clz(static_cast<uint64_t>(val));
102     }
103     return std::numeric_limits<uint64_t>::digits;
104 }
105 
StdMathClz32(int32_t val)106 extern "C" int32_t StdMathClz32(int32_t val)
107 {
108     if (val != 0) {
109         return Clz(static_cast<uint32_t>(val));
110     }
111     return std::numeric_limits<uint32_t>::digits;
112 }
113 
StdMathLog(double val)114 extern "C" double StdMathLog(double val)
115 {
116     return std::log(val);
117 }
118 
StdMathRem(double val,double val2)119 extern "C" double StdMathRem(double val, double val2)
120 {
121     return std::remainder(val, val2);
122 }
123 
StdMathMod(double val,double val2)124 extern "C" double StdMathMod(double val, double val2)
125 {
126     return std::fmod(val, val2);
127 }
128 
StdMathSignbit(double val)129 extern "C" bool StdMathSignbit(double val)
130 {
131     return std::signbit(val);
132 }
133 
StdMathImul(float val,float val2)134 extern "C" int StdMathImul(float val, float val2)
135 {
136     if (!std::isfinite(val) || !std::isfinite(val2)) {
137         return 0;
138     }
139     return static_cast<int32_t>(static_cast<int64_t>(val) * static_cast<int64_t>(val2));
140 }
141 
StdMathFround(double val)142 extern "C" double StdMathFround(double val)
143 {
144     if (std::isnan(std::abs(val))) {
145         return std::numeric_limits<float>::quiet_NaN();
146     }
147 
148     return static_cast<float>(val);
149 }
150 
151 }  // namespace panda::ets::intrinsics
152