Text file
src/math/log_riscv64.s
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "textflag.h"
6
7 #define Ln2Hi 6.93147180369123816490e-01
8 #define Ln2Lo 1.90821492927058770002e-10
9 #define L1 6.666666666666735130e-01
10 #define L2 3.999999999940941908e-01
11 #define L3 2.857142874366239149e-01
12 #define L4 2.222219843214978396e-01
13 #define L5 1.818357216161805012e-01
14 #define L6 1.531383769920937332e-01
15 #define L7 1.479819860511658591e-01
16 #define Two52 4.503599627370496e+15 // 2**52
17 #define NaN 0x7FF8000000000001
18 #define NegInf 0xFFF0000000000000
19
20 // Mantissa of Sqrt2/2, shifted up by 12. Frexp yields f1 = 0x3FE<<52 | frac,
21 // so f1 and Sqrt2/2 share an exponent and f1 < Sqrt2/2 is exactly
22 // frac < mantissa(Sqrt2/2), which is compared with the sign and exponent
23 // fields already shifted out.
24 #define HSqrt2Frac12 0x6A09E667F3BCD000
25
26 DATA logrodata<>+0(SB)/8, $0.5
27 DATA logrodata<>+8(SB)/8, $1.0
28 DATA logrodata<>+16(SB)/8, $Ln2Hi
29 DATA logrodata<>+24(SB)/8, $Ln2Lo
30 DATA logrodata<>+32(SB)/8, $L1
31 DATA logrodata<>+40(SB)/8, $L2
32 DATA logrodata<>+48(SB)/8, $L3
33 DATA logrodata<>+56(SB)/8, $L4
34 DATA logrodata<>+64(SB)/8, $L5
35 DATA logrodata<>+72(SB)/8, $L6
36 DATA logrodata<>+80(SB)/8, $L7
37 DATA logrodata<>+88(SB)/8, $Two52
38 DATA logrodata<>+96(SB)/8, $HSqrt2Frac12
39 GLOBL logrodata<>+0(SB), NOPTR|RODATA, $104
40
41 // func archLog(x float64) float64
42 TEXT ·archLog(SB),NOSPLIT,$0-16
43 // The fast path only ever looks at the bits of x, so load them
44 // straight into an integer register instead of paying for a
45 // floating-point load plus an FMV.X.D.
46 MOV x+0(FP), X5 // X5 = raw bits of x
47
48 BLT X5, X0, isNegative // sign bit set -> x < 0 or x == -0
49
50 SRL $52, X5, X12 // X12 = biased exponent (sign bit is clear)
51 MOV $0x7FF, X6
52 BEQ X6, X12, isInfOrNaN // +Inf or NaN, return x
53
54 // Start the constant loads early: they are independent of the
55 // bit twiddling below and hide its latency.
56 MOV $logrodata<>+0(SB), X13
57 MOV 0(X13), X11 // bit pattern of 0.5
58 MOV 96(X13), X14 // HSqrt2Frac12
59 MOVD 8(X13), F4 // 1.0
60
61 BEQ X0, X12, isSmall // +0 or subnormal
62
63 normalized:
64 // f1, ki := Frexp(x); if f1 < Sqrt2/2 { f1 *= 2; ki-- }
65 //
66 SLL $12, X5, X9 // fraction bits, shifted up by 12
67 SLTU X14, X9, X7 // X7 = 1 if f1 < Sqrt2/2
68 SRL $12, X9, X10 // fraction bits
69 OR X11, X10, X10 // exponent 0x3FE: f1 in [0.5, 1)
70 SLL $52, X7, X8
71 ADD X8, X10, X10 // exponent 0x3FF when f1 is doubled
72 FMVDX X10, F1 // F1 = f1, in [Sqrt2/2, Sqrt2)
73
74 ADD $-1022, X12, X12
75 SUB X7, X12, X12 // X12 = ki
76 FCVTDL X12, F2 // F2 = k (float64)
77
78 // f1 is in [Sqrt2/2, Sqrt2), so f1-1 is exact and 2+f is the
79 // correctly rounded sum of the same exact value as 1+f1.
80 FSUBD F4, F1, F5 // f := f1 - 1
81 FADDD F4, F1, F6 // 2 + f
82 FDIVD F6, F5, F7 // s := f / (2 + f)
83
84 MOVD 32(X13), F17 // L1
85 MOVD 40(X13), F18 // L2
86 MOVD 48(X13), F19 // L3
87 MOVD 56(X13), F20 // L4
88 MOVD 64(X13), F21 // L5
89 MOVD 72(X13), F22 // L6
90 MOVD 80(X13), F23 // L7
91 MOVD 16(X13), F15 // Ln2Hi
92 MOVD 24(X13), F16 // Ln2Lo
93 MOVD 0(X13), F14 // 0.5
94 FMULD F5, F14, F12
95 FMULD F5, F12, F12 // hfsq := 0.5 * f * f
96 FMULD F2, F16, F13 // k*Ln2Lo
97
98 FMULD F7, F7, F8 // s2
99 FMULD F8, F8, F9 // s4
100
101 // t1 := s2 * (L1 + s4*(L3 + s4*(L5 + s4*L7)))
102 // t2 := s4 * (L2 + s4*(L4 + s4*L6))
103 FMADDD F9, F23, F21, F10 // s4*L7 + L5
104 FMADDD F9, F22, F20, F11 // s4*L6 + L4
105 FMADDD F9, F10, F19, F10 // s4*F10 + L3
106 FMADDD F9, F11, F18, F11 // s4*F11 + L2
107 FMADDD F9, F10, F17, F10 // s4*F10 + L1
108 FMULD F9, F11, F11 // t2
109 FMULD F8, F10, F10 // t1
110
111 FADDD F11, F10, F10 // R := t1 + t2
112
113 // return k*Ln2Hi - ((hfsq - (s*(hfsq+R) + k*Ln2Lo)) - f)
114 FADDD F12, F10, F10 // hfsq + R
115 FMADDD F7, F10, F13, F10 // s*(hfsq+R) + k*Ln2Lo
116 FSUBD F10, F12, F12
117 FSUBD F5, F12, F12
118 FMSUBD F2, F15, F12, F24
119
120 MOVD F24, ret+8(FP)
121 RET
122 isSmall:
123 BEQ X0, X5, isZero
124
125 FMVDX X5, F0
126 MOVD 88(X13), F1
127 FMULD F1, F0, F0
128 FMVXD F0, X5
129 SRL $52, X5, X12
130 ADD $-52, X12, X12
131 JMP normalized
132 isInfOrNaN:
133 MOV X5, ret+8(FP) // +Inf or NaN, return x
134 RET
135 isNegative:
136 SLL $1, X5, X6
137 BEQ X0, X6, isZero // x == -0, return -Inf
138 MOV $NaN, X6
139 MOV X6, ret+8(FP)
140 RET
141 isZero:
142 MOV $NegInf, X6
143 MOV X6, ret+8(FP)
144 RET
145
View as plain text