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 "intrinsics.h" 17 #include "plugins/ets/runtime/types/ets_string.h" 18 #include "plugins/ets/runtime/intrinsics/helpers/ets_intrinsics_helpers.h" 19 20 namespace panda::ets::intrinsics { 21 StdCoreFloatToString(float number,int radix)22EtsString *StdCoreFloatToString(float number, int radix) 23 { 24 return helpers::FpToString(number, radix); 25 } 26 StdCoreFloatIsNan(float v)27extern "C" EtsBoolean StdCoreFloatIsNan(float v) 28 { 29 return ToEtsBoolean(v != v); 30 } 31 StdCoreFloatIsFinite(float v)32extern "C" EtsBoolean StdCoreFloatIsFinite(float v) 33 { 34 static const float POSITIVE_INFINITY = 1.0 / 0.0; 35 static const float NEGATIVE_INFINITY = -1.0 / 0.0; 36 37 return ToEtsBoolean(v == v && v != POSITIVE_INFINITY && v != NEGATIVE_INFINITY); 38 } 39 StdCoreFloatBitCastFromInt(EtsInt i)40extern "C" EtsFloat StdCoreFloatBitCastFromInt(EtsInt i) 41 { 42 return bit_cast<EtsFloat>(i); 43 } 44 StdCoreFloatBitCastToInt(EtsFloat f)45extern "C" EtsInt StdCoreFloatBitCastToInt(EtsFloat f) 46 { 47 return bit_cast<EtsInt>(f); 48 } 49 IsInteger(float v)50static inline bool IsInteger(float v) 51 { 52 return std::isfinite(v) && (std::fabs(v - std::trunc(v)) <= std::numeric_limits<float>::epsilon()); 53 } 54 StdCoreFloatIsInteger(float v)55extern "C" EtsBoolean StdCoreFloatIsInteger(float v) 56 { 57 return ToEtsBoolean(IsInteger(v)); 58 } 59 60 /* 61 * In ETS Float.isSafeInteger returns (Float.isInteger(v) && (abs(v) <= Float.MAX_SAFE_INTEGER)). 62 * MAX_SAFE_INTEGER is a max integer value that can be used as a float without losing precision. 63 */ StdCoreFloatIsSafeInteger(float v)64extern "C" EtsBoolean StdCoreFloatIsSafeInteger(float v) 65 { 66 return ToEtsBoolean(IsInteger(v) && (std::fabs(v) <= helpers::MaxSafeInteger<float>())); 67 } 68 69 } // namespace panda::ets::intrinsics 70