| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package test |
| 6 |
|
| 7 |
import ( |
| 8 |
"math/bits" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
// Tests shifts of zero. |
| 14 |
|
| 15 |
//go:noinline |
| 16 |
func ofz64l64(n uint64) int64 { |
| 17 |
var x int64 |
| 18 |
return x << n |
| 19 |
} |
| 20 |
|
| 21 |
//go:noinline |
| 22 |
func ofz64l32(n uint32) int64 { |
| 23 |
var x int64 |
| 24 |
return x << n |
| 25 |
} |
| 26 |
|
| 27 |
//go:noinline |
| 28 |
func ofz64l16(n uint16) int64 { |
| 29 |
var x int64 |
| 30 |
return x << n |
| 31 |
} |
| 32 |
|
| 33 |
//go:noinline |
| 34 |
func ofz64l8(n uint8) int64 { |
| 35 |
var x int64 |
| 36 |
return x << n |
| 37 |
} |
| 38 |
|
| 39 |
//go:noinline |
| 40 |
func ofz64r64(n uint64) int64 { |
| 41 |
var x int64 |
| 42 |
return x >> n |
| 43 |
} |
| 44 |
|
| 45 |
//go:noinline |
| 46 |
func ofz64r32(n uint32) int64 { |
| 47 |
var x int64 |
| 48 |
return x >> n |
| 49 |
} |
| 50 |
|
| 51 |
//go:noinline |
| 52 |
func ofz64r16(n uint16) int64 { |
| 53 |
var x int64 |
| 54 |
return x >> n |
| 55 |
} |
| 56 |
|
| 57 |
//go:noinline |
| 58 |
func ofz64r8(n uint8) int64 { |
| 59 |
var x int64 |
| 60 |
return x >> n |
| 61 |
} |
| 62 |
|
| 63 |
//go:noinline |
| 64 |
func ofz64ur64(n uint64) uint64 { |
| 65 |
var x uint64 |
| 66 |
return x >> n |
| 67 |
} |
| 68 |
|
| 69 |
//go:noinline |
| 70 |
func ofz64ur32(n uint32) uint64 { |
| 71 |
var x uint64 |
| 72 |
return x >> n |
| 73 |
} |
| 74 |
|
| 75 |
//go:noinline |
| 76 |
func ofz64ur16(n uint16) uint64 { |
| 77 |
var x uint64 |
| 78 |
return x >> n |
| 79 |
} |
| 80 |
|
| 81 |
//go:noinline |
| 82 |
func ofz64ur8(n uint8) uint64 { |
| 83 |
var x uint64 |
| 84 |
return x >> n |
| 85 |
} |
| 86 |
|
| 87 |
//go:noinline |
| 88 |
func ofz32l64(n uint64) int32 { |
| 89 |
var x int32 |
| 90 |
return x << n |
| 91 |
} |
| 92 |
|
| 93 |
//go:noinline |
| 94 |
func ofz32l32(n uint32) int32 { |
| 95 |
var x int32 |
| 96 |
return x << n |
| 97 |
} |
| 98 |
|
| 99 |
//go:noinline |
| 100 |
func ofz32l16(n uint16) int32 { |
| 101 |
var x int32 |
| 102 |
return x << n |
| 103 |
} |
| 104 |
|
| 105 |
//go:noinline |
| 106 |
func ofz32l8(n uint8) int32 { |
| 107 |
var x int32 |
| 108 |
return x << n |
| 109 |
} |
| 110 |
|
| 111 |
//go:noinline |
| 112 |
func ofz32r64(n uint64) int32 { |
| 113 |
var x int32 |
| 114 |
return x >> n |
| 115 |
} |
| 116 |
|
| 117 |
//go:noinline |
| 118 |
func ofz32r32(n uint32) int32 { |
| 119 |
var x int32 |
| 120 |
return x >> n |
| 121 |
} |
| 122 |
|
| 123 |
//go:noinline |
| 124 |
func ofz32r16(n uint16) int32 { |
| 125 |
var x int32 |
| 126 |
return x >> n |
| 127 |
} |
| 128 |
|
| 129 |
//go:noinline |
| 130 |
func ofz32r8(n uint8) int32 { |
| 131 |
var x int32 |
| 132 |
return x >> n |
| 133 |
} |
| 134 |
|
| 135 |
//go:noinline |
| 136 |
func ofz32ur64(n uint64) uint32 { |
| 137 |
var x uint32 |
| 138 |
return x >> n |
| 139 |
} |
| 140 |
|
| 141 |
//go:noinline |
| 142 |
func ofz32ur32(n uint32) uint32 { |
| 143 |
var x uint32 |
| 144 |
return x >> n |
| 145 |
} |
| 146 |
|
| 147 |
//go:noinline |
| 148 |
func ofz32ur16(n uint16) uint32 { |
| 149 |
var x uint32 |
| 150 |
return x >> n |
| 151 |
} |
| 152 |
|
| 153 |
//go:noinline |
| 154 |
func ofz32ur8(n uint8) uint32 { |
| 155 |
var x uint32 |
| 156 |
return x >> n |
| 157 |
} |
| 158 |
|
| 159 |
//go:noinline |
| 160 |
func ofz16l64(n uint64) int16 { |
| 161 |
var x int16 |
| 162 |
return x << n |
| 163 |
} |
| 164 |
|
| 165 |
//go:noinline |
| 166 |
func ofz16l32(n uint32) int16 { |
| 167 |
var x int16 |
| 168 |
return x << n |
| 169 |
} |
| 170 |
|
| 171 |
//go:noinline |
| 172 |
func ofz16l16(n uint16) int16 { |
| 173 |
var x int16 |
| 174 |
return x << n |
| 175 |
} |
| 176 |
|
| 177 |
//go:noinline |
| 178 |
func ofz16l8(n uint8) int16 { |
| 179 |
var x int16 |
| 180 |
return x << n |
| 181 |
} |
| 182 |
|
| 183 |
//go:noinline |
| 184 |
func ofz16r64(n uint64) int16 { |
| 185 |
var x int16 |
| 186 |
return x >> n |
| 187 |
} |
| 188 |
|
| 189 |
//go:noinline |
| 190 |
func ofz16r32(n uint32) int16 { |
| 191 |
var x int16 |
| 192 |
return x >> n |
| 193 |
} |
| 194 |
|
| 195 |
//go:noinline |
| 196 |
func ofz16r16(n uint16) int16 { |
| 197 |
var x int16 |
| 198 |
return x >> n |
| 199 |
} |
| 200 |
|
| 201 |
//go:noinline |
| 202 |
func ofz16r8(n uint8) int16 { |
| 203 |
var x int16 |
| 204 |
return x >> n |
| 205 |
} |
| 206 |
|
| 207 |
//go:noinline |
| 208 |
func ofz16ur64(n uint64) uint16 { |
| 209 |
var x uint16 |
| 210 |
return x >> n |
| 211 |
} |
| 212 |
|
| 213 |
//go:noinline |
| 214 |
func ofz16ur32(n uint32) uint16 { |
| 215 |
var x uint16 |
| 216 |
return x >> n |
| 217 |
} |
| 218 |
|
| 219 |
//go:noinline |
| 220 |
func ofz16ur16(n uint16) uint16 { |
| 221 |
var x uint16 |
| 222 |
return x >> n |
| 223 |
} |
| 224 |
|
| 225 |
//go:noinline |
| 226 |
func ofz16ur8(n uint8) uint16 { |
| 227 |
var x uint16 |
| 228 |
return x >> n |
| 229 |
} |
| 230 |
|
| 231 |
//go:noinline |
| 232 |
func ofz8l64(n uint64) int8 { |
| 233 |
var x int8 |
| 234 |
return x << n |
| 235 |
} |
| 236 |
|
| 237 |
//go:noinline |
| 238 |
func ofz8l32(n uint32) int8 { |
| 239 |
var x int8 |
| 240 |
return x << n |
| 241 |
} |
| 242 |
|
| 243 |
//go:noinline |
| 244 |
func ofz8l16(n uint16) int8 { |
| 245 |
var x int8 |
| 246 |
return x << n |
| 247 |
} |
| 248 |
|
| 249 |
//go:noinline |
| 250 |
func ofz8l8(n uint8) int8 { |
| 251 |
var x int8 |
| 252 |
return x << n |
| 253 |
} |
| 254 |
|
| 255 |
//go:noinline |
| 256 |
func ofz8r64(n uint64) int8 { |
| 257 |
var x int8 |
| 258 |
return x >> n |
| 259 |
} |
| 260 |
|
| 261 |
//go:noinline |
| 262 |
func ofz8r32(n uint32) int8 { |
| 263 |
var x int8 |
| 264 |
return x >> n |
| 265 |
} |
| 266 |
|
| 267 |
//go:noinline |
| 268 |
func ofz8r16(n uint16) int8 { |
| 269 |
var x int8 |
| 270 |
return x >> n |
| 271 |
} |
| 272 |
|
| 273 |
//go:noinline |
| 274 |
func ofz8r8(n uint8) int8 { |
| 275 |
var x int8 |
| 276 |
return x >> n |
| 277 |
} |
| 278 |
|
| 279 |
//go:noinline |
| 280 |
func ofz8ur64(n uint64) uint8 { |
| 281 |
var x uint8 |
| 282 |
return x >> n |
| 283 |
} |
| 284 |
|
| 285 |
//go:noinline |
| 286 |
func ofz8ur32(n uint32) uint8 { |
| 287 |
var x uint8 |
| 288 |
return x >> n |
| 289 |
} |
| 290 |
|
| 291 |
//go:noinline |
| 292 |
func ofz8ur16(n uint16) uint8 { |
| 293 |
var x uint8 |
| 294 |
return x >> n |
| 295 |
} |
| 296 |
|
| 297 |
//go:noinline |
| 298 |
func ofz8ur8(n uint8) uint8 { |
| 299 |
var x uint8 |
| 300 |
return x >> n |
| 301 |
} |
| 302 |
|
| 303 |
func TestShiftOfZero(t *testing.T) { |
| 304 |
if got := ofz64l64(5); got != 0 { |
| 305 |
t.Errorf("0<<5 == %d, want 0", got) |
| 306 |
} |
| 307 |
if got := ofz64l32(5); got != 0 { |
| 308 |
t.Errorf("0<<5 == %d, want 0", got) |
| 309 |
} |
| 310 |
if got := ofz64l16(5); got != 0 { |
| 311 |
t.Errorf("0<<5 == %d, want 0", got) |
| 312 |
} |
| 313 |
if got := ofz64l8(5); got != 0 { |
| 314 |
t.Errorf("0<<5 == %d, want 0", got) |
| 315 |
} |
| 316 |
if got := ofz64r64(5); got != 0 { |
| 317 |
t.Errorf("0>>5 == %d, want 0", got) |
| 318 |
} |
| 319 |
if got := ofz64r32(5); got != 0 { |
| 320 |
t.Errorf("0>>5 == %d, want 0", got) |
| 321 |
} |
| 322 |
if got := ofz64r16(5); got != 0 { |
| 323 |
t.Errorf("0>>5 == %d, want 0", got) |
| 324 |
} |
| 325 |
if got := ofz64r8(5); got != 0 { |
| 326 |
t.Errorf("0>>5 == %d, want 0", got) |
| 327 |
} |
| 328 |
if got := ofz64ur64(5); got != 0 { |
| 329 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 330 |
} |
| 331 |
if got := ofz64ur32(5); got != 0 { |
| 332 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 333 |
} |
| 334 |
if got := ofz64ur16(5); got != 0 { |
| 335 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 336 |
} |
| 337 |
if got := ofz64ur8(5); got != 0 { |
| 338 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 339 |
} |
| 340 |
|
| 341 |
if got := ofz32l64(5); got != 0 { |
| 342 |
t.Errorf("0<<5 == %d, want 0", got) |
| 343 |
} |
| 344 |
if got := ofz32l32(5); got != 0 { |
| 345 |
t.Errorf("0<<5 == %d, want 0", got) |
| 346 |
} |
| 347 |
if got := ofz32l16(5); got != 0 { |
| 348 |
t.Errorf("0<<5 == %d, want 0", got) |
| 349 |
} |
| 350 |
if got := ofz32l8(5); got != 0 { |
| 351 |
t.Errorf("0<<5 == %d, want 0", got) |
| 352 |
} |
| 353 |
if got := ofz32r64(5); got != 0 { |
| 354 |
t.Errorf("0>>5 == %d, want 0", got) |
| 355 |
} |
| 356 |
if got := ofz32r32(5); got != 0 { |
| 357 |
t.Errorf("0>>5 == %d, want 0", got) |
| 358 |
} |
| 359 |
if got := ofz32r16(5); got != 0 { |
| 360 |
t.Errorf("0>>5 == %d, want 0", got) |
| 361 |
} |
| 362 |
if got := ofz32r8(5); got != 0 { |
| 363 |
t.Errorf("0>>5 == %d, want 0", got) |
| 364 |
} |
| 365 |
if got := ofz32ur64(5); got != 0 { |
| 366 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 367 |
} |
| 368 |
if got := ofz32ur32(5); got != 0 { |
| 369 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 370 |
} |
| 371 |
if got := ofz32ur16(5); got != 0 { |
| 372 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 373 |
} |
| 374 |
if got := ofz32ur8(5); got != 0 { |
| 375 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 376 |
} |
| 377 |
|
| 378 |
if got := ofz16l64(5); got != 0 { |
| 379 |
t.Errorf("0<<5 == %d, want 0", got) |
| 380 |
} |
| 381 |
if got := ofz16l32(5); got != 0 { |
| 382 |
t.Errorf("0<<5 == %d, want 0", got) |
| 383 |
} |
| 384 |
if got := ofz16l16(5); got != 0 { |
| 385 |
t.Errorf("0<<5 == %d, want 0", got) |
| 386 |
} |
| 387 |
if got := ofz16l8(5); got != 0 { |
| 388 |
t.Errorf("0<<5 == %d, want 0", got) |
| 389 |
} |
| 390 |
if got := ofz16r64(5); got != 0 { |
| 391 |
t.Errorf("0>>5 == %d, want 0", got) |
| 392 |
} |
| 393 |
if got := ofz16r32(5); got != 0 { |
| 394 |
t.Errorf("0>>5 == %d, want 0", got) |
| 395 |
} |
| 396 |
if got := ofz16r16(5); got != 0 { |
| 397 |
t.Errorf("0>>5 == %d, want 0", got) |
| 398 |
} |
| 399 |
if got := ofz16r8(5); got != 0 { |
| 400 |
t.Errorf("0>>5 == %d, want 0", got) |
| 401 |
} |
| 402 |
if got := ofz16ur64(5); got != 0 { |
| 403 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 404 |
} |
| 405 |
if got := ofz16ur32(5); got != 0 { |
| 406 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 407 |
} |
| 408 |
if got := ofz16ur16(5); got != 0 { |
| 409 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 410 |
} |
| 411 |
if got := ofz16ur8(5); got != 0 { |
| 412 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 413 |
} |
| 414 |
|
| 415 |
if got := ofz8l64(5); got != 0 { |
| 416 |
t.Errorf("0<<5 == %d, want 0", got) |
| 417 |
} |
| 418 |
if got := ofz8l32(5); got != 0 { |
| 419 |
t.Errorf("0<<5 == %d, want 0", got) |
| 420 |
} |
| 421 |
if got := ofz8l16(5); got != 0 { |
| 422 |
t.Errorf("0<<5 == %d, want 0", got) |
| 423 |
} |
| 424 |
if got := ofz8l8(5); got != 0 { |
| 425 |
t.Errorf("0<<5 == %d, want 0", got) |
| 426 |
} |
| 427 |
if got := ofz8r64(5); got != 0 { |
| 428 |
t.Errorf("0>>5 == %d, want 0", got) |
| 429 |
} |
| 430 |
if got := ofz8r32(5); got != 0 { |
| 431 |
t.Errorf("0>>5 == %d, want 0", got) |
| 432 |
} |
| 433 |
if got := ofz8r16(5); got != 0 { |
| 434 |
t.Errorf("0>>5 == %d, want 0", got) |
| 435 |
} |
| 436 |
if got := ofz8r8(5); got != 0 { |
| 437 |
t.Errorf("0>>5 == %d, want 0", got) |
| 438 |
} |
| 439 |
if got := ofz8ur64(5); got != 0 { |
| 440 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 441 |
} |
| 442 |
if got := ofz8ur32(5); got != 0 { |
| 443 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 444 |
} |
| 445 |
if got := ofz8ur16(5); got != 0 { |
| 446 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 447 |
} |
| 448 |
if got := ofz8ur8(5); got != 0 { |
| 449 |
t.Errorf("0>>>5 == %d, want 0", got) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
//go:noinline |
| 454 |
func byz64l(n int64) int64 { |
| 455 |
return n << 0 |
| 456 |
} |
| 457 |
|
| 458 |
//go:noinline |
| 459 |
func byz64r(n int64) int64 { |
| 460 |
return n >> 0 |
| 461 |
} |
| 462 |
|
| 463 |
//go:noinline |
| 464 |
func byz64ur(n uint64) uint64 { |
| 465 |
return n >> 0 |
| 466 |
} |
| 467 |
|
| 468 |
//go:noinline |
| 469 |
func byz32l(n int32) int32 { |
| 470 |
return n << 0 |
| 471 |
} |
| 472 |
|
| 473 |
//go:noinline |
| 474 |
func byz32r(n int32) int32 { |
| 475 |
return n >> 0 |
| 476 |
} |
| 477 |
|
| 478 |
//go:noinline |
| 479 |
func byz32ur(n uint32) uint32 { |
| 480 |
return n >> 0 |
| 481 |
} |
| 482 |
|
| 483 |
//go:noinline |
| 484 |
func byz16l(n int16) int16 { |
| 485 |
return n << 0 |
| 486 |
} |
| 487 |
|
| 488 |
//go:noinline |
| 489 |
func byz16r(n int16) int16 { |
| 490 |
return n >> 0 |
| 491 |
} |
| 492 |
|
| 493 |
//go:noinline |
| 494 |
func byz16ur(n uint16) uint16 { |
| 495 |
return n >> 0 |
| 496 |
} |
| 497 |
|
| 498 |
//go:noinline |
| 499 |
func byz8l(n int8) int8 { |
| 500 |
return n << 0 |
| 501 |
} |
| 502 |
|
| 503 |
//go:noinline |
| 504 |
func byz8r(n int8) int8 { |
| 505 |
return n >> 0 |
| 506 |
} |
| 507 |
|
| 508 |
//go:noinline |
| 509 |
func byz8ur(n uint8) uint8 { |
| 510 |
return n >> 0 |
| 511 |
} |
| 512 |
|
| 513 |
func TestShiftByZero(t *testing.T) { |
| 514 |
{ |
| 515 |
var n int64 = 0x5555555555555555 |
| 516 |
if got := byz64l(n); got != n { |
| 517 |
t.Errorf("%x<<0 == %x, want %x", n, got, n) |
| 518 |
} |
| 519 |
if got := byz64r(n); got != n { |
| 520 |
t.Errorf("%x>>0 == %x, want %x", n, got, n) |
| 521 |
} |
| 522 |
} |
| 523 |
{ |
| 524 |
var n uint64 = 0xaaaaaaaaaaaaaaaa |
| 525 |
if got := byz64ur(n); got != n { |
| 526 |
t.Errorf("%x>>>0 == %x, want %x", n, got, n) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
{ |
| 531 |
var n int32 = 0x55555555 |
| 532 |
if got := byz32l(n); got != n { |
| 533 |
t.Errorf("%x<<0 == %x, want %x", n, got, n) |
| 534 |
} |
| 535 |
if got := byz32r(n); got != n { |
| 536 |
t.Errorf("%x>>0 == %x, want %x", n, got, n) |
| 537 |
} |
| 538 |
} |
| 539 |
{ |
| 540 |
var n uint32 = 0xaaaaaaaa |
| 541 |
if got := byz32ur(n); got != n { |
| 542 |
t.Errorf("%x>>>0 == %x, want %x", n, got, n) |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
{ |
| 547 |
var n int16 = 0x5555 |
| 548 |
if got := byz16l(n); got != n { |
| 549 |
t.Errorf("%x<<0 == %x, want %x", n, got, n) |
| 550 |
} |
| 551 |
if got := byz16r(n); got != n { |
| 552 |
t.Errorf("%x>>0 == %x, want %x", n, got, n) |
| 553 |
} |
| 554 |
} |
| 555 |
{ |
| 556 |
var n uint16 = 0xaaaa |
| 557 |
if got := byz16ur(n); got != n { |
| 558 |
t.Errorf("%x>>>0 == %x, want %x", n, got, n) |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
{ |
| 563 |
var n int8 = 0x55 |
| 564 |
if got := byz8l(n); got != n { |
| 565 |
t.Errorf("%x<<0 == %x, want %x", n, got, n) |
| 566 |
} |
| 567 |
if got := byz8r(n); got != n { |
| 568 |
t.Errorf("%x>>0 == %x, want %x", n, got, n) |
| 569 |
} |
| 570 |
} |
| 571 |
{ |
| 572 |
var n uint8 = 0x55 |
| 573 |
if got := byz8ur(n); got != n { |
| 574 |
t.Errorf("%x>>>0 == %x, want %x", n, got, n) |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
//go:noinline |
| 580 |
func two64l(x int64) int64 { |
| 581 |
return x << 1 << 1 |
| 582 |
} |
| 583 |
|
| 584 |
//go:noinline |
| 585 |
func two64r(x int64) int64 { |
| 586 |
return x >> 1 >> 1 |
| 587 |
} |
| 588 |
|
| 589 |
//go:noinline |
| 590 |
func two64ur(x uint64) uint64 { |
| 591 |
return x >> 1 >> 1 |
| 592 |
} |
| 593 |
|
| 594 |
//go:noinline |
| 595 |
func two32l(x int32) int32 { |
| 596 |
return x << 1 << 1 |
| 597 |
} |
| 598 |
|
| 599 |
//go:noinline |
| 600 |
func two32r(x int32) int32 { |
| 601 |
return x >> 1 >> 1 |
| 602 |
} |
| 603 |
|
| 604 |
//go:noinline |
| 605 |
func two32ur(x uint32) uint32 { |
| 606 |
return x >> 1 >> 1 |
| 607 |
} |
| 608 |
|
| 609 |
//go:noinline |
| 610 |
func two16l(x int16) int16 { |
| 611 |
return x << 1 << 1 |
| 612 |
} |
| 613 |
|
| 614 |
//go:noinline |
| 615 |
func two16r(x int16) int16 { |
| 616 |
return x >> 1 >> 1 |
| 617 |
} |
| 618 |
|
| 619 |
//go:noinline |
| 620 |
func two16ur(x uint16) uint16 { |
| 621 |
return x >> 1 >> 1 |
| 622 |
} |
| 623 |
|
| 624 |
//go:noinline |
| 625 |
func two8l(x int8) int8 { |
| 626 |
return x << 1 << 1 |
| 627 |
} |
| 628 |
|
| 629 |
//go:noinline |
| 630 |
func two8r(x int8) int8 { |
| 631 |
return x >> 1 >> 1 |
| 632 |
} |
| 633 |
|
| 634 |
//go:noinline |
| 635 |
func two8ur(x uint8) uint8 { |
| 636 |
return x >> 1 >> 1 |
| 637 |
} |
| 638 |
|
| 639 |
func TestShiftCombine(t *testing.T) { |
| 640 |
if got, want := two64l(4), int64(16); want != got { |
| 641 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 642 |
} |
| 643 |
if got, want := two64r(64), int64(16); want != got { |
| 644 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 645 |
} |
| 646 |
if got, want := two64ur(64), uint64(16); want != got { |
| 647 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 648 |
} |
| 649 |
if got, want := two32l(4), int32(16); want != got { |
| 650 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 651 |
} |
| 652 |
if got, want := two32r(64), int32(16); want != got { |
| 653 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 654 |
} |
| 655 |
if got, want := two32ur(64), uint32(16); want != got { |
| 656 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 657 |
} |
| 658 |
if got, want := two16l(4), int16(16); want != got { |
| 659 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 660 |
} |
| 661 |
if got, want := two16r(64), int16(16); want != got { |
| 662 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 663 |
} |
| 664 |
if got, want := two16ur(64), uint16(16); want != got { |
| 665 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 666 |
} |
| 667 |
if got, want := two8l(4), int8(16); want != got { |
| 668 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 669 |
} |
| 670 |
if got, want := two8r(64), int8(16); want != got { |
| 671 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 672 |
} |
| 673 |
if got, want := two8ur(64), uint8(16); want != got { |
| 674 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 675 |
} |
| 676 |
|
| 677 |
} |
| 678 |
|
| 679 |
//go:noinline |
| 680 |
func three64l(x int64) int64 { |
| 681 |
return x << 3 >> 1 << 2 |
| 682 |
} |
| 683 |
|
| 684 |
//go:noinline |
| 685 |
func three64ul(x uint64) uint64 { |
| 686 |
return x << 3 >> 1 << 2 |
| 687 |
} |
| 688 |
|
| 689 |
//go:noinline |
| 690 |
func three64r(x int64) int64 { |
| 691 |
return x >> 3 << 1 >> 2 |
| 692 |
} |
| 693 |
|
| 694 |
//go:noinline |
| 695 |
func three64ur(x uint64) uint64 { |
| 696 |
return x >> 3 << 1 >> 2 |
| 697 |
} |
| 698 |
|
| 699 |
//go:noinline |
| 700 |
func three32l(x int32) int32 { |
| 701 |
return x << 3 >> 1 << 2 |
| 702 |
} |
| 703 |
|
| 704 |
//go:noinline |
| 705 |
func three32ul(x uint32) uint32 { |
| 706 |
return x << 3 >> 1 << 2 |
| 707 |
} |
| 708 |
|
| 709 |
//go:noinline |
| 710 |
func three32r(x int32) int32 { |
| 711 |
return x >> 3 << 1 >> 2 |
| 712 |
} |
| 713 |
|
| 714 |
//go:noinline |
| 715 |
func three32ur(x uint32) uint32 { |
| 716 |
return x >> 3 << 1 >> 2 |
| 717 |
} |
| 718 |
|
| 719 |
//go:noinline |
| 720 |
func three16l(x int16) int16 { |
| 721 |
return x << 3 >> 1 << 2 |
| 722 |
} |
| 723 |
|
| 724 |
//go:noinline |
| 725 |
func three16ul(x uint16) uint16 { |
| 726 |
return x << 3 >> 1 << 2 |
| 727 |
} |
| 728 |
|
| 729 |
//go:noinline |
| 730 |
func three16r(x int16) int16 { |
| 731 |
return x >> 3 << 1 >> 2 |
| 732 |
} |
| 733 |
|
| 734 |
//go:noinline |
| 735 |
func three16ur(x uint16) uint16 { |
| 736 |
return x >> 3 << 1 >> 2 |
| 737 |
} |
| 738 |
|
| 739 |
//go:noinline |
| 740 |
func three8l(x int8) int8 { |
| 741 |
return x << 3 >> 1 << 2 |
| 742 |
} |
| 743 |
|
| 744 |
//go:noinline |
| 745 |
func three8ul(x uint8) uint8 { |
| 746 |
return x << 3 >> 1 << 2 |
| 747 |
} |
| 748 |
|
| 749 |
//go:noinline |
| 750 |
func three8r(x int8) int8 { |
| 751 |
return x >> 3 << 1 >> 2 |
| 752 |
} |
| 753 |
|
| 754 |
//go:noinline |
| 755 |
func three8ur(x uint8) uint8 { |
| 756 |
return x >> 3 << 1 >> 2 |
| 757 |
} |
| 758 |
|
| 759 |
func TestShiftCombine3(t *testing.T) { |
| 760 |
if got, want := three64l(4), int64(64); want != got { |
| 761 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 762 |
} |
| 763 |
if got, want := three64ul(4), uint64(64); want != got { |
| 764 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 765 |
} |
| 766 |
if got, want := three64r(64), int64(4); want != got { |
| 767 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 768 |
} |
| 769 |
if got, want := three64ur(64), uint64(4); want != got { |
| 770 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 771 |
} |
| 772 |
if got, want := three32l(4), int32(64); want != got { |
| 773 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 774 |
} |
| 775 |
if got, want := three32ul(4), uint32(64); want != got { |
| 776 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 777 |
} |
| 778 |
if got, want := three32r(64), int32(4); want != got { |
| 779 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 780 |
} |
| 781 |
if got, want := three32ur(64), uint32(4); want != got { |
| 782 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 783 |
} |
| 784 |
if got, want := three16l(4), int16(64); want != got { |
| 785 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 786 |
} |
| 787 |
if got, want := three16ul(4), uint16(64); want != got { |
| 788 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 789 |
} |
| 790 |
if got, want := three16r(64), int16(4); want != got { |
| 791 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 792 |
} |
| 793 |
if got, want := three16ur(64), uint16(4); want != got { |
| 794 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 795 |
} |
| 796 |
if got, want := three8l(4), int8(64); want != got { |
| 797 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 798 |
} |
| 799 |
if got, want := three8ul(4), uint8(64); want != got { |
| 800 |
t.Errorf("4<<1<<1 == %d, want %d", got, want) |
| 801 |
} |
| 802 |
if got, want := three8r(64), int8(4); want != got { |
| 803 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 804 |
} |
| 805 |
if got, want := three8ur(64), uint8(4); want != got { |
| 806 |
t.Errorf("64>>1>>1 == %d, want %d", got, want) |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
var ( |
| 811 |
one64 int64 = 1 |
| 812 |
one64u uint64 = 1 |
| 813 |
one32 int32 = 1 |
| 814 |
one32u uint32 = 1 |
| 815 |
one16 int16 = 1 |
| 816 |
one16u uint16 = 1 |
| 817 |
one8 int8 = 1 |
| 818 |
one8u uint8 = 1 |
| 819 |
) |
| 820 |
|
| 821 |
func TestShiftLargeCombine(t *testing.T) { |
| 822 |
var N uint64 = 0x8000000000000000 |
| 823 |
if one64<<N<<N == 1 { |
| 824 |
t.Errorf("shift overflow mishandled") |
| 825 |
} |
| 826 |
if one64>>N>>N == 1 { |
| 827 |
t.Errorf("shift overflow mishandled") |
| 828 |
} |
| 829 |
if one64u>>N>>N == 1 { |
| 830 |
t.Errorf("shift overflow mishandled") |
| 831 |
} |
| 832 |
if one32<<N<<N == 1 { |
| 833 |
t.Errorf("shift overflow mishandled") |
| 834 |
} |
| 835 |
if one32>>N>>N == 1 { |
| 836 |
t.Errorf("shift overflow mishandled") |
| 837 |
} |
| 838 |
if one32u>>N>>N == 1 { |
| 839 |
t.Errorf("shift overflow mishandled") |
| 840 |
} |
| 841 |
if one16<<N<<N == 1 { |
| 842 |
t.Errorf("shift overflow mishandled") |
| 843 |
} |
| 844 |
if one16>>N>>N == 1 { |
| 845 |
t.Errorf("shift overflow mishandled") |
| 846 |
} |
| 847 |
if one16u>>N>>N == 1 { |
| 848 |
t.Errorf("shift overflow mishandled") |
| 849 |
} |
| 850 |
if one8<<N<<N == 1 { |
| 851 |
t.Errorf("shift overflow mishandled") |
| 852 |
} |
| 853 |
if one8>>N>>N == 1 { |
| 854 |
t.Errorf("shift overflow mishandled") |
| 855 |
} |
| 856 |
if one8u>>N>>N == 1 { |
| 857 |
t.Errorf("shift overflow mishandled") |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
func TestShiftLargeCombine3(t *testing.T) { |
| 862 |
var N uint64 = 0x8000000000000001 |
| 863 |
if one64<<N>>2<<N == 1 { |
| 864 |
t.Errorf("shift overflow mishandled") |
| 865 |
} |
| 866 |
if one64u<<N>>2<<N == 1 { |
| 867 |
t.Errorf("shift overflow mishandled") |
| 868 |
} |
| 869 |
if one64>>N<<2>>N == 1 { |
| 870 |
t.Errorf("shift overflow mishandled") |
| 871 |
} |
| 872 |
if one64u>>N<<2>>N == 1 { |
| 873 |
t.Errorf("shift overflow mishandled") |
| 874 |
} |
| 875 |
if one32<<N>>2<<N == 1 { |
| 876 |
t.Errorf("shift overflow mishandled") |
| 877 |
} |
| 878 |
if one32u<<N>>2<<N == 1 { |
| 879 |
t.Errorf("shift overflow mishandled") |
| 880 |
} |
| 881 |
if one32>>N<<2>>N == 1 { |
| 882 |
t.Errorf("shift overflow mishandled") |
| 883 |
} |
| 884 |
if one32u>>N<<2>>N == 1 { |
| 885 |
t.Errorf("shift overflow mishandled") |
| 886 |
} |
| 887 |
if one16<<N>>2<<N == 1 { |
| 888 |
t.Errorf("shift overflow mishandled") |
| 889 |
} |
| 890 |
if one16u<<N>>2<<N == 1 { |
| 891 |
t.Errorf("shift overflow mishandled") |
| 892 |
} |
| 893 |
if one16>>N<<2>>N == 1 { |
| 894 |
t.Errorf("shift overflow mishandled") |
| 895 |
} |
| 896 |
if one16u>>N<<2>>N == 1 { |
| 897 |
t.Errorf("shift overflow mishandled") |
| 898 |
} |
| 899 |
if one8<<N>>2<<N == 1 { |
| 900 |
t.Errorf("shift overflow mishandled") |
| 901 |
} |
| 902 |
if one8u<<N>>2<<N == 1 { |
| 903 |
t.Errorf("shift overflow mishandled") |
| 904 |
} |
| 905 |
if one8>>N<<2>>N == 1 { |
| 906 |
t.Errorf("shift overflow mishandled") |
| 907 |
} |
| 908 |
if one8u>>N<<2>>N == 1 { |
| 909 |
t.Errorf("shift overflow mishandled") |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
func TestShiftGeneric(t *testing.T) { |
| 914 |
for _, test := range [...]struct { |
| 915 |
valueWidth int |
| 916 |
signed bool |
| 917 |
shiftWidth int |
| 918 |
left bool |
| 919 |
f interface{} |
| 920 |
}{ |
| 921 |
{64, true, 64, true, func(n int64, s uint64) int64 { return n << s }}, |
| 922 |
{64, true, 64, false, func(n int64, s uint64) int64 { return n >> s }}, |
| 923 |
{64, false, 64, false, func(n uint64, s uint64) uint64 { return n >> s }}, |
| 924 |
{64, true, 32, true, func(n int64, s uint32) int64 { return n << s }}, |
| 925 |
{64, true, 32, false, func(n int64, s uint32) int64 { return n >> s }}, |
| 926 |
{64, false, 32, false, func(n uint64, s uint32) uint64 { return n >> s }}, |
| 927 |
{64, true, 16, true, func(n int64, s uint16) int64 { return n << s }}, |
| 928 |
{64, true, 16, false, func(n int64, s uint16) int64 { return n >> s }}, |
| 929 |
{64, false, 16, false, func(n uint64, s uint16) uint64 { return n >> s }}, |
| 930 |
{64, true, 8, true, func(n int64, s uint8) int64 { return n << s }}, |
| 931 |
{64, true, 8, false, func(n int64, s uint8) int64 { return n >> s }}, |
| 932 |
{64, false, 8, false, func(n uint64, s uint8) uint64 { return n >> s }}, |
| 933 |
|
| 934 |
{32, true, 64, true, func(n int32, s uint64) int32 { return n << s }}, |
| 935 |
{32, true, 64, false, func(n int32, s uint64) int32 { return n >> s }}, |
| 936 |
{32, false, 64, false, func(n uint32, s uint64) uint32 { return n >> s }}, |
| 937 |
{32, true, 32, true, func(n int32, s uint32) int32 { return n << s }}, |
| 938 |
{32, true, 32, false, func(n int32, s uint32) int32 { return n >> s }}, |
| 939 |
{32, false, 32, false, func(n uint32, s uint32) uint32 { return n >> s }}, |
| 940 |
{32, true, 16, true, func(n int32, s uint16) int32 { return n << s }}, |
| 941 |
{32, true, 16, false, func(n int32, s uint16) int32 { return n >> s }}, |
| 942 |
{32, false, 16, false, func(n uint32, s uint16) uint32 { return n >> s }}, |
| 943 |
{32, true, 8, true, func(n int32, s uint8) int32 { return n << s }}, |
| 944 |
{32, true, 8, false, func(n int32, s uint8) int32 { return n >> s }}, |
| 945 |
{32, false, 8, false, func(n uint32, s uint8) uint32 { return n >> s }}, |
| 946 |
|
| 947 |
{16, true, 64, true, func(n int16, s uint64) int16 { return n << s }}, |
| 948 |
{16, true, 64, false, func(n int16, s uint64) int16 { return n >> s }}, |
| 949 |
{16, false, 64, false, func(n uint16, s uint64) uint16 { return n >> s }}, |
| 950 |
{16, true, 32, true, func(n int16, s uint32) int16 { return n << s }}, |
| 951 |
{16, true, 32, false, func(n int16, s uint32) int16 { return n >> s }}, |
| 952 |
{16, false, 32, false, func(n uint16, s uint32) uint16 { return n >> s }}, |
| 953 |
{16, true, 16, true, func(n int16, s uint16) int16 { return n << s }}, |
| 954 |
{16, true, 16, false, func(n int16, s uint16) int16 { return n >> s }}, |
| 955 |
{16, false, 16, false, func(n uint16, s uint16) uint16 { return n >> s }}, |
| 956 |
{16, true, 8, true, func(n int16, s uint8) int16 { return n << s }}, |
| 957 |
{16, true, 8, false, func(n int16, s uint8) int16 { return n >> s }}, |
| 958 |
{16, false, 8, false, func(n uint16, s uint8) uint16 { return n >> s }}, |
| 959 |
|
| 960 |
{8, true, 64, true, func(n int8, s uint64) int8 { return n << s }}, |
| 961 |
{8, true, 64, false, func(n int8, s uint64) int8 { return n >> s }}, |
| 962 |
{8, false, 64, false, func(n uint8, s uint64) uint8 { return n >> s }}, |
| 963 |
{8, true, 32, true, func(n int8, s uint32) int8 { return n << s }}, |
| 964 |
{8, true, 32, false, func(n int8, s uint32) int8 { return n >> s }}, |
| 965 |
{8, false, 32, false, func(n uint8, s uint32) uint8 { return n >> s }}, |
| 966 |
{8, true, 16, true, func(n int8, s uint16) int8 { return n << s }}, |
| 967 |
{8, true, 16, false, func(n int8, s uint16) int8 { return n >> s }}, |
| 968 |
{8, false, 16, false, func(n uint8, s uint16) uint8 { return n >> s }}, |
| 969 |
{8, true, 8, true, func(n int8, s uint8) int8 { return n << s }}, |
| 970 |
{8, true, 8, false, func(n int8, s uint8) int8 { return n >> s }}, |
| 971 |
{8, false, 8, false, func(n uint8, s uint8) uint8 { return n >> s }}, |
| 972 |
} { |
| 973 |
fv := reflect.ValueOf(test.f) |
| 974 |
var args [2]reflect.Value |
| 975 |
for i := 0; i < test.valueWidth; i++ { |
| 976 |
// Build value to be shifted. |
| 977 |
var n int64 = 1 |
| 978 |
for j := 0; j < i; j++ { |
| 979 |
n <<= 1 |
| 980 |
} |
| 981 |
args[0] = reflect.ValueOf(n).Convert(fv.Type().In(0)) |
| 982 |
for s := 0; s <= test.shiftWidth; s++ { |
| 983 |
args[1] = reflect.ValueOf(s).Convert(fv.Type().In(1)) |
| 984 |
|
| 985 |
// Compute desired result. We're testing variable shifts |
| 986 |
// assuming constant shifts are correct. |
| 987 |
r := n |
| 988 |
var op string |
| 989 |
switch { |
| 990 |
case test.left: |
| 991 |
op = "<<" |
| 992 |
for j := 0; j < s; j++ { |
| 993 |
r <<= 1 |
| 994 |
} |
| 995 |
switch test.valueWidth { |
| 996 |
case 32: |
| 997 |
r = int64(int32(r)) |
| 998 |
case 16: |
| 999 |
r = int64(int16(r)) |
| 1000 |
case 8: |
| 1001 |
r = int64(int8(r)) |
| 1002 |
} |
| 1003 |
case test.signed: |
| 1004 |
op = ">>" |
| 1005 |
switch test.valueWidth { |
| 1006 |
case 32: |
| 1007 |
r = int64(int32(r)) |
| 1008 |
case 16: |
| 1009 |
r = int64(int16(r)) |
| 1010 |
case 8: |
| 1011 |
r = int64(int8(r)) |
| 1012 |
} |
| 1013 |
for j := 0; j < s; j++ { |
| 1014 |
r >>= 1 |
| 1015 |
} |
| 1016 |
default: |
| 1017 |
op = ">>>" |
| 1018 |
for j := 0; j < s; j++ { |
| 1019 |
r = int64(uint64(r) >> 1) |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
// Call function. |
| 1024 |
res := fv.Call(args[:])[0].Convert(reflect.ValueOf(r).Type()) |
| 1025 |
|
| 1026 |
if res.Int() != r { |
| 1027 |
t.Errorf("%s%dx%d(%x,%x)=%x, want %x", op, test.valueWidth, test.shiftWidth, n, s, res.Int(), r) |
| 1028 |
} |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
var shiftSink64 int64 |
| 1035 |
|
| 1036 |
func BenchmarkShiftArithmeticRight(b *testing.B) { |
| 1037 |
x := shiftSink64 |
| 1038 |
for i := 0; i < b.N; i++ { |
| 1039 |
x = x >> (i & 63) |
| 1040 |
} |
| 1041 |
shiftSink64 = x |
| 1042 |
} |
| 1043 |
|
| 1044 |
//go:noinline |
| 1045 |
func incorrectRotate1(x, c uint64) uint64 { |
| 1046 |
// This should not compile to a rotate instruction. |
| 1047 |
return x<<c | x>>(64-c) |
| 1048 |
} |
| 1049 |
|
| 1050 |
//go:noinline |
| 1051 |
func incorrectRotate2(x uint64) uint64 { |
| 1052 |
var c uint64 = 66 |
| 1053 |
// This should not compile to a rotate instruction. |
| 1054 |
return x<<c | x>>(64-c) |
| 1055 |
} |
| 1056 |
|
| 1057 |
func TestIncorrectRotate(t *testing.T) { |
| 1058 |
if got := incorrectRotate1(1, 66); got != 0 { |
| 1059 |
t.Errorf("got %x want 0", got) |
| 1060 |
} |
| 1061 |
if got := incorrectRotate2(1); got != 0 { |
| 1062 |
t.Errorf("got %x want 0", got) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
//go:noinline |
| 1067 |
func variableShiftOverflow64x8(x int64, y, z uint8) (a, b, c int64) { |
| 1068 |
// Verify junk bits are ignored when doing a variable shift. |
| 1069 |
return x >> (y + z), x << (y + z), int64(uint64(x) >> (y + z)) |
| 1070 |
} |
| 1071 |
|
| 1072 |
//go:noinline |
| 1073 |
func variableShiftOverflow32x8(x int32, y, z uint8) (a, b, c int32) { |
| 1074 |
// Verify junk bits are ignored when doing a variable shift. |
| 1075 |
return x >> (y + z), x << (y + z), int32(uint32(x) >> (y + z)) |
| 1076 |
} |
| 1077 |
|
| 1078 |
//go:noinline |
| 1079 |
func variableShiftOverflow16x8(x int16, y, z uint8) (a, b, c int16) { |
| 1080 |
// Verify junk bits are ignored when doing a variable shift. |
| 1081 |
return x >> (y + z), x << (y + z), int16(uint16(x) >> (y + z)) |
| 1082 |
} |
| 1083 |
|
| 1084 |
//go:noinline |
| 1085 |
func variableShiftOverflow8x8(x int8, y, z uint8) (a, b, c int8) { |
| 1086 |
// Verify junk bits are ignored when doing a variable shift. |
| 1087 |
return x >> (y + z), x << (y + z), int8(uint8(x) >> (y + z)) |
| 1088 |
} |
| 1089 |
|
| 1090 |
//go:noinline |
| 1091 |
func variableShiftOverflow64x16(x int64, y, z uint16) (a, b, c int64) { |
| 1092 |
// Verify junk bits are ignored when doing a variable shift. |
| 1093 |
return x >> (y + z), x << (y + z), int64(uint64(x) >> (y + z)) |
| 1094 |
} |
| 1095 |
|
| 1096 |
//go:noinline |
| 1097 |
func variableShiftOverflow32x16(x int32, y, z uint16) (a, b, c int32) { |
| 1098 |
// Verify junk bits are ignored when doing a variable shift. |
| 1099 |
return x >> (y + z), x << (y + z), int32(uint32(x) >> (y + z)) |
| 1100 |
} |
| 1101 |
|
| 1102 |
//go:noinline |
| 1103 |
func variableShiftOverflow16x16(x int16, y, z uint16) (a, b, c int16) { |
| 1104 |
// Verify junk bits are ignored when doing a variable shift. |
| 1105 |
return x >> (y + z), x << (y + z), int16(uint16(x) >> (y + z)) |
| 1106 |
} |
| 1107 |
|
| 1108 |
//go:noinline |
| 1109 |
func variableShiftOverflow8x16(x int8, y, z uint16) (a, b, c int8) { |
| 1110 |
// Verify junk bits are ignored when doing a variable shift. |
| 1111 |
return x >> (y + z), x << (y + z), int8(uint8(x) >> (y + z)) |
| 1112 |
} |
| 1113 |
|
| 1114 |
//go:noinline |
| 1115 |
func makeU8(x uint64) uint8 { |
| 1116 |
// Ensure the upper portions of the register are clear before testing large shift values |
| 1117 |
// using non-native types (e.g uint8 on PPC64). |
| 1118 |
return uint8(x) |
| 1119 |
} |
| 1120 |
|
| 1121 |
//go:noinline |
| 1122 |
func makeU16(x uint64) uint16 { |
| 1123 |
// Ensure the upper portions of the register are clear before testing large shift values |
| 1124 |
// using non-native types (e.g uint8 on PPC64). |
| 1125 |
return uint16(x) |
| 1126 |
} |
| 1127 |
|
| 1128 |
func TestShiftOverflow(t *testing.T) { |
| 1129 |
if v, w, z := variableShiftOverflow64x8(-64, makeU8(255), 2); v != -32 || w != -128 || z != 0x7fffffffffffffe0 { |
| 1130 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fffffffffffffe0", v, w, z) |
| 1131 |
} |
| 1132 |
if v, w, z := variableShiftOverflow32x8(-64, makeU8(255), 2); v != -32 || w != -128 || z != 0x7fffffe0 { |
| 1133 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fffffe0", v, w, z) |
| 1134 |
} |
| 1135 |
if v, w, z := variableShiftOverflow16x8(-64, makeU8(255), 2); v != -32 || w != -128 || z != 0x7fe0 { |
| 1136 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fe0", v, w, z) |
| 1137 |
} |
| 1138 |
if v, w, z := variableShiftOverflow8x8(-64, makeU8(255), 2); v != -32 || w != -128 || z != 0x60 { |
| 1139 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x60", v, w, z) |
| 1140 |
} |
| 1141 |
if v, w, z := variableShiftOverflow64x16(-64, makeU16(0xffff), 2); v != -32 || w != -128 || z != 0x7fffffffffffffe0 { |
| 1142 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fffffffffffffe0", v, w, z) |
| 1143 |
} |
| 1144 |
if v, w, z := variableShiftOverflow32x16(-64, makeU16(0xffff), 2); v != -32 || w != -128 || z != 0x7fffffe0 { |
| 1145 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fffffe0,", v, w, z) |
| 1146 |
} |
| 1147 |
if v, w, z := variableShiftOverflow16x16(-64, makeU16(0xffff), 2); v != -32 || w != -128 || z != 0x7fe0 { |
| 1148 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x7fe0", v, w, z) |
| 1149 |
} |
| 1150 |
if v, w, z := variableShiftOverflow8x16(-64, makeU16(0xffff), 2); v != -32 || w != -128 || z != 0x60 { |
| 1151 |
t.Errorf("got %d %d 0x%x, expected -32 -128 0x60", v, w, z) |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
//go:noinline |
| 1156 |
func lsh64s(n int64, k int) int64 { |
| 1157 |
return n << k |
| 1158 |
} |
| 1159 |
|
| 1160 |
//go:noinline |
| 1161 |
func lsh64u(n uint64, k int) uint64 { |
| 1162 |
return n << k |
| 1163 |
} |
| 1164 |
|
| 1165 |
//go:noinline |
| 1166 |
func lsh32s(n int32, k int) int32 { |
| 1167 |
return n << k |
| 1168 |
} |
| 1169 |
|
| 1170 |
//go:noinline |
| 1171 |
func lsh32u(n uint32, k int) uint32 { |
| 1172 |
return n << k |
| 1173 |
} |
| 1174 |
|
| 1175 |
//go:noinline |
| 1176 |
func lsh16s(n int16, k int) int16 { |
| 1177 |
return n << k |
| 1178 |
} |
| 1179 |
|
| 1180 |
//go:noinline |
| 1181 |
func lsh16u(n uint16, k int) uint16 { |
| 1182 |
return n << k |
| 1183 |
} |
| 1184 |
|
| 1185 |
//go:noinline |
| 1186 |
func lsh8s(n int8, k int) int8 { |
| 1187 |
return n << k |
| 1188 |
} |
| 1189 |
|
| 1190 |
//go:noinline |
| 1191 |
func lsh8u(n uint8, k int) uint8 { |
| 1192 |
return n << k |
| 1193 |
} |
| 1194 |
|
| 1195 |
//go:noinline |
| 1196 |
func rsh64s(n int64, k int) int64 { |
| 1197 |
return n >> k |
| 1198 |
} |
| 1199 |
|
| 1200 |
//go:noinline |
| 1201 |
func rsh64u(n uint64, k int) uint64 { |
| 1202 |
return n >> k |
| 1203 |
} |
| 1204 |
|
| 1205 |
//go:noinline |
| 1206 |
func rsh32s(n int32, k int) int32 { |
| 1207 |
return n >> k |
| 1208 |
} |
| 1209 |
|
| 1210 |
//go:noinline |
| 1211 |
func rsh32u(n uint32, k int) uint32 { |
| 1212 |
return n >> k |
| 1213 |
} |
| 1214 |
|
| 1215 |
//go:noinline |
| 1216 |
func rsh16s(n int16, k int) int16 { |
| 1217 |
return n >> k |
| 1218 |
} |
| 1219 |
|
| 1220 |
//go:noinline |
| 1221 |
func rsh16u(n uint16, k int) uint16 { |
| 1222 |
return n >> k |
| 1223 |
} |
| 1224 |
|
| 1225 |
//go:noinline |
| 1226 |
func rsh8s(n int8, k int) int8 { |
| 1227 |
return n >> k |
| 1228 |
} |
| 1229 |
|
| 1230 |
//go:noinline |
| 1231 |
func rsh8u(n uint8, k int) uint8 { |
| 1232 |
return n >> k |
| 1233 |
} |
| 1234 |
|
| 1235 |
func TestOverShiftLeft(t *testing.T) { |
| 1236 |
for _, f := range []reflect.Value{ |
| 1237 |
reflect.ValueOf(lsh64s), |
| 1238 |
reflect.ValueOf(lsh64u), |
| 1239 |
reflect.ValueOf(lsh32s), |
| 1240 |
reflect.ValueOf(lsh32u), |
| 1241 |
reflect.ValueOf(lsh16s), |
| 1242 |
reflect.ValueOf(lsh16u), |
| 1243 |
reflect.ValueOf(lsh8s), |
| 1244 |
reflect.ValueOf(lsh8u), |
| 1245 |
} { |
| 1246 |
typ := f.Type().In(0) // type of input/output |
| 1247 |
one := reflect.ValueOf(1).Convert(typ) |
| 1248 |
zero := reflect.ValueOf(0).Convert(typ).Interface() |
| 1249 |
for k := 0; k < 100; k++ { |
| 1250 |
got := f.Call([]reflect.Value{one, reflect.ValueOf(k)})[0].Interface() |
| 1251 |
if k >= int(typ.Size()*8) { |
| 1252 |
if got != zero { |
| 1253 |
t.Errorf("shifted to zero prematurely: %s %d %v", typ, k, got) |
| 1254 |
} |
| 1255 |
} else { |
| 1256 |
if got == zero { |
| 1257 |
t.Errorf("shift doesn't result in zero: %s %d %v", typ, k, got) |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
func TestOverShiftRightU(t *testing.T) { |
| 1265 |
for _, f := range []reflect.Value{ |
| 1266 |
reflect.ValueOf(rsh64u), |
| 1267 |
reflect.ValueOf(rsh32u), |
| 1268 |
reflect.ValueOf(rsh16u), |
| 1269 |
reflect.ValueOf(rsh8u), |
| 1270 |
} { |
| 1271 |
typ := f.Type().In(0) // type of input/output |
| 1272 |
max := reflect.ValueOf(uint64(1) << (typ.Size()*8 - 1)).Convert(typ) |
| 1273 |
zero := reflect.ValueOf(0).Convert(typ).Interface() |
| 1274 |
for k := 0; k < 100; k++ { |
| 1275 |
got := f.Call([]reflect.Value{max, reflect.ValueOf(k)})[0].Interface() |
| 1276 |
if k >= int(typ.Size()*8) { |
| 1277 |
if got != zero { |
| 1278 |
t.Errorf("shifted to zero prematurely: %s %d %v", typ, k, got) |
| 1279 |
} |
| 1280 |
} else { |
| 1281 |
if got == zero { |
| 1282 |
t.Errorf("shift doesn't result in zero: %s %d %v", typ, k, got) |
| 1283 |
} |
| 1284 |
} |
| 1285 |
} |
| 1286 |
} |
| 1287 |
} |
| 1288 |
func TestOverShiftRightS(t *testing.T) { |
| 1289 |
for _, f := range []reflect.Value{ |
| 1290 |
reflect.ValueOf(rsh64s), |
| 1291 |
reflect.ValueOf(rsh32s), |
| 1292 |
reflect.ValueOf(rsh16s), |
| 1293 |
reflect.ValueOf(rsh8s), |
| 1294 |
} { |
| 1295 |
typ := f.Type().In(0) // type of input/output |
| 1296 |
maxInt := reflect.ValueOf(int64(1)<<(typ.Size()*8-1) - 1).Convert(typ) |
| 1297 |
zero := reflect.ValueOf(0).Convert(typ).Interface() |
| 1298 |
for k := 0; k < 100; k++ { |
| 1299 |
got := f.Call([]reflect.Value{maxInt, reflect.ValueOf(k)})[0].Interface() |
| 1300 |
if k < int(typ.Size()*8)-1 { |
| 1301 |
if got == zero { |
| 1302 |
t.Errorf("shifted to zero prematurely: %s %d %v", typ, k, got) |
| 1303 |
} |
| 1304 |
} else { |
| 1305 |
if got != zero { |
| 1306 |
t.Errorf("shift doesn't result in zero: %s %d %v", typ, k, got) |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
minInt := reflect.ValueOf(int64(1) << (typ.Size()*8 - 1)).Convert(typ) |
| 1311 |
negOne := reflect.ValueOf(-1).Convert(typ).Interface() |
| 1312 |
for k := 0; k < 100; k++ { |
| 1313 |
got := f.Call([]reflect.Value{minInt, reflect.ValueOf(k)})[0].Interface() |
| 1314 |
if k < int(typ.Size()*8)-1 { |
| 1315 |
if got == negOne { |
| 1316 |
t.Errorf("shifted to negative one prematurely: %s %d %v", typ, k, got) |
| 1317 |
} |
| 1318 |
} else { |
| 1319 |
if got != negOne { |
| 1320 |
t.Errorf("shift doesn't result in negative one: %s %d %v", typ, k, got) |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
func TestNegShifts(t *testing.T) { |
| 1328 |
for i := 0; i < bits.UintSize; i++ { |
| 1329 |
k := (-1) << i |
| 1330 |
shouldPanic(func() { lsh64s(0, k) }) |
| 1331 |
shouldPanic(func() { lsh64u(0, k) }) |
| 1332 |
shouldPanic(func() { lsh32s(0, k) }) |
| 1333 |
shouldPanic(func() { lsh32u(0, k) }) |
| 1334 |
shouldPanic(func() { lsh16s(0, k) }) |
| 1335 |
shouldPanic(func() { lsh16u(0, k) }) |
| 1336 |
shouldPanic(func() { lsh8s(0, k) }) |
| 1337 |
shouldPanic(func() { lsh8u(0, k) }) |
| 1338 |
shouldPanic(func() { rsh64s(0, k) }) |
| 1339 |
shouldPanic(func() { rsh64u(0, k) }) |
| 1340 |
shouldPanic(func() { rsh32s(0, k) }) |
| 1341 |
shouldPanic(func() { rsh32u(0, k) }) |
| 1342 |
shouldPanic(func() { rsh16s(0, k) }) |
| 1343 |
shouldPanic(func() { rsh16u(0, k) }) |
| 1344 |
shouldPanic(func() { rsh8s(0, k) }) |
| 1345 |
shouldPanic(func() { rsh8u(0, k) }) |
| 1346 |
} |
| 1347 |
} |
| 1348 |
func shouldPanic(f func()) { |
| 1349 |
defer func() { |
| 1350 |
if recover() == nil { |
| 1351 |
panic("not panicking") |
| 1352 |
} |
| 1353 |
}() |
| 1354 |
f() |
| 1355 |
} |