-
Notifications
You must be signed in to change notification settings - Fork 25
/
floats.go
274 lines (233 loc) · 7.37 KB
/
floats.go
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"fmt"
"math"
"math/bits"
)
const (
float32ExpBits = 8
float32SignifBits = 23
float64ExpBits = 11
float64SignifBits = 52
floatExpLabel = "floatexp"
floatSignifLabel = "floatsignif"
)
// Float32 is a shorthand for [Float32Range](-[math.MaxFloat32], [math.MaxFloat32]).
func Float32() *Generator[float32] {
return Float32Range(-math.MaxFloat32, math.MaxFloat32)
}
// Float32Min is a shorthand for [Float32Range](min, [math.MaxFloat32]).
func Float32Min(min float32) *Generator[float32] {
return Float32Range(min, math.MaxFloat32)
}
// Float32Max is a shorthand for [Float32Range](-[math.MaxFloat32], max).
func Float32Max(max float32) *Generator[float32] {
return Float32Range(-math.MaxFloat32, max)
}
// Float32Range creates a generator of 32-bit floating-point numbers in range [min, max].
// Both min and max can be infinite.
func Float32Range(min float32, max float32) *Generator[float32] {
assertf(min == min, "min should not be a NaN")
assertf(max == max, "max should not be a NaN")
assertf(min <= max, "invalid range [%v, %v]", min, max)
return newGenerator[float32](&float32Gen{
floatGen{
min: float64(min),
max: float64(max),
minVal: -math.MaxFloat32,
maxVal: math.MaxFloat32,
},
})
}
// Float64 is a shorthand for [Float64Range](-[math.MaxFloat64], [math.MaxFloat64]).
func Float64() *Generator[float64] {
return Float64Range(-math.MaxFloat64, math.MaxFloat64)
}
// Float64Min is a shorthand for [Float64Range](min, [math.MaxFloat64]).
func Float64Min(min float64) *Generator[float64] {
return Float64Range(min, math.MaxFloat64)
}
// Float64Max is a shorthand for [Float64Range](-[math.MaxFloat64], max).
func Float64Max(max float64) *Generator[float64] {
return Float64Range(-math.MaxFloat64, max)
}
// Float64Range creates a generator of 64-bit floating-point numbers in range [min, max].
// Both min and max can be infinite.
func Float64Range(min float64, max float64) *Generator[float64] {
assertf(min == min, "min should not be a NaN")
assertf(max == max, "max should not be a NaN")
assertf(min <= max, "invalid range [%v, %v]", min, max)
return newGenerator[float64](&float64Gen{
floatGen{
min: min,
max: max,
minVal: -math.MaxFloat64,
maxVal: math.MaxFloat64,
},
})
}
type floatGen struct {
min float64
max float64
minVal float64
maxVal float64
}
type float32Gen struct{ floatGen }
type float64Gen struct{ floatGen }
func (g *floatGen) stringImpl(kind string) string {
if g.min != g.minVal && g.max != g.maxVal {
return fmt.Sprintf("%sRange(%g, %g)", kind, g.min, g.max)
} else if g.min != g.minVal {
return fmt.Sprintf("%sMin(%g)", kind, g.min)
} else if g.max != g.maxVal {
return fmt.Sprintf("%sMax(%g)", kind, g.max)
}
return fmt.Sprintf("%s()", kind)
}
func (g *float32Gen) String() string {
return g.stringImpl("Float32")
}
func (g *float64Gen) String() string {
return g.stringImpl("Float64")
}
func (g *float32Gen) value(t *T) float32 {
return float32FromParts(genFloatRange(t.s, g.min, g.max, float32SignifBits))
}
func (g *float64Gen) value(t *T) float64 {
return float64FromParts(genFloatRange(t.s, g.min, g.max, float64SignifBits))
}
func ufloatFracBits(e int32, signifBits uint) uint {
if e <= 0 {
return signifBits
} else if uint(e) < signifBits {
return signifBits - uint(e)
} else {
return 0
}
}
func ufloat32Parts(f float32) (int32, uint64, uint64) {
u := math.Float32bits(f) & math.MaxInt32
e := int32(u>>float32SignifBits) - int32(bitmask64(float32ExpBits-1))
s := uint64(u) & bitmask64(float32SignifBits)
n := ufloatFracBits(e, float32SignifBits)
return e, s >> n, s & bitmask64(n)
}
func ufloat64Parts(f float64) (int32, uint64, uint64) {
u := math.Float64bits(f) & math.MaxInt64
e := int32(u>>float64SignifBits) - int32(bitmask64(float64ExpBits-1))
s := u & bitmask64(float64SignifBits)
n := ufloatFracBits(e, float64SignifBits)
return e, s >> n, s & bitmask64(n)
}
func ufloat32FromParts(e int32, si uint64, sf uint64) float32 {
e_ := (uint32(e) + uint32(bitmask64(float32ExpBits-1))) << float32SignifBits
s_ := (uint32(si) << ufloatFracBits(e, float32SignifBits)) | uint32(sf)
return math.Float32frombits(e_ | s_)
}
func ufloat64FromParts(e int32, si uint64, sf uint64) float64 {
e_ := (uint64(e) + bitmask64(float64ExpBits-1)) << float64SignifBits
s_ := (si << ufloatFracBits(e, float64SignifBits)) | sf
return math.Float64frombits(e_ | s_)
}
func float32FromParts(sign bool, e int32, si uint64, sf uint64) float32 {
f := ufloat32FromParts(e, si, sf)
if sign {
return -f
} else {
return f
}
}
func float64FromParts(sign bool, e int32, si uint64, sf uint64) float64 {
f := ufloat64FromParts(e, si, sf)
if sign {
return -f
} else {
return f
}
}
func genUfloatRange(s bitStream, min float64, max float64, signifBits uint) (int32, uint64, uint64) {
assert(min >= 0 && min <= max)
var (
minExp, maxExp int32
minSignifI, maxSignifI, minSignifF, maxSignifF uint64
)
if signifBits == float32SignifBits {
minExp, minSignifI, minSignifF = ufloat32Parts(float32(min))
maxExp, maxSignifI, maxSignifF = ufloat32Parts(float32(max))
} else {
minExp, minSignifI, minSignifF = ufloat64Parts(min)
maxExp, maxSignifI, maxSignifF = ufloat64Parts(max)
}
i := s.beginGroup(floatExpLabel, false)
e, lOverflow, rOverflow := genIntRange(s, int64(minExp), int64(maxExp), true)
s.endGroup(i, false)
fracBits := ufloatFracBits(int32(e), signifBits)
j := s.beginGroup(floatSignifLabel, false)
var siMin, siMax uint64
switch {
case lOverflow:
siMin, siMax = minSignifI, minSignifI
case rOverflow:
siMin, siMax = maxSignifI, maxSignifI
case minExp == maxExp:
siMin, siMax = minSignifI, maxSignifI
case int32(e) == minExp:
siMin, siMax = minSignifI, bitmask64(signifBits-fracBits)
case int32(e) == maxExp:
siMin, siMax = 0, maxSignifI
default:
siMin, siMax = 0, bitmask64(signifBits-fracBits)
}
si, _, _ := genUintRange(s, siMin, siMax, false)
var sfMin, sfMax uint64
switch {
case lOverflow:
sfMin, sfMax = minSignifF, minSignifF
case rOverflow:
sfMin, sfMax = maxSignifF, maxSignifF
case minExp == maxExp && minSignifI == maxSignifI:
sfMin, sfMax = minSignifF, maxSignifF
case int32(e) == minExp && si == minSignifI:
sfMin, sfMax = minSignifF, bitmask64(fracBits)
case int32(e) == maxExp && si == maxSignifI:
sfMin, sfMax = 0, maxSignifF
default:
sfMin, sfMax = 0, bitmask64(fracBits)
}
maxR := bits.Len64(sfMax - sfMin)
r := genUintNNoReject(s, uint64(maxR))
sf, _, _ := genUintRange(s, sfMin, sfMax, false)
s.endGroup(j, false)
for i := uint(0); i < uint(maxR)-uint(r); i++ {
mask := ^(uint64(1) << i)
if sf&mask < sfMin {
break
}
sf &= mask
}
return int32(e), si, sf
}
func genFloatRange(s bitStream, min float64, max float64, signifBits uint) (bool, int32, uint64, uint64) {
var posMin, negMin, pNeg float64
if min >= 0 {
posMin = min
pNeg = 0
} else if max <= 0 {
negMin = -max
pNeg = 1
} else {
pNeg = 0.5
}
if flipBiasedCoin(s, pNeg) {
e, si, sf := genUfloatRange(s, negMin, -min, signifBits)
return true, e, si, sf
} else {
e, si, sf := genUfloatRange(s, posMin, max, signifBits)
return false, e, si, sf
}
}