forked from ericlagergren/decimal
/
suite.go
358 lines (328 loc) · 8.05 KB
/
suite.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Package suite provides a simple API for parsing and using IBM Labs'
// "Floating-Point Test-Suite for IEEE"
package suite
import (
"bufio"
"fmt"
"io"
"math/big"
"math/bits"
"strings"
)
// ParseCases returns a slice of test cases in .fptest form read from r.
func ParseCases(r io.Reader) (cases []Case, err error) {
s := bufio.NewScanner(r)
s.Split(bufio.ScanLines)
for s.Scan() {
p := s.Bytes()
// Skip empty lines and comments.
if len(p) == 0 || p[0] == '#' {
continue
}
c, err := ParseCase(p)
if err != nil {
return nil, err
}
cases = append(cases, c)
}
return cases, s.Err()
}
// Test suite documentation is from:
// https://www.research.ibm.com/haifa/projects/verification/fpgen/papers/ieee-test-suite-v2.pdf
// Case represents a specific test case.
//
// Here's a nice ascii diagram:
//
// prec trap excep
// | | |
// |\ | |
// vv v v
// d64+ =0 i 100 200 -> 300 i
// ^ ^ ^^ ^^^ ^^^ ^ ^^^
// | | \| \|/ \|/ | \|/
// | | | | | | |
// | | mode \ / | output
// | op | output delim
// prefix inputs
//
type Case struct {
Prefix string
Prec int
Op Op
Mode big.RoundingMode
Trap Condition
Inputs []Data
Output Data
Excep Condition
}
// TODO(eric): String should print the same format as the input
func (c Case) String() string {
return fmt.Sprintf("%s%d [%s, %s]: %s(%s) = %s %s",
c.Prefix, c.Prec, c.Trap, c.Mode, c.Op,
join(c.Inputs, ", ", -1), c.Output, c.Excep)
}
// ShortString returns the same as String, except long data values are capped at
// length digits.
func (c Case) ShortString(length int) string {
return fmt.Sprintf("%s%d [%s, %s]: %s(%s) = %s %s",
c.Prefix, c.Prec, c.Trap, c.Mode, c.Op,
join(c.Inputs, ", ", length), trunc(c.Output, length), c.Excep)
}
func trunc(d Data, l int) Data {
if l <= 0 || l >= len(d) {
return d
}
return d[:l/2] + "..." + d[len(d)-(l/2):]
}
func join(a []Data, sep Data, l int) Data {
if len(a) == 0 {
return ""
}
if len(a) == 1 {
return trunc(a[0], l)
}
n := len(sep) * (len(a) - 1)
for i := 0; i < len(a); i++ {
n += len(trunc(a[i], l))
}
b := make([]byte, n)
bp := copy(b, trunc(a[0], l))
for _, s := range a[1:] {
bp += copy(b[bp:], sep)
bp += copy(b[bp:], trunc(s, l))
}
return Data(b)
}
// Data is input or output from a test case.
type Data string
// NoData is output when the operation throws some sort of Condition and does
// not "return" any data.
const NoData Data = "#"
// IsNaN returns two booleans indicating whether the data is a NaN value and
// whether it's signaling or not.
func (i Data) IsNaN() (nan, signal bool) {
if len(i) == 1 {
return (i == "S" || i == "Q"), i == "S"
}
if i[0] == '-' {
i = i[1:]
}
return strings.EqualFold(string(i), "nan") ||
strings.EqualFold(string(i), "qnan") ||
strings.EqualFold(string(i), "snan"), i[0] == 's' || i[0] == 'S'
}
// IsInf returns a boolean indicating whether the data is an Infinity and an
// int indicating the signedness of the Infinity.
func (i Data) IsInf() (int, bool) {
if len(i) != 4 {
return 0, false
}
if strings.EqualFold(string(i), "-Inf") {
return -1, true
}
if strings.EqualFold(string(i), "+Inf") {
return +1, true
}
return 0, false
}
// Condition is a bitmask value raised after or during specific operations.
type Condition uint32
const (
Clamped Condition = 1 << iota
ConversionSyntax
DivisionByZero
DivisionImpossible
DivisionUndefined
Inexact
InsufficientStorage
InvalidContext
InvalidOperation
Overflow
Rounded
Subnormal
Underflow
)
func (c Condition) String() string {
if c == 0 {
return "NoConditions"
}
// Each condition is one bit, so this saves some allocations.
a := make([]string, 0, bits.OnesCount32(uint32(c)))
for i := Condition(1); c != 0; i <<= 1 {
if c&i == 0 {
continue
}
switch c ^= i; i {
case Clamped:
a = append(a, "clamped")
case ConversionSyntax:
a = append(a, "conversion syntax")
case DivisionByZero:
a = append(a, "division by zero")
case DivisionImpossible:
a = append(a, "division impossible")
case Inexact:
a = append(a, "inexact")
case InsufficientStorage:
a = append(a, "insufficient storage")
case InvalidContext:
a = append(a, "invalid context")
case InvalidOperation:
a = append(a, "invalid operation")
case Overflow:
a = append(a, "overflow")
case Rounded:
a = append(a, "rounded")
case Subnormal:
a = append(a, "subnormal")
case Underflow:
a = append(a, "underflow")
default:
a = append(a, fmt.Sprintf("unknown(%d)", i))
}
}
return strings.Join(a, ", ")
}
func ConditionFromString(s string) (r Condition) {
for i := range s {
r |= valToCondition[s[i]]
}
return r
}
var valToCondition = map[byte]Condition{
'x': Inexact,
'u': Underflow, // tininess and 'extraordinary' error
'v': Underflow, // tininess and inexactness after rounding
'w': Underflow, // tininess and inexactness prior to rounding
'o': Overflow,
'z': DivisionByZero,
'i': InvalidOperation,
// custom
'c': Clamped,
'r': Rounded,
'y': ConversionSyntax,
'm': DivisionImpossible,
'n': DivisionUndefined,
't': InsufficientStorage,
'?': InvalidContext,
's': Subnormal,
}
var valToMode = map[string]big.RoundingMode{
">": big.ToPositiveInf,
"<": big.ToNegativeInf,
"0": big.ToZero,
"=0": big.ToNearestEven,
"=^": big.ToNearestAway,
"^": big.AwayFromZero,
}
// Op is a specific operation the test case must perform.
type Op uint8
const (
Add Op = iota // add
Sub // subtract
Mul // multiply
Div // divide
FMA // fused multiply-add
Sqrt // square root
Rem // remainder
RFI // round float to int
CFF // convert between floating point formats
CFI // convert float to integer
CIF // convert integer to float
CFD // convert to string
CDF // convert string to float
QuietCmp // quiet comparison
SigCmp // signaling comparison
Copy // copy
Neg // negate
Abs // absolute value
CopySign // copy sign
Scalb // scalb
Logb // logb
NextAfter // next after
Class // class
IsSigned // is signed
IsNormal // is norm
IsInf // is inf
IsZero // is zero
IsSubNormal // is subnormal
IsNaN // is nan
IsSignaling // is signaling
IsFinite // is finite
MinNum // minnum
MaxNum // maxnum
MinNumMag // minnummag
MaxNumMag // maxnummag
SameQuantum // same quantum
Quantize // quantize
NextUp // next up
NextDown // next down
Equiv // equivalent
// Custom
SetRat
Sign
Signbit
Exp
Log
Log10
Pow
IntDiv
Normalize
RoundToInt
Shift
)
var valToOp = map[string]Op{
"+": Add,
"-": Sub,
"*": Mul,
"/": Div,
"*-": FMA,
"Decimal": Sqrt,
"%": Rem,
"rfi": RFI,
"cff": CFF,
"cfi": CFI,
"cif": CIF,
"cfd": CFD,
"cdf": CDF,
"qC": QuietCmp,
"sC": SigCmp,
"cp": Copy,
"~": Neg,
"A": Abs,
"@": CopySign,
"S": Scalb,
"L": Logb,
"Na": NextAfter,
"?": Class,
"?-": IsSigned,
"?n": IsNormal,
"?f": IsFinite,
"?0": IsZero,
"?s": IsSubNormal,
"?i": IsInf,
"?N": IsNaN,
"?sN": IsSignaling,
"<C": MinNum,
">C": MaxNum,
"<A": MinNumMag,
">A": MaxNumMag,
"=quant": SameQuantum,
"quant": Quantize,
"Nu": NextUp,
"Nd": NextDown,
"eq": Equiv,
// Custom
"rat": SetRat,
"sign": Sign,
"signbit": Signbit,
"exp": Exp,
"log": Log,
"log10": Log10,
"pow": Pow,
"//": IntDiv,
"norm": Normalize,
"rtie": RoundToInt,
"shift": Shift,
}
//go:generate stringer -type=Op