• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 "intrinsics.h"
17 #include "plugins/ets/runtime/types/ets_string.h"
18 #include "plugins/ets/runtime/intrinsics/helpers/ets_intrinsics_helpers.h"
19 #include "plugins/ets/runtime/intrinsics/helpers/ets_to_string_cache.h"
20 
21 namespace ark::ets::intrinsics {
22 
StdCoreFloatToString(float number,int radix)23 EtsString *StdCoreFloatToString(float number, int radix)
24 {
25     auto *cache = PandaEtsVM::GetCurrent()->GetFloatToStringCache();
26     if (UNLIKELY(radix != helpers::DECIMAL || cache == nullptr)) {
27         return helpers::FpToString(number, radix);
28     }
29     return cache->GetOrCache(EtsCoroutine::GetCurrent(), number);
30 }
31 
StdCoreFloatIsNan(float v)32 extern "C" EtsBoolean StdCoreFloatIsNan(float v)
33 {
34     return ToEtsBoolean(v != v);
35 }
36 
StdCoreFloatIsFinite(float v)37 extern "C" EtsBoolean StdCoreFloatIsFinite(float v)
38 {
39     static const float POSITIVE_INFINITY = 1.0 / 0.0;
40     static const float NEGATIVE_INFINITY = -1.0 / 0.0;
41 
42     return ToEtsBoolean(v == v && v != POSITIVE_INFINITY && v != NEGATIVE_INFINITY);
43 }
44 
StdCoreFloatBitCastFromInt(EtsInt i)45 extern "C" EtsFloat StdCoreFloatBitCastFromInt(EtsInt i)
46 {
47     return bit_cast<EtsFloat>(i);
48 }
49 
StdCoreFloatBitCastToInt(EtsFloat f)50 extern "C" EtsInt StdCoreFloatBitCastToInt(EtsFloat f)
51 {
52     return bit_cast<EtsInt>(f);
53 }
54 
IsInteger(float v)55 static inline bool IsInteger(float v)
56 {
57     return std::isfinite(v) && (std::fabs(v - std::trunc(v)) <= std::numeric_limits<float>::epsilon());
58 }
59 
StdCoreFloatIsInteger(float v)60 extern "C" EtsBoolean StdCoreFloatIsInteger(float v)
61 {
62     return ToEtsBoolean(IsInteger(v));
63 }
64 
65 /*
66  * In ETS Float.isSafeInteger returns (Float.isInteger(v) && (abs(v) <= Float.MAX_SAFE_INTEGER)).
67  * MAX_SAFE_INTEGER is a max integer value that can be used as a float without losing precision.
68  */
StdCoreFloatIsSafeInteger(float v)69 extern "C" EtsBoolean StdCoreFloatIsSafeInteger(float v)
70 {
71     return ToEtsBoolean(IsInteger(v) && (std::fabs(v) <= helpers::MaxSafeInteger<float>()));
72 }
73 
StdCoreFloatToShort(EtsFloat val)74 EtsShort StdCoreFloatToShort(EtsFloat val)
75 {
76     // CC-OFFNXT(G.NAM.03) false positive
77     int intVal = CastFloatToInt<EtsFloat, EtsInt>(val);
78     return static_cast<int16_t>(intVal);
79 }
80 
StdCoreFloatToByte(EtsFloat val)81 EtsByte StdCoreFloatToByte(EtsFloat val)
82 {
83     // CC-OFFNXT(G.NAM.03) false positive
84     int intVal = CastFloatToInt<EtsFloat, EtsInt>(val);
85     return static_cast<int8_t>(intVal);
86 }
87 
StdCoreFloatToInt(EtsFloat val)88 EtsInt StdCoreFloatToInt(EtsFloat val)
89 {
90     return CastFloatToInt<EtsFloat, EtsInt>(val);
91 }
92 
StdCoreFloatToLong(EtsFloat val)93 EtsLong StdCoreFloatToLong(EtsFloat val)
94 {
95     return CastFloatToInt<EtsFloat, EtsLong>(val);
96 }
97 
StdCoreFloatToDouble(EtsFloat val)98 EtsDouble StdCoreFloatToDouble(EtsFloat val)
99 {
100     return static_cast<double>(val);
101 }
102 
StdCoreFloatToChar(EtsFloat val)103 EtsChar StdCoreFloatToChar(EtsFloat val)
104 {
105     return CastFloatToInt<EtsFloat, EtsChar>(val);
106 }
107 
108 }  // namespace ark::ets::intrinsics
109