-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversions.go
More file actions
231 lines (218 loc) · 7 KB
/
conversions.go
File metadata and controls
231 lines (218 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package float
import (
"math"
"math/bits"
)
// Int32ToFloatX80 returns the result of converting the 32-bit two's complement integer `a'
// to the extended double-precision floating-point format. The conversion
// is performed according to the IEC/IEEE Standard for Binary Floating-Point
// Arithmetic.
func Int32ToFloatX80(a int32) X80 {
if a == 0 {
return X80Zero
}
absA := a
zSign := a < 0
if zSign {
absA = -a
}
shiftCount := bits.LeadingZeros32(uint32(absA)) + 32
return packFloatX80(zSign, 0x403E-shiftCount, uint64(absA)<<shiftCount)
}
// Int64ToFloatX80 returns the result of converting the 64-bit two's complement integer `a'
// to the extended double-precision floating-point format. The conversion
// is performed according to the IEC/IEEE Standard for Binary Floating-Point
// Arithmetic.
func Int64ToFloatX80(a int64) X80 {
if a == 0 {
return X80Zero
}
absA := a
zSign := a < 0
if zSign {
absA = -a
}
shiftCount := bits.LeadingZeros64(uint64(absA))
return packFloatX80(zSign, 0x403E-shiftCount, uint64(absA)<<shiftCount)
}
// Float32ToFloatX80 returns the result of converting the single-precision floating-point value
// `a' to the extended double-precision floating-point format. The conversion
// is performed according to the IEC/IEEE Standard for Binary Floating-Point
// Arithmetic.
func Float32ToFloatX80(a float32) X80 {
return Float64ToFloatX80(float64(a))
}
// Float64ToFloatX80 returns the result of converting the double-precision floating-point value
// `a' to the extended double-precision floating-point format. The conversion
// is performed according to the IEC/IEEE Standard for Binary Floating-Point
// Arithmetic.
func Float64ToFloatX80(a float64) X80 {
b := math.Float64bits(a)
aSig := b & 0x000FFFFFFFFFFFFF
aExp := int((b >> 52) & 0x7FF)
aSign := b>>63 != 0
if aExp == 0x7FF {
if aSig != 0 {
return X80NaN
}
return packFloatX80(aSign, 0x7FFF, 0x8000000000000000)
}
if aExp == 0 {
if aSig == 0 {
return packFloatX80(aSign, 0, 0)
}
shiftCount := bits.LeadingZeros64(aSig) - 11
aExp, aSig = 1-shiftCount, aSig<<shiftCount
}
return packFloatX80(aSign, aExp+0x3C00, (aSig|0x0010000000000000)<<11)
}
// ToInt32 returns the result of converting the extended double-precision floating-
// point value `a' to the 32-bit two's complement integer format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic---which means in particular that the conversion
// is rounded according to the current rounding mode. If `a' is a NaN, the
// largest positive integer is returned. Otherwise, if the conversion
// overflows, the largest integer with the same sign as `a' is returned.
func (a X80) ToInt32() int32 {
aSig := a.frac()
aExp := a.exp()
aSign := a.sign()
if (aExp == 0x7FFF) && uint64(aSig<<1) != 0 {
aSign = false
}
shiftCount := 0x4037 - aExp
if shiftCount <= 0 {
shiftCount = 1
}
aSig = shift64RightJamming(aSig, int16(shiftCount))
return roundAndPackInt32(aSign, aSig)
}
// ToInt32RoundZero returns the result of converting the extended double-precision floating-
// point value `a' to the 32-bit two's complement integer format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic, except that the conversion is always rounded
// toward zero. If `a' is a NaN, the largest positive integer is returned.
// Otherwise, if the conversion overflows, the largest integer with the same
// sign as `a' is returned.
func (a X80) ToInt32RoundZero() int32 {
aSig := a.frac()
aExp := a.exp()
aSign := a.sign()
invalid := func() int32 {
Raise(ExceptionInvalid)
if aSign {
return math.MinInt32
}
return math.MaxInt32
}
if 0x401E < aExp {
if aExp == 0x7FFF && uint64(aSig<<1) != 0 {
aSign = false
}
return invalid()
} else if aExp < 0x3FFF {
if aExp != 0 || aSig != 0 {
Raise(ExceptionInexact)
}
return 0
}
shiftCount := 0x403E - aExp
savedASig := aSig
aSig >>= shiftCount
z := int32(aSig)
if aSign {
z = -z
}
if (z < 0) != aSign {
return invalid()
}
if (aSig << shiftCount) != savedASig {
Raise(ExceptionInexact)
}
return z
}
// ToInt64 returns the result of converting the extended double-precision floating-
// point value `a' to the 64-bit two's complement integer format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic---which means in particular that the conversion
// is rounded according to the current rounding mode. If `a' is a NaN,
// the largest positive integer is returned. Otherwise, if the conversion
// overflows, the largest integer with the same sign as `a' is returned.
func (a X80) ToInt64() int64 {
aSig := a.frac()
aExp := a.exp()
aSign := a.sign()
shiftCount := 0x403E - aExp
aSigExtra := uint64(0)
if shiftCount < 0 {
Raise(ExceptionInvalid)
if !aSign || (aExp == 0x7FFF && aSig != 0x8000000000000000) {
return math.MaxInt64
}
return math.MinInt64
}
aSig, aSigExtra = shift64ExtraRightJamming(aSig, 0, int16(shiftCount))
return roundAndPackInt64(aSign, aSig, aSigExtra)
}
// ToInt64RoundZero returns the result of converting the extended double-precision
// floating-point value `a' to the 64-bit two's complement integer format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic, except that the conversion is always rounded
// toward zero. If `a' is a NaN, the largest positive integer is returned.
// Otherwise, if the conversion overflows, the largest integer with the same
// sign as `a' is returned.
func (a X80) ToInt64RoundZero() int64 {
aSig := a.frac()
aExp := a.exp()
aSign := a.sign()
shiftCount := aExp - 0x403E
if 0 <= shiftCount {
aSig &= math.MaxInt64
if a.high == 0xC03E && aSig == 0 {
return math.MinInt64
}
Raise(ExceptionInvalid)
if !aSign || ((aExp == 0x7FFF) && aSig != 0) {
return math.MaxInt64
}
return math.MinInt64
} else if aExp < 0x3FFF {
if aExp != 0 || aSig != 0 {
Raise(ExceptionInexact)
}
return 0
}
z := int64(aSig >> (-shiftCount))
if uint64(aSig<<(shiftCount&63)) != 0 {
Raise(ExceptionInexact)
}
if aSign {
z = -z
}
return z
}
// ToFloat32 returns the result of converting the extended double-precision floating-
// point value `a' to the double-precision floating-point format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic.
func (a X80) ToFloat32() float32 {
return float32(a.ToFloat64())
}
// ToFloat64 returns the result of converting the extended double-precision floating-
// point value `a' to the double-precision floating-point format. The
// conversion is performed according to the IEC/IEEE Standard for Binary
// Floating-Point Arithmetic.
func (a X80) ToFloat64() float64 {
aSig, aExp, aSign := a.frac(), a.exp(), a.sign()
if aExp == 0x7FFF {
if aSig<<1 != 0 {
return math.NaN()
}
return packFloat64(aSign, 0x7FF, 0)
}
zSig := shift64RightJamming(aSig, 1)
if aExp != 0 || aSig != 0 {
aExp -= 0x3C01
}
return roundAndPackFloat64(aSign, int16(aExp), zSig)
}