...
Run Format

Source file src/math/bits/bits_test.go

Documentation: math/bits

     1  // Copyright 2017 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 bits_test
     6  
     7  import (
     8  	. "math/bits"
     9  	"testing"
    10  	"unsafe"
    11  )
    12  
    13  func TestUintSize(t *testing.T) {
    14  	var x uint
    15  	if want := unsafe.Sizeof(x) * 8; UintSize != want {
    16  		t.Fatalf("UintSize = %d; want %d", UintSize, want)
    17  	}
    18  }
    19  
    20  func TestLeadingZeros(t *testing.T) {
    21  	for i := 0; i < 256; i++ {
    22  		nlz := tab[i].nlz
    23  		for k := 0; k < 64-8; k++ {
    24  			x := uint64(i) << uint(k)
    25  			if x <= 1<<8-1 {
    26  				got := LeadingZeros8(uint8(x))
    27  				want := nlz - k + (8 - 8)
    28  				if x == 0 {
    29  					want = 8
    30  				}
    31  				if got != want {
    32  					t.Fatalf("LeadingZeros8(%#02x) == %d; want %d", x, got, want)
    33  				}
    34  			}
    35  
    36  			if x <= 1<<16-1 {
    37  				got := LeadingZeros16(uint16(x))
    38  				want := nlz - k + (16 - 8)
    39  				if x == 0 {
    40  					want = 16
    41  				}
    42  				if got != want {
    43  					t.Fatalf("LeadingZeros16(%#04x) == %d; want %d", x, got, want)
    44  				}
    45  			}
    46  
    47  			if x <= 1<<32-1 {
    48  				got := LeadingZeros32(uint32(x))
    49  				want := nlz - k + (32 - 8)
    50  				if x == 0 {
    51  					want = 32
    52  				}
    53  				if got != want {
    54  					t.Fatalf("LeadingZeros32(%#08x) == %d; want %d", x, got, want)
    55  				}
    56  				if UintSize == 32 {
    57  					got = LeadingZeros(uint(x))
    58  					if got != want {
    59  						t.Fatalf("LeadingZeros(%#08x) == %d; want %d", x, got, want)
    60  					}
    61  				}
    62  			}
    63  
    64  			if x <= 1<<64-1 {
    65  				got := LeadingZeros64(uint64(x))
    66  				want := nlz - k + (64 - 8)
    67  				if x == 0 {
    68  					want = 64
    69  				}
    70  				if got != want {
    71  					t.Fatalf("LeadingZeros64(%#016x) == %d; want %d", x, got, want)
    72  				}
    73  				if UintSize == 64 {
    74  					got = LeadingZeros(uint(x))
    75  					if got != want {
    76  						t.Fatalf("LeadingZeros(%#016x) == %d; want %d", x, got, want)
    77  					}
    78  				}
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  // Exported (global) variable serving as input for some
    85  // of the benchmarks to ensure side-effect free calls
    86  // are not optimized away.
    87  var Input uint64 = DeBruijn64
    88  
    89  // Exported (global) variable to store function results
    90  // during benchmarking to ensure side-effect free calls
    91  // are not optimized away.
    92  var Output int
    93  
    94  func BenchmarkLeadingZeros(b *testing.B) {
    95  	var s int
    96  	for i := 0; i < b.N; i++ {
    97  		s += LeadingZeros(uint(Input) >> (uint(i) % UintSize))
    98  	}
    99  	Output = s
   100  }
   101  
   102  func BenchmarkLeadingZeros8(b *testing.B) {
   103  	var s int
   104  	for i := 0; i < b.N; i++ {
   105  		s += LeadingZeros8(uint8(Input) >> (uint(i) % 8))
   106  	}
   107  	Output = s
   108  }
   109  
   110  func BenchmarkLeadingZeros16(b *testing.B) {
   111  	var s int
   112  	for i := 0; i < b.N; i++ {
   113  		s += LeadingZeros16(uint16(Input) >> (uint(i) % 16))
   114  	}
   115  	Output = s
   116  }
   117  
   118  func BenchmarkLeadingZeros32(b *testing.B) {
   119  	var s int
   120  	for i := 0; i < b.N; i++ {
   121  		s += LeadingZeros32(uint32(Input) >> (uint(i) % 32))
   122  	}
   123  	Output = s
   124  }
   125  
   126  func BenchmarkLeadingZeros64(b *testing.B) {
   127  	var s int
   128  	for i := 0; i < b.N; i++ {
   129  		s += LeadingZeros64(uint64(Input) >> (uint(i) % 64))
   130  	}
   131  	Output = s
   132  }
   133  
   134  func TestTrailingZeros(t *testing.T) {
   135  	for i := 0; i < 256; i++ {
   136  		ntz := tab[i].ntz
   137  		for k := 0; k < 64-8; k++ {
   138  			x := uint64(i) << uint(k)
   139  			want := ntz + k
   140  			if x <= 1<<8-1 {
   141  				got := TrailingZeros8(uint8(x))
   142  				if x == 0 {
   143  					want = 8
   144  				}
   145  				if got != want {
   146  					t.Fatalf("TrailingZeros8(%#02x) == %d; want %d", x, got, want)
   147  				}
   148  			}
   149  
   150  			if x <= 1<<16-1 {
   151  				got := TrailingZeros16(uint16(x))
   152  				if x == 0 {
   153  					want = 16
   154  				}
   155  				if got != want {
   156  					t.Fatalf("TrailingZeros16(%#04x) == %d; want %d", x, got, want)
   157  				}
   158  			}
   159  
   160  			if x <= 1<<32-1 {
   161  				got := TrailingZeros32(uint32(x))
   162  				if x == 0 {
   163  					want = 32
   164  				}
   165  				if got != want {
   166  					t.Fatalf("TrailingZeros32(%#08x) == %d; want %d", x, got, want)
   167  				}
   168  				if UintSize == 32 {
   169  					got = TrailingZeros(uint(x))
   170  					if got != want {
   171  						t.Fatalf("TrailingZeros(%#08x) == %d; want %d", x, got, want)
   172  					}
   173  				}
   174  			}
   175  
   176  			if x <= 1<<64-1 {
   177  				got := TrailingZeros64(uint64(x))
   178  				if x == 0 {
   179  					want = 64
   180  				}
   181  				if got != want {
   182  					t.Fatalf("TrailingZeros64(%#016x) == %d; want %d", x, got, want)
   183  				}
   184  				if UintSize == 64 {
   185  					got = TrailingZeros(uint(x))
   186  					if got != want {
   187  						t.Fatalf("TrailingZeros(%#016x) == %d; want %d", x, got, want)
   188  					}
   189  				}
   190  			}
   191  		}
   192  	}
   193  }
   194  
   195  func BenchmarkTrailingZeros(b *testing.B) {
   196  	var s int
   197  	for i := 0; i < b.N; i++ {
   198  		s += TrailingZeros(uint(Input) << (uint(i) % UintSize))
   199  	}
   200  	Output = s
   201  }
   202  
   203  func BenchmarkTrailingZeros8(b *testing.B) {
   204  	var s int
   205  	for i := 0; i < b.N; i++ {
   206  		s += TrailingZeros8(uint8(Input) << (uint(i) % 8))
   207  	}
   208  	Output = s
   209  }
   210  
   211  func BenchmarkTrailingZeros16(b *testing.B) {
   212  	var s int
   213  	for i := 0; i < b.N; i++ {
   214  		s += TrailingZeros16(uint16(Input) << (uint(i) % 16))
   215  	}
   216  	Output = s
   217  }
   218  
   219  func BenchmarkTrailingZeros32(b *testing.B) {
   220  	var s int
   221  	for i := 0; i < b.N; i++ {
   222  		s += TrailingZeros32(uint32(Input) << (uint(i) % 32))
   223  	}
   224  	Output = s
   225  }
   226  
   227  func BenchmarkTrailingZeros64(b *testing.B) {
   228  	var s int
   229  	for i := 0; i < b.N; i++ {
   230  		s += TrailingZeros64(uint64(Input) << (uint(i) % 64))
   231  	}
   232  	Output = s
   233  }
   234  
   235  func TestOnesCount(t *testing.T) {
   236  	var x uint64
   237  	for i := 0; i <= 64; i++ {
   238  		testOnesCount(t, x, i)
   239  		x = x<<1 | 1
   240  	}
   241  
   242  	for i := 64; i >= 0; i-- {
   243  		testOnesCount(t, x, i)
   244  		x = x << 1
   245  	}
   246  
   247  	for i := 0; i < 256; i++ {
   248  		for k := 0; k < 64-8; k++ {
   249  			testOnesCount(t, uint64(i)<<uint(k), tab[i].pop)
   250  		}
   251  	}
   252  }
   253  
   254  func testOnesCount(t *testing.T, x uint64, want int) {
   255  	if x <= 1<<8-1 {
   256  		got := OnesCount8(uint8(x))
   257  		if got != want {
   258  			t.Fatalf("OnesCount8(%#02x) == %d; want %d", uint8(x), got, want)
   259  		}
   260  	}
   261  
   262  	if x <= 1<<16-1 {
   263  		got := OnesCount16(uint16(x))
   264  		if got != want {
   265  			t.Fatalf("OnesCount16(%#04x) == %d; want %d", uint16(x), got, want)
   266  		}
   267  	}
   268  
   269  	if x <= 1<<32-1 {
   270  		got := OnesCount32(uint32(x))
   271  		if got != want {
   272  			t.Fatalf("OnesCount32(%#08x) == %d; want %d", uint32(x), got, want)
   273  		}
   274  		if UintSize == 32 {
   275  			got = OnesCount(uint(x))
   276  			if got != want {
   277  				t.Fatalf("OnesCount(%#08x) == %d; want %d", uint32(x), got, want)
   278  			}
   279  		}
   280  	}
   281  
   282  	if x <= 1<<64-1 {
   283  		got := OnesCount64(uint64(x))
   284  		if got != want {
   285  			t.Fatalf("OnesCount64(%#016x) == %d; want %d", x, got, want)
   286  		}
   287  		if UintSize == 64 {
   288  			got = OnesCount(uint(x))
   289  			if got != want {
   290  				t.Fatalf("OnesCount(%#016x) == %d; want %d", x, got, want)
   291  			}
   292  		}
   293  	}
   294  }
   295  
   296  func BenchmarkOnesCount(b *testing.B) {
   297  	var s int
   298  	for i := 0; i < b.N; i++ {
   299  		s += OnesCount(uint(Input))
   300  	}
   301  	Output = s
   302  }
   303  
   304  func BenchmarkOnesCount8(b *testing.B) {
   305  	var s int
   306  	for i := 0; i < b.N; i++ {
   307  		s += OnesCount8(uint8(Input))
   308  	}
   309  	Output = s
   310  }
   311  
   312  func BenchmarkOnesCount16(b *testing.B) {
   313  	var s int
   314  	for i := 0; i < b.N; i++ {
   315  		s += OnesCount16(uint16(Input))
   316  	}
   317  	Output = s
   318  }
   319  
   320  func BenchmarkOnesCount32(b *testing.B) {
   321  	var s int
   322  	for i := 0; i < b.N; i++ {
   323  		s += OnesCount32(uint32(Input))
   324  	}
   325  	Output = s
   326  }
   327  
   328  func BenchmarkOnesCount64(b *testing.B) {
   329  	var s int
   330  	for i := 0; i < b.N; i++ {
   331  		s += OnesCount64(uint64(Input))
   332  	}
   333  	Output = s
   334  }
   335  
   336  func TestRotateLeft(t *testing.T) {
   337  	var m uint64 = DeBruijn64
   338  
   339  	for k := uint(0); k < 128; k++ {
   340  		x8 := uint8(m)
   341  		got8 := RotateLeft8(x8, int(k))
   342  		want8 := x8<<(k&0x7) | x8>>(8-k&0x7)
   343  		if got8 != want8 {
   344  			t.Fatalf("RotateLeft8(%#02x, %d) == %#02x; want %#02x", x8, k, got8, want8)
   345  		}
   346  		got8 = RotateLeft8(want8, -int(k))
   347  		if got8 != x8 {
   348  			t.Fatalf("RotateLeft8(%#02x, -%d) == %#02x; want %#02x", want8, k, got8, x8)
   349  		}
   350  
   351  		x16 := uint16(m)
   352  		got16 := RotateLeft16(x16, int(k))
   353  		want16 := x16<<(k&0xf) | x16>>(16-k&0xf)
   354  		if got16 != want16 {
   355  			t.Fatalf("RotateLeft16(%#04x, %d) == %#04x; want %#04x", x16, k, got16, want16)
   356  		}
   357  		got16 = RotateLeft16(want16, -int(k))
   358  		if got16 != x16 {
   359  			t.Fatalf("RotateLeft16(%#04x, -%d) == %#04x; want %#04x", want16, k, got16, x16)
   360  		}
   361  
   362  		x32 := uint32(m)
   363  		got32 := RotateLeft32(x32, int(k))
   364  		want32 := x32<<(k&0x1f) | x32>>(32-k&0x1f)
   365  		if got32 != want32 {
   366  			t.Fatalf("RotateLeft32(%#08x, %d) == %#08x; want %#08x", x32, k, got32, want32)
   367  		}
   368  		got32 = RotateLeft32(want32, -int(k))
   369  		if got32 != x32 {
   370  			t.Fatalf("RotateLeft32(%#08x, -%d) == %#08x; want %#08x", want32, k, got32, x32)
   371  		}
   372  		if UintSize == 32 {
   373  			x := uint(m)
   374  			got := RotateLeft(x, int(k))
   375  			want := x<<(k&0x1f) | x>>(32-k&0x1f)
   376  			if got != want {
   377  				t.Fatalf("RotateLeft(%#08x, %d) == %#08x; want %#08x", x, k, got, want)
   378  			}
   379  			got = RotateLeft(want, -int(k))
   380  			if got != x {
   381  				t.Fatalf("RotateLeft(%#08x, -%d) == %#08x; want %#08x", want, k, got, x)
   382  			}
   383  		}
   384  
   385  		x64 := uint64(m)
   386  		got64 := RotateLeft64(x64, int(k))
   387  		want64 := x64<<(k&0x3f) | x64>>(64-k&0x3f)
   388  		if got64 != want64 {
   389  			t.Fatalf("RotateLeft64(%#016x, %d) == %#016x; want %#016x", x64, k, got64, want64)
   390  		}
   391  		got64 = RotateLeft64(want64, -int(k))
   392  		if got64 != x64 {
   393  			t.Fatalf("RotateLeft64(%#016x, -%d) == %#016x; want %#016x", want64, k, got64, x64)
   394  		}
   395  		if UintSize == 64 {
   396  			x := uint(m)
   397  			got := RotateLeft(x, int(k))
   398  			want := x<<(k&0x3f) | x>>(64-k&0x3f)
   399  			if got != want {
   400  				t.Fatalf("RotateLeft(%#016x, %d) == %#016x; want %#016x", x, k, got, want)
   401  			}
   402  			got = RotateLeft(want, -int(k))
   403  			if got != x {
   404  				t.Fatalf("RotateLeft(%#08x, -%d) == %#08x; want %#08x", want, k, got, x)
   405  			}
   406  		}
   407  	}
   408  }
   409  
   410  func BenchmarkRotateLeft(b *testing.B) {
   411  	var s uint
   412  	for i := 0; i < b.N; i++ {
   413  		s += RotateLeft(uint(Input), i)
   414  	}
   415  	Output = int(s)
   416  }
   417  
   418  func BenchmarkRotateLeft8(b *testing.B) {
   419  	var s uint8
   420  	for i := 0; i < b.N; i++ {
   421  		s += RotateLeft8(uint8(Input), i)
   422  	}
   423  	Output = int(s)
   424  }
   425  
   426  func BenchmarkRotateLeft16(b *testing.B) {
   427  	var s uint16
   428  	for i := 0; i < b.N; i++ {
   429  		s += RotateLeft16(uint16(Input), i)
   430  	}
   431  	Output = int(s)
   432  }
   433  
   434  func BenchmarkRotateLeft32(b *testing.B) {
   435  	var s uint32
   436  	for i := 0; i < b.N; i++ {
   437  		s += RotateLeft32(uint32(Input), i)
   438  	}
   439  	Output = int(s)
   440  }
   441  
   442  func BenchmarkRotateLeft64(b *testing.B) {
   443  	var s uint64
   444  	for i := 0; i < b.N; i++ {
   445  		s += RotateLeft64(uint64(Input), i)
   446  	}
   447  	Output = int(s)
   448  }
   449  
   450  func TestReverse(t *testing.T) {
   451  	// test each bit
   452  	for i := uint(0); i < 64; i++ {
   453  		testReverse(t, uint64(1)<<i, uint64(1)<<(63-i))
   454  	}
   455  
   456  	// test a few patterns
   457  	for _, test := range []struct {
   458  		x, r uint64
   459  	}{
   460  		{0, 0},
   461  		{0x1, 0x8 << 60},
   462  		{0x2, 0x4 << 60},
   463  		{0x3, 0xc << 60},
   464  		{0x4, 0x2 << 60},
   465  		{0x5, 0xa << 60},
   466  		{0x6, 0x6 << 60},
   467  		{0x7, 0xe << 60},
   468  		{0x8, 0x1 << 60},
   469  		{0x9, 0x9 << 60},
   470  		{0xa, 0x5 << 60},
   471  		{0xb, 0xd << 60},
   472  		{0xc, 0x3 << 60},
   473  		{0xd, 0xb << 60},
   474  		{0xe, 0x7 << 60},
   475  		{0xf, 0xf << 60},
   476  		{0x5686487, 0xe12616a000000000},
   477  		{0x0123456789abcdef, 0xf7b3d591e6a2c480},
   478  	} {
   479  		testReverse(t, test.x, test.r)
   480  		testReverse(t, test.r, test.x)
   481  	}
   482  }
   483  
   484  func testReverse(t *testing.T, x64, want64 uint64) {
   485  	x8 := uint8(x64)
   486  	got8 := Reverse8(x8)
   487  	want8 := uint8(want64 >> (64 - 8))
   488  	if got8 != want8 {
   489  		t.Fatalf("Reverse8(%#02x) == %#02x; want %#02x", x8, got8, want8)
   490  	}
   491  
   492  	x16 := uint16(x64)
   493  	got16 := Reverse16(x16)
   494  	want16 := uint16(want64 >> (64 - 16))
   495  	if got16 != want16 {
   496  		t.Fatalf("Reverse16(%#04x) == %#04x; want %#04x", x16, got16, want16)
   497  	}
   498  
   499  	x32 := uint32(x64)
   500  	got32 := Reverse32(x32)
   501  	want32 := uint32(want64 >> (64 - 32))
   502  	if got32 != want32 {
   503  		t.Fatalf("Reverse32(%#08x) == %#08x; want %#08x", x32, got32, want32)
   504  	}
   505  	if UintSize == 32 {
   506  		x := uint(x32)
   507  		got := Reverse(x)
   508  		want := uint(want32)
   509  		if got != want {
   510  			t.Fatalf("Reverse(%#08x) == %#08x; want %#08x", x, got, want)
   511  		}
   512  	}
   513  
   514  	got64 := Reverse64(x64)
   515  	if got64 != want64 {
   516  		t.Fatalf("Reverse64(%#016x) == %#016x; want %#016x", x64, got64, want64)
   517  	}
   518  	if UintSize == 64 {
   519  		x := uint(x64)
   520  		got := Reverse(x)
   521  		want := uint(want64)
   522  		if got != want {
   523  			t.Fatalf("Reverse(%#08x) == %#016x; want %#016x", x, got, want)
   524  		}
   525  	}
   526  }
   527  
   528  func BenchmarkReverse(b *testing.B) {
   529  	var s uint
   530  	for i := 0; i < b.N; i++ {
   531  		s += Reverse(uint(i))
   532  	}
   533  	Output = int(s)
   534  }
   535  
   536  func BenchmarkReverse8(b *testing.B) {
   537  	var s uint8
   538  	for i := 0; i < b.N; i++ {
   539  		s += Reverse8(uint8(i))
   540  	}
   541  	Output = int(s)
   542  }
   543  
   544  func BenchmarkReverse16(b *testing.B) {
   545  	var s uint16
   546  	for i := 0; i < b.N; i++ {
   547  		s += Reverse16(uint16(i))
   548  	}
   549  	Output = int(s)
   550  }
   551  
   552  func BenchmarkReverse32(b *testing.B) {
   553  	var s uint32
   554  	for i := 0; i < b.N; i++ {
   555  		s += Reverse32(uint32(i))
   556  	}
   557  	Output = int(s)
   558  }
   559  
   560  func BenchmarkReverse64(b *testing.B) {
   561  	var s uint64
   562  	for i := 0; i < b.N; i++ {
   563  		s += Reverse64(uint64(i))
   564  	}
   565  	Output = int(s)
   566  }
   567  
   568  func TestReverseBytes(t *testing.T) {
   569  	for _, test := range []struct {
   570  		x, r uint64
   571  	}{
   572  		{0, 0},
   573  		{0x01, 0x01 << 56},
   574  		{0x0123, 0x2301 << 48},
   575  		{0x012345, 0x452301 << 40},
   576  		{0x01234567, 0x67452301 << 32},
   577  		{0x0123456789, 0x8967452301 << 24},
   578  		{0x0123456789ab, 0xab8967452301 << 16},
   579  		{0x0123456789abcd, 0xcdab8967452301 << 8},
   580  		{0x0123456789abcdef, 0xefcdab8967452301 << 0},
   581  	} {
   582  		testReverseBytes(t, test.x, test.r)
   583  		testReverseBytes(t, test.r, test.x)
   584  	}
   585  }
   586  
   587  func testReverseBytes(t *testing.T, x64, want64 uint64) {
   588  	x16 := uint16(x64)
   589  	got16 := ReverseBytes16(x16)
   590  	want16 := uint16(want64 >> (64 - 16))
   591  	if got16 != want16 {
   592  		t.Fatalf("ReverseBytes16(%#04x) == %#04x; want %#04x", x16, got16, want16)
   593  	}
   594  
   595  	x32 := uint32(x64)
   596  	got32 := ReverseBytes32(x32)
   597  	want32 := uint32(want64 >> (64 - 32))
   598  	if got32 != want32 {
   599  		t.Fatalf("ReverseBytes32(%#08x) == %#08x; want %#08x", x32, got32, want32)
   600  	}
   601  	if UintSize == 32 {
   602  		x := uint(x32)
   603  		got := ReverseBytes(x)
   604  		want := uint(want32)
   605  		if got != want {
   606  			t.Fatalf("ReverseBytes(%#08x) == %#08x; want %#08x", x, got, want)
   607  		}
   608  	}
   609  
   610  	got64 := ReverseBytes64(x64)
   611  	if got64 != want64 {
   612  		t.Fatalf("ReverseBytes64(%#016x) == %#016x; want %#016x", x64, got64, want64)
   613  	}
   614  	if UintSize == 64 {
   615  		x := uint(x64)
   616  		got := ReverseBytes(x)
   617  		want := uint(want64)
   618  		if got != want {
   619  			t.Fatalf("ReverseBytes(%#016x) == %#016x; want %#016x", x, got, want)
   620  		}
   621  	}
   622  }
   623  
   624  func BenchmarkReverseBytes(b *testing.B) {
   625  	var s uint
   626  	for i := 0; i < b.N; i++ {
   627  		s += ReverseBytes(uint(i))
   628  	}
   629  	Output = int(s)
   630  }
   631  
   632  func BenchmarkReverseBytes16(b *testing.B) {
   633  	var s uint16
   634  	for i := 0; i < b.N; i++ {
   635  		s += ReverseBytes16(uint16(i))
   636  	}
   637  	Output = int(s)
   638  }
   639  
   640  func BenchmarkReverseBytes32(b *testing.B) {
   641  	var s uint32
   642  	for i := 0; i < b.N; i++ {
   643  		s += ReverseBytes32(uint32(i))
   644  	}
   645  	Output = int(s)
   646  }
   647  
   648  func BenchmarkReverseBytes64(b *testing.B) {
   649  	var s uint64
   650  	for i := 0; i < b.N; i++ {
   651  		s += ReverseBytes64(uint64(i))
   652  	}
   653  	Output = int(s)
   654  }
   655  
   656  func TestLen(t *testing.T) {
   657  	for i := 0; i < 256; i++ {
   658  		len := 8 - tab[i].nlz
   659  		for k := 0; k < 64-8; k++ {
   660  			x := uint64(i) << uint(k)
   661  			want := 0
   662  			if x != 0 {
   663  				want = len + k
   664  			}
   665  			if x <= 1<<8-1 {
   666  				got := Len8(uint8(x))
   667  				if got != want {
   668  					t.Fatalf("Len8(%#02x) == %d; want %d", x, got, want)
   669  				}
   670  			}
   671  
   672  			if x <= 1<<16-1 {
   673  				got := Len16(uint16(x))
   674  				if got != want {
   675  					t.Fatalf("Len16(%#04x) == %d; want %d", x, got, want)
   676  				}
   677  			}
   678  
   679  			if x <= 1<<32-1 {
   680  				got := Len32(uint32(x))
   681  				if got != want {
   682  					t.Fatalf("Len32(%#08x) == %d; want %d", x, got, want)
   683  				}
   684  				if UintSize == 32 {
   685  					got := Len(uint(x))
   686  					if got != want {
   687  						t.Fatalf("Len(%#08x) == %d; want %d", x, got, want)
   688  					}
   689  				}
   690  			}
   691  
   692  			if x <= 1<<64-1 {
   693  				got := Len64(uint64(x))
   694  				if got != want {
   695  					t.Fatalf("Len64(%#016x) == %d; want %d", x, got, want)
   696  				}
   697  				if UintSize == 64 {
   698  					got := Len(uint(x))
   699  					if got != want {
   700  						t.Fatalf("Len(%#016x) == %d; want %d", x, got, want)
   701  					}
   702  				}
   703  			}
   704  		}
   705  	}
   706  }
   707  
   708  // ----------------------------------------------------------------------------
   709  // Testing support
   710  
   711  type entry = struct {
   712  	nlz, ntz, pop int
   713  }
   714  
   715  // tab contains results for all uint8 values
   716  var tab [256]entry
   717  
   718  func init() {
   719  	tab[0] = entry{8, 8, 0}
   720  	for i := 1; i < len(tab); i++ {
   721  		// nlz
   722  		x := i // x != 0
   723  		n := 0
   724  		for x&0x80 == 0 {
   725  			n++
   726  			x <<= 1
   727  		}
   728  		tab[i].nlz = n
   729  
   730  		// ntz
   731  		x = i // x != 0
   732  		n = 0
   733  		for x&1 == 0 {
   734  			n++
   735  			x >>= 1
   736  		}
   737  		tab[i].ntz = n
   738  
   739  		// pop
   740  		x = i // x != 0
   741  		n = 0
   742  		for x != 0 {
   743  			n += int(x & 1)
   744  			x >>= 1
   745  		}
   746  		tab[i].pop = n
   747  	}
   748  }
   749  

View as plain text