Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 1 | // $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 ( |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 18 | "bufio" |
| 19 | "fmt" |
| 20 | "os" |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 23 | var bout *bufio.Writer |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 30 | hi uint32 |
| 31 | lo uint32 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type Int64 struct { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 35 | hi int32 |
| 36 | lo uint32 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func (a Uint64) Int64() (c Int64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 40 | c.hi = int32(a.hi) |
| 41 | c.lo = a.lo |
| 42 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func (a Uint64) Cmp(b Uint64) int { |
| 46 | switch { |
| 47 | case a.hi < b.hi: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 48 | return -1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 49 | case a.hi > b.hi: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 50 | return 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 51 | case a.lo < b.lo: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 52 | return -1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 53 | case a.lo > b.lo: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 54 | return 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 55 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 56 | return 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func (a Uint64) LeftShift(b uint) (c Uint64) { |
| 60 | switch { |
| 61 | case b >= 64: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 62 | c.hi = 0 |
| 63 | c.lo = 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 64 | case b >= 32: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 65 | c.hi = a.lo << (b - 32) |
| 66 | c.lo = 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 67 | default: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 68 | c.hi = a.hi<<b | a.lo>>(32-b) |
| 69 | c.lo = a.lo << b |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 70 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 71 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (a Uint64) RightShift(b uint) (c Uint64) { |
| 75 | switch { |
| 76 | case b >= 64: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 77 | c.hi = 0 |
| 78 | c.lo = a.hi |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 79 | case b >= 32: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 80 | c.hi = 0 |
| 81 | c.lo = a.hi >> (b - 32) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 82 | default: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 83 | c.hi = a.hi >> b |
| 84 | c.lo = a.hi<<(32-b) | a.lo>>b |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 85 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 86 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func (a Uint64) LeftShift64(b Uint64) (c Uint64) { |
| 90 | if b.hi != 0 || b.lo >= 64 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 91 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 92 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 93 | return a.LeftShift(uint(b.lo)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (a Uint64) RightShift64(b Uint64) (c Uint64) { |
| 97 | if b.hi != 0 || b.lo >= 64 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 98 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 99 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 100 | return a.RightShift(uint(b.lo)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func (a Uint64) Plus(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 104 | var carry uint32 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 105 | if c.lo = a.lo + b.lo; c.lo < a.lo { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 106 | carry = 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 107 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 108 | c.hi = a.hi + b.hi + carry |
| 109 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func (a Uint64) Minus(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 113 | var borrow uint32 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 114 | if c.lo = a.lo - b.lo; c.lo > a.lo { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 115 | borrow = 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 116 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 117 | c.hi = a.hi - b.hi - borrow |
| 118 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | func (a Uint64) Neg() (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 122 | var zero Uint64 |
| 123 | return zero.Minus(a) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func (a Uint64) Com() (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 127 | c.hi = ^a.hi |
| 128 | c.lo = ^a.lo |
| 129 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | func (a Uint64) Len() int { |
| 133 | switch { |
| 134 | case a.hi != 0: |
| 135 | for i := 31; i >= 0; i-- { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 136 | if a.hi&(1<<uint(i)) != 0 { |
| 137 | return i + 1 + 32 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | case a.lo != 0: |
| 141 | for i := 31; i >= 0; i-- { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 142 | if a.lo&(1<<uint(i)) != 0 { |
| 143 | return i + 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 147 | return 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | func (a Uint64) HasBit(b uint) bool { |
| 151 | switch { |
| 152 | case b >= 64: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 153 | return false |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 154 | case b >= 32: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 155 | return a.hi&(1<<(b-32)) != 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 156 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 157 | return a.lo&(1<<b) != 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func (a Uint64) Times(b Uint64) (c Uint64) { |
| 161 | for i := uint(0); i < 64; i++ { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 162 | if b.HasBit(i) { |
| 163 | c = c.Plus(a.LeftShift(i)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 166 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | func (a Uint64) DivMod(b Uint64) (quo, rem Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 170 | n := a.Len() - b.Len() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 171 | if n >= 0 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 172 | b = b.LeftShift(uint(n)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 173 | for i := 0; i <= n; i++ { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 174 | quo = quo.LeftShift(1) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 175 | if b.Cmp(a) <= 0 { // b <= a |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 176 | quo.lo |= 1 |
| 177 | a = a.Minus(b) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 178 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 179 | b = b.RightShift(1) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 182 | rem = a |
| 183 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | func (a Uint64) And(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 187 | c.hi = a.hi & b.hi |
| 188 | c.lo = a.lo & b.lo |
| 189 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | func (a Uint64) AndNot(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 193 | c.hi = a.hi &^ b.hi |
| 194 | c.lo = a.lo &^ b.lo |
| 195 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | func (a Uint64) Or(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 199 | c.hi = a.hi | b.hi |
| 200 | c.lo = a.lo | b.lo |
| 201 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | func (a Uint64) Xor(b Uint64) (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 205 | c.hi = a.hi ^ b.hi |
| 206 | c.lo = a.lo ^ b.lo |
| 207 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 210 | func (a Uint64) String() string { return fmt.Sprintf("%#x%08x", a.hi, a.lo) } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 211 | |
| 212 | func (a Int64) Uint64() (c Uint64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 213 | c.hi = uint32(a.hi) |
| 214 | c.lo = a.lo |
| 215 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 224 | return -1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 225 | case a.hi > b.hi: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 226 | return 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 227 | case a.lo < b.lo: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 228 | return -1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 229 | case a.lo > b.lo: |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 230 | return 1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 231 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 232 | return 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 235 | func (a Int64) LeftShift(b uint) (c Int64) { return a.Uint64().LeftShift(b).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 236 | |
| 237 | func (a Int64) RightShift(b uint) (c Int64) { |
| 238 | switch { |
| 239 | case b >= 64: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 240 | c.hi = a.hi >> 31 // sign extend |
| 241 | c.lo = uint32(c.hi) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 242 | case b >= 32: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 243 | c.hi = a.hi >> 31 // sign extend |
| 244 | c.lo = uint32(a.hi >> (b - 32)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 245 | default: |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 246 | c.hi = a.hi >> b |
| 247 | c.lo = uint32(a.hi<<(32-b)) | a.lo>>b |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 248 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 249 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | func (a Int64) LeftShift64(b Uint64) (c Int64) { |
| 253 | if b.hi != 0 || b.lo >= 64 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 254 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 255 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 256 | return a.LeftShift(uint(b.lo)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | func (a Int64) RightShift64(b Uint64) (c Int64) { |
| 260 | if b.hi != 0 || b.lo >= 64 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 261 | return a.RightShift(64) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 262 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 263 | return a.RightShift(uint(b.lo)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 266 | func (a Int64) Plus(b Int64) (c Int64) { return a.Uint64().Plus(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 267 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 268 | func (a Int64) Minus(b Int64) (c Int64) { return a.Uint64().Minus(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 269 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 270 | func (a Int64) Neg() (c Int64) { return a.Uint64().Neg().Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 271 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 272 | func (a Int64) Com() (c Int64) { return a.Uint64().Com().Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 273 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 274 | func (a Int64) Times(b Int64) (c Int64) { return a.Uint64().Times(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 275 | |
| 276 | func (a Int64) DivMod(b Int64) (quo Int64, rem Int64) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 277 | var zero Int64 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 278 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 279 | quoSign := +1 |
| 280 | remSign := +1 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 281 | if a.Cmp(zero) < 0 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 282 | quoSign = -1 |
| 283 | remSign = -1 |
| 284 | a = a.Neg() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 285 | } |
| 286 | if b.Cmp(zero) < 0 { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 287 | quoSign = -quoSign |
| 288 | b = b.Neg() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 291 | q, r := a.Uint64().DivMod(b.Uint64()) |
| 292 | quo = q.Int64() |
| 293 | rem = r.Int64() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 294 | |
| 295 | if quoSign < 0 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 296 | quo = quo.Neg() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 297 | } |
| 298 | if remSign < 0 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 299 | rem = rem.Neg() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 300 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 301 | return |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 304 | func (a Int64) And(b Int64) (c Int64) { return a.Uint64().And(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 305 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 306 | func (a Int64) AndNot(b Int64) (c Int64) { return a.Uint64().AndNot(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 307 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 308 | func (a Int64) Or(b Int64) (c Int64) { return a.Uint64().Or(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 309 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 310 | func (a Int64) Xor(b Int64) (c Int64) { return a.Uint64().Xor(b.Uint64()).Int64() } |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 311 | |
| 312 | func (a Int64) String() string { |
| 313 | if a.hi < 0 { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 314 | return fmt.Sprintf("-%s", a.Neg().Uint64()) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 315 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 316 | return a.Uint64().String() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 319 | var int64Values = []Int64{ |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 327 | Int64{0, 1 << 31}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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)}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 344 | Int64{-1, 1<<32 - 1<<31}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 345 | Int64{-1, 1<<32 - (1<<31 + 1)}, |
| 346 | Int64{-1, 1<<32 - (1<<32 - 1<<30)}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 347 | Int64{-1, 0}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 348 | Int64{-1, 1}, |
| 349 | Int64{-2, 0}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 350 | Int64{-(1 << 31), 10000}, |
| 351 | Int64{-(1 << 31), 1}, |
| 352 | Int64{-(1 << 31), 0}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 353 | Int64{-0x789abcde, 0xf0123456}, |
| 354 | } |
| 355 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 356 | var uint64Values = []Uint64{ |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 364 | Uint64{0, 1 << 31}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 381 | var shiftValues = []Uint64{ |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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}, |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 400 | Uint64{1 << 28, 0}, |
| 401 | Uint64{1 << 31, 0}, |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 402 | Uint64{1<<32 - 1, 0}, |
| 403 | Uint64{1<<32 - 1, 1<<32 - 1}, |
| 404 | } |
| 405 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 406 | var ntest = 0 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 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" + |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 507 | "\n" |
| 508 | |
| 509 | func varTests() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 510 | fmt.Fprint(bout, prolog) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 511 | for _, a := range int64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 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()) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 515 | for _, b := range int64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 516 | var div, mod Int64 |
| 517 | dodiv := false |
| 518 | var zero Int64 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 519 | if b.Cmp(zero) != 0 { // b != 0 |
| 520 | // Can't divide by zero but also can't divide -0x8000...000 by -1. |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 521 | var bigneg = Int64{-0x80000000, 0} |
| 522 | var minus1 = Int64{-1, ^uint32(0)} |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 523 | if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1 |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 524 | div, mod = a.DivMod(b) |
| 525 | dodiv = true |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 530 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 531 | } |
| 532 | for _, b := range shiftValues { |
| 533 | fmt.Fprintf(bout, "\ttestInt64Shift(%v, %v, %v, %v);\n", |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 534 | a, b, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 535 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 536 | fmt.Fprintf(bout, "}\n") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | for _, a := range uint64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 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()) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 543 | for _, b := range uint64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 544 | var div, mod Uint64 |
| 545 | dodiv := false |
| 546 | var zero Uint64 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 547 | if b.Cmp(zero) != 0 { // b != 0 |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 548 | div, mod = a.DivMod(b) |
| 549 | dodiv = true |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 553 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 554 | } |
| 555 | for _, b := range shiftValues { |
| 556 | fmt.Fprintf(bout, "\ttestUint64Shift(%v, %v, %v, %v);\n", |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 557 | a, b, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 558 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 559 | fmt.Fprintf(bout, "}\n") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | // Part 2 is tests of operations involving one variable and one constant. |
| 564 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 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" + |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 580 | "\n" |
| 581 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 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" + |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 597 | "\n" |
| 598 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 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" + |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 609 | "}\n" |
| 610 | |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 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" + |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 621 | "}\n" |
| 622 | |
| 623 | func constTests() { |
| 624 | for i, a := range int64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 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") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 628 | } |
| 629 | for i, a := range uint64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 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") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 633 | } |
| 634 | for i, a := range shiftValues { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 635 | fmt.Fprintf(bout, shiftConstR, "Int64", i, "int64", a, "int64") |
| 636 | fmt.Fprintf(bout, shiftConstR, "Uint64", i, "uint64", a, "uint64") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 637 | } |
| 638 | for i, a := range int64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 639 | fmt.Fprintf(bout, "func test%v() {\n", ntest) |
| 640 | ntest++ |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 641 | for j, b := range int64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 642 | var div, mod Int64 |
| 643 | dodiv := false |
| 644 | var zero Int64 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 645 | if b.Cmp(zero) != 0 { // b != 0 |
| 646 | // Can't divide by zero but also can't divide -0x8000...000 by -1. |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 647 | var bigneg = Int64{-0x80000000, 0} |
| 648 | var minus1 = Int64{-1, ^uint32(0)} |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 649 | if a.Cmp(bigneg) != 0 || b.Cmp(minus1) != 0 { // a != -1<<63 || b != -1 |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 650 | div, mod = a.DivMod(b) |
| 651 | dodiv = true |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 656 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 659 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 660 | } |
| 661 | for j, b := range shiftValues { |
| 662 | fmt.Fprintf(bout, "\ttestInt64ShiftL%v(%v, %v, %v);\n", |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 663 | i, b, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 664 | fmt.Fprintf(bout, "\ttestInt64ShiftR%v(%v, %v, %v);\n", |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 665 | j, a, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 666 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 667 | fmt.Fprintf(bout, "}\n") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 668 | } |
| 669 | for i, a := range uint64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 670 | fmt.Fprintf(bout, "func test%v() {\n", ntest) |
| 671 | ntest++ |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 672 | for j, b := range uint64Values { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 673 | var div, mod Uint64 |
| 674 | dodiv := false |
| 675 | var zero Uint64 |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 676 | if b.Cmp(zero) != 0 { // b != 0 |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 677 | div, mod = a.DivMod(b) |
| 678 | dodiv = true |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 682 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 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, |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 685 | a.And(b), a.Or(b), a.Xor(b), a.AndNot(b), dodiv) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 686 | } |
| 687 | for j, b := range shiftValues { |
| 688 | fmt.Fprintf(bout, "\ttestUint64ShiftL%v(%v, %v, %v);\n", |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 689 | i, b, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 690 | fmt.Fprintf(bout, "\ttestUint64ShiftR%v(%v, %v, %v);\n", |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 691 | j, a, a.LeftShift64(b), a.RightShift64(b)) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 692 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 693 | fmt.Fprintf(bout, "}\n") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 698 | bout = bufio.NewWriter(os.Stdout) |
| 699 | varTests() |
| 700 | constTests() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 701 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 702 | fmt.Fprintf(bout, "func main() {\n") |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 703 | for i := 0; i < ntest; i++ { |
Robert Griesemer | 5f5dcfb | 2009-12-09 16:55:03 -0800 | [diff] [blame] | 704 | fmt.Fprintf(bout, "\ttest%v();\n", i) |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 705 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 706 | fmt.Fprintf(bout, "\tif !ok { os.Exit(1) }\n") |
| 707 | fmt.Fprintf(bout, "}\n") |
| 708 | bout.Flush() |
Russ Cox | ead9f8b | 2009-05-31 12:35:11 -0700 | [diff] [blame] | 709 | } |