OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [64bit.go] - Blame information for rev 858

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $D/$F.go && $L $F.$A && ./$A.out >tmp.go &&
2
// $G tmp.go && $L tmp.$A && ./$A.out || echo BUG: 64bit
3
// rm -f tmp.go
4
 
5
// Copyright 2009 The Go Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style
7
// license that can be found in the LICENSE file.
8
 
9
// Generate test of 64-bit arithmetic.
10
// Most synthesized routines have different cases for
11
// constants vs variables and even the generated code has
12
// different cases for large and small constants,
13
// so try a good range of inputs.
14
 
15
package main
16
 
17
import (
18
        "bufio"
19
        "fmt"
20
        "os"
21
)
22
 
23
var bout *bufio.Writer
24
 
25
// 64-bit math without using 64-bit numbers,
26
// so that we can generate the test program even
27
// if the compiler has buggy or missing 64-bit support.
28
 
29
type Uint64 struct {
30
        hi      uint32
31
        lo      uint32
32
}
33
 
34
type Int64 struct {
35
        hi      int32
36
        lo      uint32
37
}
38
 
39
func (a Uint64) Int64() (c Int64) {
40
        c.hi = int32(a.hi)
41
        c.lo = a.lo
42
        return
43
}
44
 
45
func (a Uint64) Cmp(b Uint64) int {
46
        switch {
47
        case a.hi < b.hi:
48
                return -1
49
        case a.hi > b.hi:
50
                return 1
51
        case a.lo < b.lo:
52
                return -1
53
        case a.lo > b.lo:
54
                return 1
55
        }
56
        return 0
57
}
58
 
59
func (a Uint64) LeftShift(b uint) (c Uint64) {
60
        switch {
61
        case b >= 64:
62
                c.hi = 0
63
                c.lo = 0
64
        case b >= 32:
65
                c.hi = a.lo << (b - 32)
66
                c.lo = 0
67
        default:
68
                c.hi = a.hi<>(32-b)
69
                c.lo = a.lo << b
70
        }
71
        return
72
}
73
 
74
func (a Uint64) RightShift(b uint) (c Uint64) {
75
        switch {
76
        case b >= 64:
77
                c.hi = 0
78
                c.lo = a.hi
79
        case b >= 32:
80
                c.hi = 0
81
                c.lo = a.hi >> (b - 32)
82
        default:
83
                c.hi = a.hi >> b
84
                c.lo = a.hi<<(32-b) | a.lo>>b
85
        }
86
        return
87
}
88
 
89
func (a Uint64) LeftShift64(b Uint64) (c Uint64) {
90
        if b.hi != 0 || b.lo >= 64 {
91
                return
92
        }
93
        return a.LeftShift(uint(b.lo))
94
}
95
 
96
func (a Uint64) RightShift64(b Uint64) (c Uint64) {
97
        if b.hi != 0 || b.lo >= 64 {
98
                return
99
        }
100
        return a.RightShift(uint(b.lo))
101
}
102
 
103
func (a Uint64) Plus(b Uint64) (c Uint64) {
104
        var carry uint32
105
        if c.lo = a.lo + b.lo; c.lo < a.lo {
106
                carry = 1
107
        }
108
        c.hi = a.hi + b.hi + carry
109
        return
110
}
111
 
112
func (a Uint64) Minus(b Uint64) (c Uint64) {
113
        var borrow uint32
114
        if c.lo = a.lo - b.lo; c.lo > a.lo {
115
                borrow = 1
116
        }
117
        c.hi = a.hi - b.hi - borrow
118
        return
119
}
120
 
121
func (a Uint64) Neg() (c Uint64) {
122
        var zero Uint64
123
        return zero.Minus(a)
124
}
125
 
126
func (a Uint64) Com() (c Uint64) {
127
        c.hi = ^a.hi
128
        c.lo = ^a.lo
129
        return
130
}
131
 
132
func (a Uint64) Len() int {
133
        switch {
134
        case a.hi != 0:
135
                for i := 31; i >= 0; i-- {
136
                        if a.hi&(1<
137
                                return i + 1 + 32
138
                        }
139
                }
140
        case a.lo != 0:
141
                for i := 31; i >= 0; i-- {
142
                        if a.lo&(1<
143
                                return i + 1
144
                        }
145
                }
146
        }
147
        return 0
148
}
149
 
150
func (a Uint64) HasBit(b uint) bool {
151
        switch {
152
        case b >= 64:
153
                return false
154
        case b >= 32:
155
                return a.hi&(1<<(b-32)) != 0
156
        }
157
        return a.lo&(1<
158
}
159
 
160
func (a Uint64) Times(b Uint64) (c Uint64) {
161
        for i := uint(0); i < 64; i++ {
162
                if b.HasBit(i) {
163
                        c = c.Plus(a.LeftShift(i))
164
                }
165
        }
166
        return
167
}
168
 
169
func (a Uint64) DivMod(b Uint64) (quo, rem Uint64) {
170
        n := a.Len() - b.Len()
171
        if n >= 0 {
172
                b = b.LeftShift(uint(n))
173
                for i := 0; i <= n; i++ {
174
                        quo = quo.LeftShift(1)
175
                        if b.Cmp(a) <= 0 {      // b <= a
176
                                quo.lo |= 1
177
                                a = a.Minus(b)
178
                        }
179
                        b = b.RightShift(1)
180
                }
181
        }
182
        rem = a
183
        return
184
}
185
 
186
func (a Uint64) And(b Uint64) (c Uint64) {
187
        c.hi = a.hi & b.hi
188
        c.lo = a.lo & b.lo
189
        return
190
}
191
 
192
func (a Uint64) AndNot(b Uint64) (c Uint64) {
193
        c.hi = a.hi &^ b.hi
194
        c.lo = a.lo &^ b.lo
195
        return
196
}
197
 
198
func (a Uint64) Or(b Uint64) (c Uint64) {
199
        c.hi = a.hi | b.hi
200
        c.lo = a.lo | b.lo
201
        return
202
}
203
 
204
func (a Uint64) Xor(b Uint64) (c Uint64) {
205
        c.hi = a.hi ^ b.hi
206
        c.lo = a.lo ^ b.lo
207
        return
208
}
209
 
210
func (a Uint64) String() string { return fmt.Sprintf("%#x%08x", a.hi, a.lo) }
211
 
212
func (a Int64) Uint64() (c Uint64) {
213
        c.hi = uint32(a.hi)
214
        c.lo = a.lo
215
        return
216
}
217
 
218
func (a Int64) Cmp(b Int64) int {
219
        // Same body as Uint64.Cmp,
220
        // but behaves differently
221
        // because hi is uint32 not int32.
222
        switch {
223
        case a.hi < b.hi:
224
                return -1
225
        case a.hi > b.hi:
226
                return 1
227
        case a.lo < b.lo:
228
                return -1
229
        case a.lo > b.lo:
230
                return 1
231
        }
232
        return 0
233
}
234
 
235
func (a Int64) LeftShift(b uint) (c Int64)      { return a.Uint64().LeftShift(b).Int64() }
236
 
237
func (a Int64) RightShift(b uint) (c Int64) {
238
        switch {
239
        case b >= 64:
240
                c.hi = a.hi >> 31       // sign extend
241
                c.lo = uint32(c.hi)
242
        case b >= 32:
243
                c.hi = a.hi >> 31       // sign extend
244
                c.lo = uint32(a.hi >> (b - 32))
245
        default:
246
                c.hi = a.hi >> b
247
                c.lo = uint32(a.hi<<(32-b)) | a.lo>>b
248
        }
249
        return
250
}
251
 
252
func (a Int64) LeftShift64(b Uint64) (c Int64) {
253
        if b.hi != 0 || b.lo >= 64 {
254
                return
255
        }
256
        return a.LeftShift(uint(b.lo))
257
}
258
 
259
func (a Int64) RightShift64(b Uint64) (c Int64) {
260
        if b.hi != 0 || b.lo >= 64 {
261
                return a.RightShift(64)
262
        }
263
        return a.RightShift(uint(b.lo))
264
}
265
 
266
func (a Int64) Plus(b Int64) (c Int64)  { return a.Uint64().Plus(b.Uint64()).Int64() }
267
 
268
func (a Int64) Minus(b Int64) (c Int64) { return a.Uint64().Minus(b.Uint64()).Int64() }
269
 
270
func (a Int64) Neg() (c Int64)  { return a.Uint64().Neg().Int64() }
271
 
272
func (a Int64) Com() (c Int64)  { return a.Uint64().Com().Int64() }
273
 
274
func (a Int64) Times(b Int64) (c Int64) { return a.Uint64().Times(b.Uint64()).Int64() }
275
 
276
func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) {
277
        var zero Int64
278
 
279
        quoSign := +1
280
        remSign := +1
281
        if a.Cmp(zero) < 0 {
282
                quoSign = -1
283
                remSign = -1
284
                a = a.Neg()
285
        }
286
        if b.Cmp(zero) < 0 {
287
                quoSign = -quoSign
288
                b = b.Neg()
289
        }
290
 
291
        q, r := a.Uint64().DivMod(b.Uint64())
292
        quo = q.Int64()
293
        rem = r.Int64()
294
 
295
        if quoSign < 0 {
296
                quo = quo.Neg()
297
        }
298
        if remSign < 0 {
299
                rem = rem.Neg()
300
        }
301
        return
302
}
303
 
304
func (a Int64) And(b Int64) (c Int64)   { return a.Uint64().And(b.Uint64()).Int64() }
305
 
306
func (a Int64) AndNot(b Int64) (c Int64)        { return a.Uint64().AndNot(b.Uint64()).Int64() }
307
 
308
func (a Int64) Or(b Int64) (c Int64)    { return a.Uint64().Or(b.Uint64()).Int64() }
309
 
310
func (a Int64) Xor(b Int64) (c Int64)   { return a.Uint64().Xor(b.Uint64()).Int64() }
311
 
312
func (a Int64) String() string {
313
        if a.hi < 0 {
314
                return fmt.Sprintf("-%s", a.Neg().Uint64())
315
        }
316
        return a.Uint64().String()
317
}
318
 
319
var int64Values = []Int64{
320
        Int64{0, 0},
321
        Int64{0, 1},
322
        Int64{0, 2},
323
        Int64{0, 3},
324
        Int64{0, 100},
325
        Int64{0, 10001},
326
        Int64{0, 1<<31 - 1},
327
        Int64{0, 1 << 31},
328
        Int64{0, 1<<31 + 1},
329
        Int64{0, 1<<32 - 1<<30},
330
        Int64{0, 1<<32 - 1},
331
        Int64{1, 0},
332
        Int64{1, 1},
333
        Int64{2, 0},
334
        Int64{1<<31 - 1, 1<<32 - 10000},
335
        Int64{1<<31 - 1, 1<<32 - 1},
336
        Int64{0x789abcde, 0xf0123456},
337
 
338
        Int64{-1, 1<<32 - 1},
339
        Int64{-1, 1<<32 - 2},
340
        Int64{-1, 1<<32 - 3},
341
        Int64{-1, 1<<32 - 100},
342
        Int64{-1, 1<<32 - 10001},
343
        Int64{-1, 1<<32 - (1<<31 - 1)},
344
        Int64{-1, 1<<32 - 1<<31},
345
        Int64{-1, 1<<32 - (1<<31 + 1)},
346
        Int64{-1, 1<<32 - (1<<32 - 1<<30)},
347
        Int64{-1, 0},
348
        Int64{-1, 1},
349
        Int64{-2, 0},
350
        Int64{-(1 << 31), 10000},
351
        Int64{-(1 << 31), 1},
352
        Int64{-(1 << 31), 0},
353
        Int64{-0x789abcde, 0xf0123456},
354
}
355
 
356
var uint64Values = []Uint64{
357
        Uint64{0, 0},
358
        Uint64{0, 1},
359
        Uint64{0, 2},
360
        Uint64{0, 3},
361
        Uint64{0, 100},
362
        Uint64{0, 10001},
363
        Uint64{0, 1<<31 - 1},
364
        Uint64{0, 1 << 31},
365
        Uint64{0, 1<<31 + 1},
366
        Uint64{0, 1<<32 - 1<<30},
367
        Uint64{0, 1<<32 - 1},
368
        Uint64{1, 0},
369
        Uint64{1, 1},
370
        Uint64{2, 0},
371
        Uint64{1<<31 - 1, 1<<32 - 10000},
372
        Uint64{1<<31 - 1, 1<<32 - 1},
373
        Uint64{1<<32 - 1<<30, 0},
374
        Uint64{1<<32 - 1, 0},
375
        Uint64{1<<32 - 1, 1<<32 - 100},
376
        Uint64{1<<32 - 1, 1<<32 - 1},
377
        Uint64{0x789abcde, 0xf0123456},
378
        Uint64{0xfedcba98, 0x76543210},
379
}
380
 
381
var shiftValues = []Uint64{
382
        Uint64{0, 0},
383
        Uint64{0, 1},
384
        Uint64{0, 2},
385
        Uint64{0, 3},
386
        Uint64{0, 15},
387
        Uint64{0, 16},
388
        Uint64{0, 17},
389
        Uint64{0, 31},
390
        Uint64{0, 32},
391
        Uint64{0, 33},
392
        Uint64{0, 61},
393
        Uint64{0, 62},
394
        Uint64{0, 63},
395
        Uint64{0, 64},
396
        Uint64{0, 65},
397
        Uint64{0, 1<<32 - 1},
398
        Uint64{1, 0},
399
        Uint64{1, 1},
400
        Uint64{1 << 28, 0},
401
        Uint64{1 << 31, 0},
402
        Uint64{1<<32 - 1, 0},
403
        Uint64{1<<32 - 1, 1<<32 - 1},
404
}
405
 
406
var ntest = 0
407
 
408
// Part 1 is tests of variable operations; generic functions
409
// called by repetitive code.  Could make a table but not worth it.
410
 
411
const prolog = "\n" +
412
        "package main\n" +
413
        "\n" +
414
        "import \"os\"\n" +
415
        "\n" +
416
        "var ok = true\n" +
417
        "\n" +
418
        "func testInt64Unary(a, plus, xor, minus int64) {\n" +
419
        "       if n, op, want := +a, `+`, plus; n != want { ok=false; println(`int64`, op, a, `=`, n, `should be`, want); }\n" +
420
        "       if n, op, want := ^a, `^`, xor; n != want { ok=false; println(`int64`, op, a, `=`, n, `should be`, want); }\n" +
421
        "       if n, op, want := -a, `-`, minus; n != want { ok=false; println(`int64`, op, a, `=`, n, `should be`, want); }\n" +
422
        "}\n" +
423
        "\n" +
424
        "func testInt64Binary(a, b, add, sub, mul, div, mod, and, or, xor, andnot int64, dodiv bool) {\n" +
425
        "       if n, op, want := a + b, `+`, add; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
426
        "       if n, op, want := a - b, `-`, sub; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
427
        "       if n, op, want := a * b, `*`, mul; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
428
        "       if dodiv {\n" +
429
        "               if n, op, want := a / b, `/`, div; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
430
        "               if n, op, want := a % b, `%`, mod; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
431
        "       }\n" +
432
        "       if n, op, want := a & b, `&`, and; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
433
        "       if n, op, want := a | b, `|`, or; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
434
        "       if n, op, want := a ^ b, `^`, xor; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
435
        "       if n, op, want := a &^ b, `&^`, andnot; n != want { ok=false; println(`int64`, a, op, b, `=`, n, `should be`, want); }\n" +
436
        "}\n" +
437
        "\n" +
438
        "func testInt64Shift(a int64, b uint64, left, right int64) {\n" +
439
        "       if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`int64`, a, op, `uint64`, s, `=`, n, `should be`, want); }\n" +
440
        "       if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`int64`, a, op, `uint64`, s, `=`, n, `should be`, want); }\n" +
441
        "       if uint64(uint(b)) == b {\n" +
442
        "               b := uint(b);\n" +
443
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`int64`, a, op, `uint`, s, `=`, n, `should be`, want); }\n" +
444
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`int64`, a, op, `uint`, s, `=`, n, `should be`, want); }\n" +
445
        "       }\n" +
446
        "       if uint64(uint32(b)) == b {\n" +
447
        "               b := uint32(b);\n" +
448
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`int64`, a, op, `uint32`, s, `=`, n, `should be`, want); }\n" +
449
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`int64`, a, op, `uint32`, s, `=`, n, `should be`, want); }\n" +
450
        "       }\n" +
451
        "       if uint64(uint16(b)) == b {\n" +
452
        "               b := uint16(b);\n" +
453
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`int64`, a, op, `uint16`, s, `=`, n, `should be`, want); }\n" +
454
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`int64`, a, op, `uint16`, s, `=`, n, `should be`, want); }\n" +
455
        "       }\n" +
456
        "       if uint64(uint8(b)) == b {\n" +
457
        "               b := uint8(b);\n" +
458
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`int64`, a, op, `uint8`, s, `=`, n, `should be`, want); }\n" +
459
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`int64`, a, op, `uint8`, s, `=`, n, `should be`, want); }\n" +
460
        "       }\n" +
461
        "}\n" +
462
        "\n" +
463
        "func testUint64Unary(a, plus, xor, minus uint64) {\n" +
464
        "       if n, op, want := +a, `+`, plus; n != want { ok=false; println(`uint64`, op, a, `=`, n, `should be`, want); }\n" +
465
        "       if n, op, want := ^a, `^`, xor; n != want { ok=false; println(`uint64`, op, a, `=`, n, `should be`, want); }\n" +
466
        "       if n, op, want := -a, `-`, minus; n != want { ok=false; println(`uint64`, op, a, `=`, n, `should be`, want); }\n" +
467
        "}\n" +
468
        "\n" +
469
        "func testUint64Binary(a, b, add, sub, mul, div, mod, and, or, xor, andnot uint64, dodiv bool) {\n" +
470
        "       if n, op, want := a + b, `+`, add; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
471
        "       if n, op, want := a - b, `-`, sub; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
472
        "       if n, op, want := a * b, `*`, mul; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
473
        "       if dodiv {\n" +
474
        "               if n, op, want := a / b, `/`, div; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
475
        "               if n, op, want := a % b, `%`, mod; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
476
        "       }\n" +
477
        "       if n, op, want := a & b, `&`, and; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
478
        "       if n, op, want := a | b, `|`, or; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
479
        "       if n, op, want := a ^ b, `^`, xor; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
480
        "       if n, op, want := a &^ b, `&^`, andnot; n != want { ok=false; println(`uint64`, a, op, b, `=`, n, `should be`, want); }\n" +
481
        "}\n" +
482
        "\n" +
483
        "func testUint64Shift(a, b, left, right uint64) {\n" +
484
        "       if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`uint64`, a, op, `uint64`, s, `=`, n, `should be`, want); }\n" +
485
        "       if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`uint64`, a, op, `uint64`, s, `=`, n, `should be`, want); }\n" +
486
        "       if uint64(uint(b)) == b {\n" +
487
        "               b := uint(b);\n" +
488
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`uint64`, a, op, `uint`, s, `=`, n, `should be`, want); }\n" +
489
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`uint64`, a, op, `uint`, s, `=`, n, `should be`, want); }\n" +
490
        "       }\n" +
491
        "       if uint64(uint32(b)) == b {\n" +
492
        "               b := uint32(b);\n" +
493
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`uint64`, a, op, `uint32`, s, `=`, n, `should be`, want); }\n" +
494
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`uint64`, a, op, `uint32`, s, `=`, n, `should be`, want); }\n" +
495
        "       }\n" +
496
        "       if uint64(uint16(b)) == b {\n" +
497
        "               b := uint16(b);\n" +
498
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`uint64`, a, op, `uint16`, s, `=`, n, `should be`, want); }\n" +
499
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`uint64`, a, op, `uint16`, s, `=`, n, `should be`, want); }\n" +
500
        "       }\n" +
501
        "       if uint64(uint8(b)) == b {\n" +
502
        "               b := uint8(b);\n" +
503
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(`uint64`, a, op, `uint8`, s, `=`, n, `should be`, want); }\n" +
504
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(`uint64`, a, op, `uint8`, s, `=`, n, `should be`, want); }\n" +
505
        "       }\n" +
506
        "}\n" +
507
        "\n"
508
 
509
func varTests() {
510
        fmt.Fprint(bout, prolog)
511
        for _, a := range int64Values {
512
                fmt.Fprintf(bout, "func test%v() {\n", ntest)
513
                ntest++
514
                fmt.Fprintf(bout, "\ttestInt64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg())
515
                for _, b := range int64Values {
516
                        var div, mod Int64
517
                        dodiv := false
518
                        var zero Int64
519
                        if b.Cmp(zero) != 0 {   // b != 0
520
                                // Can't divide by zero but also can't divide -0x8000...000 by -1.
521
                                var bigneg = Int64{-0x80000000, 0}
522
                                var minus1 = Int64{-1, ^uint32(0)}
523
                                if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 {   // a != -1<<63 || b != -1
524
                                        div, mod = a.DivMod(b)
525
                                        dodiv = true
526
                                }
527
                        }
528
                        fmt.Fprintf(bout, "\ttestInt64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
529
                                a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
530
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
531
                }
532
                for _, b := range shiftValues {
533
                        fmt.Fprintf(bout, "\ttestInt64Shift(%v, %v, %v, %v);\n",
534
                                a, b, a.LeftShift64(b), a.RightShift64(b))
535
                }
536
                fmt.Fprintf(bout, "}\n")
537
        }
538
 
539
        for _, a := range uint64Values {
540
                fmt.Fprintf(bout, "func test%v() {\n", ntest)
541
                ntest++
542
                fmt.Fprintf(bout, "\ttestUint64Unary(%v, %v, %v, %v);\n", a, a, a.Com(), a.Neg())
543
                for _, b := range uint64Values {
544
                        var div, mod Uint64
545
                        dodiv := false
546
                        var zero Uint64
547
                        if b.Cmp(zero) != 0 {   // b != 0
548
                                div, mod = a.DivMod(b)
549
                                dodiv = true
550
                        }
551
                        fmt.Fprintf(bout, "\ttestUint64Binary(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
552
                                a, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
553
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
554
                }
555
                for _, b := range shiftValues {
556
                        fmt.Fprintf(bout, "\ttestUint64Shift(%v, %v, %v, %v);\n",
557
                                a, b, a.LeftShift64(b), a.RightShift64(b))
558
                }
559
                fmt.Fprintf(bout, "}\n")
560
        }
561
}
562
 
563
// Part 2 is tests of operations involving one variable and one constant.
564
 
565
const binaryConstL = "func test%vBinaryL%v(b, add, sub, mul, div, mod, and, or, xor, andnot %v, dodiv bool) {\n" +
566
        "       const a %v = %v;\n" +
567
        "       const typ = `%s`;\n" +
568
        "       if n, op, want := a + b, `+`, add; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
569
        "       if n, op, want := a - b, `-`, sub; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
570
        "       if n, op, want := a * b, `*`, mul; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
571
        "       if dodiv {\n" +
572
        "               if n, op, want := a / b, `/`, div; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
573
        "               if n, op, want := a %% b, `%%`, mod; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
574
        "       }\n" +
575
        "       if n, op, want := a & b, `&`, and; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
576
        "       if n, op, want := a | b, `|`, or; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
577
        "       if n, op, want := a ^ b, `^`, xor; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
578
        "       if n, op, want := a &^ b, `&^`, andnot; n != want { ok=false; println(typ, `const`, a, op, `var`, b, `=`, n, `should be`, want); }\n" +
579
        "}\n" +
580
        "\n"
581
 
582
const binaryConstR = "func test%vBinaryR%v(a, add, sub, mul, div, mod, and, or, xor, andnot %v, dodiv bool) {\n" +
583
        "       const b %v = %v;\n" +
584
        "       const typ = `%s`;\n" +
585
        "       if n, op, want := a + b, `+`, add; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
586
        "       if n, op, want := a - b, `-`, sub; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
587
        "       if n, op, want := a * b, `*`, mul; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
588
        "       if dodiv {\n" +
589
        "               if n, op, want := a / b, `/`, div; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
590
        "               if n, op, want := a %% b, `%%`, mod; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
591
        "       }\n" +
592
        "       if n, op, want := a & b, `&`, and; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
593
        "       if n, op, want := a | b, `|`, or; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
594
        "       if n, op, want := a ^ b, `^`, xor; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
595
        "       if n, op, want := a &^ b, `&^`, andnot; n != want { ok=false; println(typ, `var`, a, op, `const`, b, `=`, n, `should be`, want); }\n" +
596
        "}\n" +
597
        "\n"
598
 
599
const shiftConstL = "func test%vShiftL%v(b uint64, left, right %v) {\n" +
600
        "       const a %v = %v;\n" +
601
        "       const typ = `%s`;\n" +
602
        "       if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(typ, `const`, a, op, `var`, s, `=`, n, `should be`, want); }\n" +
603
        "       if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(typ, `const`, a, op, `var`, s, `=`, n, `should be`, want); }\n" +
604
        "       if uint64(uint32(b)) == b {\n" +
605
        "               b := uint32(b);\n" +
606
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(typ, `const`, a, op, `var`, s, `=`, n, `should be`, want); }\n" +
607
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(typ, `const`, a, op, `var`, s, `=`, n, `should be`, want); }\n" +
608
        "       }\n" +
609
        "}\n"
610
 
611
const shiftConstR = "func test%vShiftR%v(a, left, right %v) {\n" +
612
        "       const b uint64 = %v;\n" +
613
        "       const typ = `%s`;\n" +
614
        "       if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(typ, `var`, a, op, `const`, s, `=`, n, `should be`, want); }\n" +
615
        "       if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(typ, `var`, a, op, `const`, s, `=`, n, `should be`, want); }\n" +
616
        "       if b & 0xffffffff == b {\n" +
617
        "               const b = uint32(b & 0xffffffff);\n" +
618
        "               if n, op, s, want := a << b, `<<`, b, left; n != want { ok=false; println(typ, `var`, a, op, `const`, s, `=`, n, `should be`, want); }\n" +
619
        "               if n, op, s, want := a >> b, `>>`, b, right; n != want { ok=false; println(typ, `var`, a, op, `const`, s, `=`, n, `should be`, want); }\n" +
620
        "       }\n" +
621
        "}\n"
622
 
623
func constTests() {
624
        for i, a := range int64Values {
625
                fmt.Fprintf(bout, binaryConstL, "Int64", i, "int64", "int64", a, "int64")
626
                fmt.Fprintf(bout, binaryConstR, "Int64", i, "int64", "int64", a, "int64")
627
                fmt.Fprintf(bout, shiftConstL, "Int64", i, "int64", "int64", a, "int64")
628
        }
629
        for i, a := range uint64Values {
630
                fmt.Fprintf(bout, binaryConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
631
                fmt.Fprintf(bout, binaryConstR, "Uint64", i, "uint64", "uint64", a, "uint64")
632
                fmt.Fprintf(bout, shiftConstL, "Uint64", i, "uint64", "uint64", a, "uint64")
633
        }
634
        for i, a := range shiftValues {
635
                fmt.Fprintf(bout, shiftConstR, "Int64", i, "int64", a, "int64")
636
                fmt.Fprintf(bout, shiftConstR, "Uint64", i, "uint64", a, "uint64")
637
        }
638
        for i, a := range int64Values {
639
                fmt.Fprintf(bout, "func test%v() {\n", ntest)
640
                ntest++
641
                for j, b := range int64Values {
642
                        var div, mod Int64
643
                        dodiv := false
644
                        var zero Int64
645
                        if b.Cmp(zero) != 0 {   // b != 0
646
                                // Can't divide by zero but also can't divide -0x8000...000 by -1.
647
                                var bigneg = Int64{-0x80000000, 0}
648
                                var minus1 = Int64{-1, ^uint32(0)}
649
                                if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 {   // a != -1<<63 || b != -1
650
                                        div, mod = a.DivMod(b)
651
                                        dodiv = true
652
                                }
653
                        }
654
                        fmt.Fprintf(bout, "\ttestInt64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
655
                                i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
656
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
657
                        fmt.Fprintf(bout, "\ttestInt64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
658
                                j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
659
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
660
                }
661
                for j, b := range shiftValues {
662
                        fmt.Fprintf(bout, "\ttestInt64ShiftL%v(%v, %v, %v);\n",
663
                                i, b, a.LeftShift64(b), a.RightShift64(b))
664
                        fmt.Fprintf(bout, "\ttestInt64ShiftR%v(%v, %v, %v);\n",
665
                                j, a, a.LeftShift64(b), a.RightShift64(b))
666
                }
667
                fmt.Fprintf(bout, "}\n")
668
        }
669
        for i, a := range uint64Values {
670
                fmt.Fprintf(bout, "func test%v() {\n", ntest)
671
                ntest++
672
                for j, b := range uint64Values {
673
                        var div, mod Uint64
674
                        dodiv := false
675
                        var zero Uint64
676
                        if b.Cmp(zero) != 0 {   // b != 0
677
                                div, mod = a.DivMod(b)
678
                                dodiv = true
679
                        }
680
                        fmt.Fprintf(bout, "\ttestUint64BinaryL%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
681
                                i, b, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
682
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
683
                        fmt.Fprintf(bout, "\ttestUint64BinaryR%v(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v);\n",
684
                                j, a, a.Plus(b), a.Minus(b), a.Times(b), div, mod,
685
                                a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv)
686
                }
687
                for j, b := range shiftValues {
688
                        fmt.Fprintf(bout, "\ttestUint64ShiftL%v(%v, %v, %v);\n",
689
                                i, b, a.LeftShift64(b), a.RightShift64(b))
690
                        fmt.Fprintf(bout, "\ttestUint64ShiftR%v(%v, %v, %v);\n",
691
                                j, a, a.LeftShift64(b), a.RightShift64(b))
692
                }
693
                fmt.Fprintf(bout, "}\n")
694
        }
695
}
696
 
697
func main() {
698
        bout = bufio.NewWriter(os.Stdout)
699
        varTests()
700
        constTests()
701
 
702
        fmt.Fprintf(bout, "func main() {\n")
703
        for i := 0; i < ntest; i++ {
704
                fmt.Fprintf(bout, "\ttest%v();\n", i)
705
        }
706
        fmt.Fprintf(bout, "\tif !ok { os.Exit(1) }\n")
707
        fmt.Fprintf(bout, "}\n")
708
        bout.Flush()
709
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.