Flutter Engine
The Flutter Engine
constants_arm.cc
Go to the documentation of this file.
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
11namespace dart {
12
13using dart::bit_cast;
14
15const char* const cpu_reg_names[kNumberOfCpuRegisters] = {
16#if defined(DART_TARGET_OS_MACOS) || defined(DART_TARGET_OS_MACOS_IOS)
17 "r0", "r1", "r2", "r3", "r4", "pp", "r6", "fp",
18 "r8", "r9", "thr", "r11", "tmp", "sp", "lr", "pc",
19#else
20 "r0", "r1", "r2", "r3", "r4", "pp", "r6", "r7",
21 "r8", "r9", "thr", "fp", "tmp", "sp", "lr", "pc",
22#endif
23};
24
25const char* const cpu_reg_abi_names[kNumberOfCpuRegisters] = {
26#if defined(DART_TARGET_OS_MACOS) || defined(DART_TARGET_OS_MACOS_IOS)
27 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "fp",
28 "r8", "r9", "r10", "r11", "ip", "sp", "lr", "pc",
29#else
30 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
31 "r8", "r9", "r10", "fp", "ip", "sp", "lr", "pc",
32#endif
33};
34
35const char* const fpu_reg_names[kNumberOfFpuRegisters] = {
36 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
37#if defined(VFPv3_D32)
38 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
39#endif
40};
41const char* const fpu_d_reg_names[kNumberOfDRegisters] = {
42 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
43 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
44#if defined(VFPv3_D32)
45 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
46 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
47#endif
48};
49const char* const fpu_s_reg_names[kNumberOfSRegisters] = {
50 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
51 "s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20", "s21",
52 "s22", "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
53};
54
56
59 D4, D5, D6, D7};
61 S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15};
62
63float ReciprocalEstimate(float a) {
64 // From the ARM Architecture Reference Manual A2-85.
65 if (isinf(a) || (fabs(a) >= exp2f(126)))
66 return a >= 0.0f ? 0.0f : -0.0f;
67 else if (a == 0.0f)
68 return 1.0f / a;
69 else if (isnan(a))
70 return a;
71
72 uint32_t a_bits = bit_cast<uint32_t, float>(a);
73 // scaled = '0011 1111 1110' : a<22:0> : Zeros(29)
74 uint64_t scaled = (static_cast<uint64_t>(0x3fe) << 52) |
75 ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29);
76 // result_exp = 253 - UInt(a<30:23>)
77 int32_t result_exp = 253 - ((a_bits >> 23) & 0xff);
78 ASSERT((result_exp >= 1) && (result_exp <= 252));
79
80 double scaled_d = bit_cast<double, uint64_t>(scaled);
81 ASSERT((scaled_d >= 0.5) && (scaled_d < 1.0));
82
83 // a in units of 1/512 rounded down.
84 int32_t q = static_cast<int32_t>(scaled_d * 512.0);
85 // reciprocal r.
86 double r = 1.0 / ((static_cast<double>(q) + 0.5) / 512.0);
87 // r in units of 1/256 rounded to nearest.
88 int32_t s = static_cast<int32_t>(256.0 * r + 0.5);
89 double estimate = static_cast<double>(s) / 256.0;
90 ASSERT((estimate >= 1.0) && (estimate <= (511.0 / 256.0)));
91
92 // result = sign : result_exp<7:0> : estimate<51:29>
93 int32_t result_bits =
94 (a_bits & 0x80000000) | ((result_exp & 0xff) << 23) |
95 ((bit_cast<uint64_t, double>(estimate) >> 29) & 0x7fffff);
96 return bit_cast<float, int32_t>(result_bits);
97}
98
99float ReciprocalStep(float op1, float op2) {
100 float p;
101 if ((isinf(op1) && op2 == 0.0f) || (op1 == 0.0f && isinf(op2))) {
102 p = 0.0f;
103 } else {
104 p = op1 * op2;
105 }
106 return 2.0f - p;
107}
108
109float ReciprocalSqrtEstimate(float a) {
110 // From the ARM Architecture Reference Manual A2-87.
111 if (a < 0.0f)
112 return NAN;
113 else if (isinf(a) || (fabs(a) >= exp2f(126)))
114 return 0.0f;
115 else if (a == 0.0)
116 return 1.0f / a;
117 else if (isnan(a))
118 return a;
119
120 uint32_t a_bits = bit_cast<uint32_t, float>(a);
121 uint64_t scaled;
122 if (((a_bits >> 23) & 1) != 0) {
123 // scaled = '0 01111111101' : operand<22:0> : Zeros(29)
124 scaled = (static_cast<uint64_t>(0x3fd) << 52) |
125 ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29);
126 } else {
127 // scaled = '0 01111111110' : operand<22:0> : Zeros(29)
128 scaled = (static_cast<uint64_t>(0x3fe) << 52) |
129 ((static_cast<uint64_t>(a_bits) & 0x7fffff) << 29);
130 }
131 // result_exp = (380 - UInt(operand<30:23>) DIV 2;
132 int32_t result_exp = (380 - ((a_bits >> 23) & 0xff)) / 2;
133
134 double scaled_d = bit_cast<double, uint64_t>(scaled);
135 ASSERT((scaled_d >= 0.25) && (scaled_d < 1.0));
136
137 double r;
138 if (scaled_d < 0.5) {
139 // range 0.25 <= a < 0.5
140
141 // a in units of 1/512 rounded down.
142 int32_t q0 = static_cast<int32_t>(scaled_d * 512.0);
143 // reciprocal root r.
144 r = 1.0 / sqrt((static_cast<double>(q0) + 0.5) / 512.0);
145 } else {
146 // range 0.5 <= a < 1.0
147
148 // a in units of 1/256 rounded down.
149 int32_t q1 = static_cast<int32_t>(scaled_d * 256.0);
150 // reciprocal root r.
151 r = 1.0 / sqrt((static_cast<double>(q1) + 0.5) / 256.0);
152 }
153 // r in units of 1/256 rounded to nearest.
154 int32_t s = static_cast<int>(256.0 * r + 0.5);
155 double estimate = static_cast<double>(s) / 256.0;
156 ASSERT((estimate >= 1.0) && (estimate <= (511.0 / 256.0)));
157
158 // result = 0 : result_exp<7:0> : estimate<51:29>
159 int32_t result_bits =
160 ((result_exp & 0xff) << 23) |
161 ((bit_cast<uint64_t, double>(estimate) >> 29) & 0x7fffff);
162 return bit_cast<float, int32_t>(result_bits);
163}
164
165float ReciprocalSqrtStep(float op1, float op2) {
166 float p;
167 if ((isinf(op1) && op2 == 0.0f) || (op1 == 0.0f && isinf(op2))) {
168 p = 0.0f;
169 } else {
170 p = op1 * op2;
171 }
172 return (3.0f - p) / 2.0f;
173}
174
175} // namespace dart
176
177#endif // defined(TARGET_ARCH_ARM)
static const FpuRegister FpuArgumentRegisters[]
static const Register ArgumentRegisters[]
static const DRegister FpuDArgumentRegisters[]
static const SRegister FpuSArgumentRegisters[]
#define ASSERT(E)
struct MyStruct s
struct MyStruct a[10]
Definition: dart_vm.cc:33
float ReciprocalSqrtEstimate(float op)
DART_FORCE_INLINE D bit_cast(const S &source)
Definition: globals.h:646
float ReciprocalStep(float op1, float op2)
const char *const fpu_reg_names[kNumberOfFpuRegisters]
@ kNumberOfCpuRegisters
Definition: constants_arm.h:98
const int kNumberOfFpuRegisters
const char *const fpu_d_reg_names[kNumberOfDRegisters]
@ kNumberOfSRegisters
const char *const cpu_reg_names[kNumberOfCpuRegisters]
float ReciprocalSqrtStep(float op1, float op2)
QRegister FpuRegister
@ kNumberOfDRegisters
float ReciprocalEstimate(float op)
const char *const fpu_s_reg_names[kNumberOfSRegisters]
const char *const cpu_reg_abi_names[kNumberOfCpuRegisters]
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706