| 1 | // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "platform/globals.h" // NOLINT |
| 6 | |
| 7 | #if defined(TARGET_ARCH_ARM) |
| 8 | |
| 9 | #include "vm/constants.h" // NOLINT |
| 10 | |
| 11 | namespace dart { |
| 12 | |
| 13 | using dart::bit_cast; |
| 14 | |
| 15 | const char* cpu_reg_names[kNumberOfCpuRegisters] = { |
| 16 | "r0" , "r1" , "r2" , "r3" , "r4" , "r5" , "r6" , "r7" , |
| 17 | "r8" , "ctx" , "pp" , "fp" , "ip" , "sp" , "lr" , "pc" , |
| 18 | }; |
| 19 | |
| 20 | const char* fpu_reg_names[kNumberOfFpuRegisters] = { |
| 21 | "q0" , "q1" , "q2" , "q3" , "q4" , "q5" , "q6" , "q7" , |
| 22 | #if defined(VFPv3_D32) |
| 23 | "q8" , "q9" , "q10" , "q11" , "q12" , "q13" , "q14" , "q15" , |
| 24 | #endif |
| 25 | }; |
| 26 | const char* fpu_d_reg_names[kNumberOfDRegisters] = { |
| 27 | "d0" , "d1" , "d2" , "d3" , "d4" , "d5" , "d6" , "d7" , |
| 28 | "d8" , "d9" , "d10" , "d11" , "d12" , "d13" , "d14" , "d15" , |
| 29 | #if defined(VFPv3_D32) |
| 30 | "d16" , "d17" , "d18" , "d19" , "d20" , "d21" , "d22" , "d23" , |
| 31 | "d24" , "d25" , "d26" , "d27" , "d28" , "d29" , "d30" , "d31" , |
| 32 | #endif |
| 33 | }; |
| 34 | const char* fpu_s_reg_names[kNumberOfSRegisters] = { |
| 35 | "s0" , "s1" , "s2" , "s3" , "s4" , "s5" , "s6" , "s7" , "s8" , "s9" , "s10" , |
| 36 | "s11" , "s12" , "s13" , "s14" , "s15" , "s16" , "s17" , "s18" , "s19" , "s20" , "s21" , |
| 37 | "s22" , "s23" , "s24" , "s25" , "s26" , "s27" , "s28" , "s29" , "s30" , "s31" , |
| 38 | }; |
| 39 | |
| 40 | const Register CallingConventions::ArgumentRegisters[] = {R0, R1, R2, R3}; |
| 41 | |
| 42 | const FpuRegister CallingConventions::FpuArgumentRegisters[] = {Q0, Q1, Q2, Q3}; |
| 43 | const DRegister CallingConventions::FpuDArgumentRegisters[] = {D0, D1, D2, D3, |
| 44 | D4, D5, D6, D7}; |
| 45 | const SRegister CallingConventions::FpuSArgumentRegisters[] = { |
| 46 | S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15}; |
| 47 | |
| 48 | float ReciprocalEstimate(float a) { |
| 49 | // From the ARM Architecture Reference Manual A2-85. |
| 50 | if (isinf(a) || (fabs(a) >= exp2f(126))) |
| 51 | return a >= 0.0f ? 0.0f : -0.0f; |
| 52 | else if (a == 0.0f) |
| 53 | return 1.0f / a; |
| 54 | else if (isnan(a)) |
| 55 | return a; |
| 56 | |
| 57 | uint32_t a_bits = bit_cast<uint32_t, float>(a); |
| 58 | // scaled = '0011 1111 1110' : a<22:0> : Zeros(29) |
| 59 | uint64_t scaled = (static_cast<uint64_t>(0x3fe) << 52) | |
| 60 | ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29); |
| 61 | // result_exp = 253 - UInt(a<30:23>) |
| 62 | int32_t result_exp = 253 - ((a_bits >> 23) & 0xff); |
| 63 | ASSERT((result_exp >= 1) && (result_exp <= 252)); |
| 64 | |
| 65 | double scaled_d = bit_cast<double, uint64_t>(scaled); |
| 66 | ASSERT((scaled_d >= 0.5) && (scaled_d < 1.0)); |
| 67 | |
| 68 | // a in units of 1/512 rounded down. |
| 69 | int32_t q = static_cast<int32_t>(scaled_d * 512.0); |
| 70 | // reciprocal r. |
| 71 | double r = 1.0 / ((static_cast<double>(q) + 0.5) / 512.0); |
| 72 | // r in units of 1/256 rounded to nearest. |
| 73 | int32_t s = static_cast<int32_t>(256.0 * r + 0.5); |
| 74 | double estimate = static_cast<double>(s) / 256.0; |
| 75 | ASSERT((estimate >= 1.0) && (estimate <= (511.0 / 256.0))); |
| 76 | |
| 77 | // result = sign : result_exp<7:0> : estimate<51:29> |
| 78 | int32_t result_bits = |
| 79 | (a_bits & 0x80000000) | ((result_exp & 0xff) << 23) | |
| 80 | ((bit_cast<uint64_t, double>(estimate) >> 29) & 0x7fffff); |
| 81 | return bit_cast<float, int32_t>(result_bits); |
| 82 | } |
| 83 | |
| 84 | float ReciprocalStep(float op1, float op2) { |
| 85 | float p; |
| 86 | if ((isinf(op1) && op2 == 0.0f) || (op1 == 0.0f && isinf(op2))) { |
| 87 | p = 0.0f; |
| 88 | } else { |
| 89 | p = op1 * op2; |
| 90 | } |
| 91 | return 2.0f - p; |
| 92 | } |
| 93 | |
| 94 | float ReciprocalSqrtEstimate(float a) { |
| 95 | // From the ARM Architecture Reference Manual A2-87. |
| 96 | if (a < 0.0f) |
| 97 | return NAN; |
| 98 | else if (isinf(a) || (fabs(a) >= exp2f(126))) |
| 99 | return 0.0f; |
| 100 | else if (a == 0.0) |
| 101 | return 1.0f / a; |
| 102 | else if (isnan(a)) |
| 103 | return a; |
| 104 | |
| 105 | uint32_t a_bits = bit_cast<uint32_t, float>(a); |
| 106 | uint64_t scaled; |
| 107 | if (((a_bits >> 23) & 1) != 0) { |
| 108 | // scaled = '0 01111111101' : operand<22:0> : Zeros(29) |
| 109 | scaled = (static_cast<uint64_t>(0x3fd) << 52) | |
| 110 | ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29); |
| 111 | } else { |
| 112 | // scaled = '0 01111111110' : operand<22:0> : Zeros(29) |
| 113 | scaled = (static_cast<uint64_t>(0x3fe) << 52) | |
| 114 | ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29); |
| 115 | } |
| 116 | // result_exp = (380 - UInt(operand<30:23>) DIV 2; |
| 117 | int32_t result_exp = (380 - ((a_bits >> 23) & 0xff)) / 2; |
| 118 | |
| 119 | double scaled_d = bit_cast<double, uint64_t>(scaled); |
| 120 | ASSERT((scaled_d >= 0.25) && (scaled_d < 1.0)); |
| 121 | |
| 122 | double r; |
| 123 | if (scaled_d < 0.5) { |
| 124 | // range 0.25 <= a < 0.5 |
| 125 | |
| 126 | // a in units of 1/512 rounded down. |
| 127 | int32_t q0 = static_cast<int32_t>(scaled_d * 512.0); |
| 128 | // reciprocal root r. |
| 129 | r = 1.0 / sqrt((static_cast<double>(q0) + 0.5) / 512.0); |
| 130 | } else { |
| 131 | // range 0.5 <= a < 1.0 |
| 132 | |
| 133 | // a in units of 1/256 rounded down. |
| 134 | int32_t q1 = static_cast<int32_t>(scaled_d * 256.0); |
| 135 | // reciprocal root r. |
| 136 | r = 1.0 / sqrt((static_cast<double>(q1) + 0.5) / 256.0); |
| 137 | } |
| 138 | // r in units of 1/256 rounded to nearest. |
| 139 | int32_t s = static_cast<int>(256.0 * r + 0.5); |
| 140 | double estimate = static_cast<double>(s) / 256.0; |
| 141 | ASSERT((estimate >= 1.0) && (estimate <= (511.0 / 256.0))); |
| 142 | |
| 143 | // result = 0 : result_exp<7:0> : estimate<51:29> |
| 144 | int32_t result_bits = |
| 145 | ((result_exp & 0xff) << 23) | |
| 146 | ((bit_cast<uint64_t, double>(estimate) >> 29) & 0x7fffff); |
| 147 | return bit_cast<float, int32_t>(result_bits); |
| 148 | } |
| 149 | |
| 150 | float ReciprocalSqrtStep(float op1, float op2) { |
| 151 | float p; |
| 152 | if ((isinf(op1) && op2 == 0.0f) || (op1 == 0.0f && isinf(op2))) { |
| 153 | p = 0.0f; |
| 154 | } else { |
| 155 | p = op1 * op2; |
| 156 | } |
| 157 | return (3.0f - p) / 2.0f; |
| 158 | } |
| 159 | |
| 160 | } // namespace dart |
| 161 | |
| 162 | #endif // defined(TARGET_ARCH_ARM) |
| 163 | |