OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WASM_EXTERNAL_REFS_H |
| 6 #define WASM_EXTERNAL_REFS_H |
| 7 |
| 8 namespace v8 { |
| 9 namespace internal { |
| 10 namespace wasm { |
| 11 |
| 12 static void f32_trunc_wrapper(float* param) { *param = truncf(*param); } |
| 13 |
| 14 static void f32_floor_wrapper(float* param) { *param = floorf(*param); } |
| 15 |
| 16 static void f32_ceil_wrapper(float* param) { *param = ceilf(*param); } |
| 17 |
| 18 static void f32_nearest_int_wrapper(float* param) { |
| 19 *param = nearbyintf(*param); |
| 20 } |
| 21 |
| 22 static void f64_trunc_wrapper(double* param) { *param = trunc(*param); } |
| 23 |
| 24 static void f64_floor_wrapper(double* param) { *param = floor(*param); } |
| 25 |
| 26 static void f64_ceil_wrapper(double* param) { *param = ceil(*param); } |
| 27 |
| 28 static void f64_nearest_int_wrapper(double* param) { |
| 29 *param = nearbyint(*param); |
| 30 } |
| 31 |
| 32 static void int64_to_float32_wrapper(int64_t* input, float* output) { |
| 33 *output = static_cast<float>(*input); |
| 34 } |
| 35 |
| 36 static void uint64_to_float32_wrapper(uint64_t* input, float* output) { |
| 37 #if V8_CC_MSVC |
| 38 // With MSVC we use static_cast<float>(uint32_t) instead of |
| 39 // static_cast<float>(uint64_t) to achieve round-to-nearest-ties-even |
| 40 // semantics. The idea is to calculate |
| 41 // static_cast<float>(high_word) * 2^32 + static_cast<float>(low_word). To |
| 42 // achieve proper rounding in all cases we have to adjust the high_word |
| 43 // with a "rounding bit" sometimes. The rounding bit is stored in the LSB of |
| 44 // the high_word if the low_word may affect the rounding of the high_word. |
| 45 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); |
| 46 uint32_t high_word = static_cast<uint32_t>(*input >> 32); |
| 47 |
| 48 float shift = static_cast<float>(1ull << 32); |
| 49 // If the MSB of the high_word is set, then we make space for a rounding bit. |
| 50 if (high_word < 0x80000000) { |
| 51 high_word <<= 1; |
| 52 shift = static_cast<float>(1ull << 31); |
| 53 } |
| 54 |
| 55 if ((high_word & 0xfe000000) && low_word) { |
| 56 // Set the rounding bit. |
| 57 high_word |= 1; |
| 58 } |
| 59 |
| 60 float result = static_cast<float>(high_word); |
| 61 result *= shift; |
| 62 result += static_cast<float>(low_word); |
| 63 *output = result; |
| 64 |
| 65 #else |
| 66 *output = static_cast<float>(*input); |
| 67 #endif |
| 68 } |
| 69 |
| 70 static void int64_to_float64_wrapper(int64_t* input, double* output) { |
| 71 *output = static_cast<double>(*input); |
| 72 } |
| 73 |
| 74 static void uint64_to_float64_wrapper(uint64_t* input, double* output) { |
| 75 #if V8_CC_MSVC |
| 76 // With MSVC we use static_cast<double>(uint32_t) instead of |
| 77 // static_cast<double>(uint64_t) to achieve round-to-nearest-ties-even |
| 78 // semantics. The idea is to calculate |
| 79 // static_cast<double>(high_word) * 2^32 + static_cast<double>(low_word). |
| 80 uint32_t low_word = static_cast<uint32_t>(*input & 0xffffffff); |
| 81 uint32_t high_word = static_cast<uint32_t>(*input >> 32); |
| 82 |
| 83 double shift = static_cast<double>(1ull << 32); |
| 84 |
| 85 double result = static_cast<double>(high_word); |
| 86 result *= shift; |
| 87 result += static_cast<double>(low_word); |
| 88 *output = result; |
| 89 |
| 90 #else |
| 91 *output = static_cast<double>(*input); |
| 92 #endif |
| 93 } |
| 94 |
| 95 } // namespace wasm |
| 96 } // namespace internal |
| 97 } // namespace v8 |
| 98 |
| 99 #endif |
OLD | NEW |