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
5package main
6
7import (
8	"fmt"
9	T "runtime/internal/sys"
10)
11
12var A = []uint64{0x0102030405060708, 0x1122334455667788}
13var B = []uint64{0x0807060504030201, 0x8877665544332211}
14
15var errors int
16
17func logf(f string, args ...interface{}) {
18	errors++
19	fmt.Printf(f, args...)
20	if errors > 100 { // 100 is enough spewage
21		panic("100 errors is plenty is enough")
22	}
23}
24
25func test(i int, x uint64) {
26	t := T.TrailingZeros64(x) // ERROR "intrinsic substitution for TrailingZeros64"
27	if i != t {
28		logf("TrailingZeros64(0x%x) expected %d but got %d\n", x, i, t)
29	}
30	x = -x
31	t = T.TrailingZeros64(x) // ERROR "intrinsic substitution for TrailingZeros64"
32	if i != t {
33		logf("TrailingZeros64(0x%x) expected %d but got %d\n", x, i, t)
34	}
35
36	if i <= 32 {
37		x32 := uint32(x)
38		t32 := T.TrailingZeros32(x32) // ERROR "intrinsic substitution for TrailingZeros32"
39		if i != t32 {
40			logf("TrailingZeros32(0x%x) expected %d but got %d\n", x32, i, t32)
41		}
42		x32 = -x32
43		t32 = T.TrailingZeros32(x32) // ERROR "intrinsic substitution for TrailingZeros32"
44		if i != t32 {
45			logf("TrailingZeros32(0x%x) expected %d but got %d\n", x32, i, t32)
46		}
47	}
48}
49
50func main() {
51	// Test Bswap first because the other test relies on it
52	// working correctly (to implement bit reversal).
53	for i := range A {
54		x := A[i]
55		y := B[i]
56		X := T.Bswap64(x) // ERROR "intrinsic substitution for Bswap64"
57		Y := T.Bswap64(y) // ERROR "intrinsic substitution for Bswap64"
58		if y != X {
59			logf("Bswap64(0x%08x) expected 0x%08x but got 0x%08x\n", x, y, X)
60		}
61		if x != Y {
62			logf("Bswap64(0x%08x) expected 0x%08x but got 0x%08x\n", y, x, Y)
63		}
64
65		x32 := uint32(X)
66		y32 := uint32(Y >> 32)
67
68		X32 := T.Bswap32(x32) // ERROR "intrinsic substitution for Bswap32"
69		Y32 := T.Bswap32(y32) // ERROR "intrinsic substitution for Bswap32"
70		if y32 != X32 {
71			logf("Bswap32(0x%08x) expected 0x%08x but got 0x%08x\n", x32, y32, X32)
72		}
73		if x32 != Y32 {
74			logf("Bswap32(0x%08x) expected 0x%08x but got 0x%08x\n", y32, x32, Y32)
75		}
76	}
77
78	// Zero is a special case, be sure it is done right.
79	if T.TrailingZeros32(0) != 32 { // ERROR "intrinsic substitution for TrailingZeros32"
80		logf("TrailingZeros32(0) != 32")
81	}
82	if T.TrailingZeros64(0) != 64 { // ERROR "intrinsic substitution for TrailingZeros64"
83		logf("TrailingZeros64(0) != 64")
84	}
85
86	for i := 0; i <= 64; i++ {
87		for j := uint64(1); j <= 255; j += 2 {
88			for k := uint64(1); k <= 65537; k += 128 {
89				x := (j * k) << uint(i)
90				test(i, x)
91			}
92		}
93	}
94}
95