1 /* 2 * Copyright (c) 2021 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 #ifndef ECMASCRIPT_BASE_NUMBER_HELPER_H 17 #define ECMASCRIPT_BASE_NUMBER_HELPER_H 18 19 #include <cstdint> 20 #include "ecmascript/ecma_string.h" 21 22 #include "ecmascript/js_tagged_value.h" 23 24 namespace panda::ecmascript::base { 25 // NOLINTNEXTLINE(modernize-avoid-c-arrays) 26 27 constexpr double MIN_RADIX = 2; 28 constexpr double MAX_RADIX = 36; 29 constexpr double MIN_FRACTION = 0; 30 constexpr double MAX_FRACTION = 100; 31 32 // Coversion flags 33 static constexpr uint32_t NO_FLAGS = 0U; 34 static constexpr uint32_t ALLOW_BINARY = 1U << 0U; 35 static constexpr uint32_t ALLOW_OCTAL = 1U << 1U; 36 static constexpr uint32_t ALLOW_HEX = 1U << 2U; 37 static constexpr uint32_t IGNORE_TRAILING = 1U << 3U; 38 39 static constexpr uint32_t MAX_PRECISION = 16; 40 static constexpr uint8_t BINARY = 2; 41 static constexpr uint8_t OCTAL = 8; 42 static constexpr uint8_t DECIMAL = 10; 43 static constexpr uint8_t HEXADECIMAL = 16; 44 static constexpr double HALF = 0.5; 45 static constexpr double EPSILON = std::numeric_limits<double>::epsilon(); 46 static constexpr double MAX_SAFE_INTEGER = 9007199254740991; 47 static constexpr double MAX_VALUE = std::numeric_limits<double>::max(); 48 static constexpr double MIN_VALUE = std::numeric_limits<double>::min(); 49 static constexpr double POSITIVE_INFINITY = std::numeric_limits<double>::infinity(); 50 static constexpr double NAN_VALUE = std::numeric_limits<double>::quiet_NaN(); 51 52 // Helper defines for double 53 static constexpr int DOUBLE_MAX_PRECISION = 15; 54 static constexpr int DOUBLE_EXPONENT_BIAS = 0x3FF; 55 static constexpr size_t DOUBLE_SIGNIFICAND_SIZE = 52; 56 static constexpr uint64_t DOUBLE_SIGN_MASK = 0x8000000000000000ULL; 57 static constexpr uint64_t DOUBLE_EXPONENT_MASK = 0x7FFULL << DOUBLE_SIGNIFICAND_SIZE; 58 static constexpr uint64_t DOUBLE_SIGNIFICAND_MASK = 0x000FFFFFFFFFFFFFULL; 59 static constexpr uint64_t DOUBLE_HIDDEN_BIT = 1ULL << DOUBLE_SIGNIFICAND_SIZE; 60 61 static constexpr size_t INT64_BITS = 64; 62 static constexpr size_t INT32_BITS = 32; 63 static constexpr size_t INT16_BITS = 16; 64 static constexpr size_t INT8_BITS = 8; 65 66 // help defines for random 67 static constexpr int LEFT52 = 52 ; 68 static constexpr int RIGHT12 = 12; 69 static constexpr uint32_t USE_LEFT = 0x3ff; 70 static constexpr int SECONDS_TO_SUBTLE = 1000000; 71 static constexpr int RIGHT27 = 27; 72 static constexpr int LEFT25 = 25; 73 static constexpr uint64_t GET_MULTIPLY = 0x2545F4914F6CDD1D; 74 75 class NumberHelper { 76 public: IsFinite(JSTaggedValue number)77 static bool IsFinite(JSTaggedValue number) 78 { 79 return number.IsInt() || (number.IsDouble() && std::isfinite(number.GetDouble())); 80 } IsNaN(JSTaggedValue number)81 static bool IsNaN(JSTaggedValue number) 82 { 83 return number.IsDouble() && std::isnan(number.GetDouble()); 84 } 85 static JSTaggedValue DoubleToString(JSThread *thread, double number, int radix); 86 static bool IsEmptyString(const uint8_t *start, const uint8_t *end); 87 static JSHandle<EcmaString> NumberToString(const JSThread *thread, JSTaggedValue number); 88 static double TruncateDouble(double d); 89 static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix, uint32_t flags = NO_FLAGS); 90 static int32_t DoubleToInt(double d, size_t bits); 91 static int32_t DoubleInRangeInt32(double d); 92 static JSTaggedValue DoubleToExponential(JSThread *thread, double number, int digit); 93 static JSTaggedValue DoubleToFixed(JSThread *thread, double number, int digit); 94 static JSTaggedValue DoubleToPrecision(JSThread *thread, double number, int digit); 95 static JSTaggedValue StringToDoubleWithRadix(const uint8_t *start, const uint8_t *end, int radix); 96 static CString IntToString(int number); 97 static JSTaggedValue StringToBigInt(JSThread *thread, JSHandle<JSTaggedValue> strVal); 98 99 private: 100 static char Carry(char current, int radix); 101 static double Strtod(const char *str, int exponent, uint8_t radix); 102 static CString IntergerToString(double number, int radix); 103 static CString DecimalsToString(double *numberInteger, double fraction, int radix, double delta); 104 static bool GotoNonspace(uint8_t **ptr, const uint8_t *end); 105 }; 106 class RandomGenerator { 107 public: 108 static uint64_t &GetRandomState(); 109 static uint64_t XorShift64(uint64_t *pVal); 110 static void InitRandom(); 111 private: 112 static thread_local uint64_t randomState; 113 }; 114 } // namespace panda::ecmascript::base 115 #endif // ECMASCRIPT_BASE_NUMBER_HELPER_H 116