Home | History | Annotate | Download | only in ssa
      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 ssa
      6 
      7 import "testing"
      8 
      9 // TestNlzNto tests nlz/nto of the same number which is used in some of
     10 // the rewrite rules.
     11 func TestNlzNto(t *testing.T) {
     12 	// construct the bit pattern 000...111, nlz(x) + nto(0) = 64
     13 	var x int64
     14 	for i := int64(0); i < 64; i++ {
     15 		if got := nto(x); got != i {
     16 			t.Errorf("expected nto(0x%X) = %d, got %d", x, i, got)
     17 		}
     18 		if got := nlz(x); got != 64-i {
     19 			t.Errorf("expected nlz(0x%X) = %d, got %d", x, 64-i, got)
     20 		}
     21 		x = (x << 1) | 1
     22 	}
     23 
     24 	x = 0
     25 	// construct the bit pattern 000...111, with bit 33 set as well.
     26 	for i := int64(0); i < 64; i++ {
     27 		tx := x | (1 << 32)
     28 		// nto should be the number of bits we've shifted on, with an extra bit
     29 		// at iter 32
     30 		ntoExp := i
     31 		if ntoExp == 32 {
     32 			ntoExp = 33
     33 		}
     34 		if got := nto(tx); got != ntoExp {
     35 			t.Errorf("expected nto(0x%X) = %d, got %d", tx, ntoExp, got)
     36 		}
     37 
     38 		// sinec bit 33 is set, nlz can be no greater than 31
     39 		nlzExp := 64 - i
     40 		if nlzExp > 31 {
     41 			nlzExp = 31
     42 		}
     43 		if got := nlz(tx); got != nlzExp {
     44 			t.Errorf("expected nlz(0x%X) = %d, got %d", tx, nlzExp, got)
     45 		}
     46 		x = (x << 1) | 1
     47 	}
     48 
     49 }
     50 
     51 func TestNlz(t *testing.T) {
     52 	var nlzTests = []struct {
     53 		v   int64
     54 		exp int64
     55 	}{{0x00, 64},
     56 		{0x01, 63},
     57 		{0x0F, 60},
     58 		{0xFF, 56},
     59 		{0xffffFFFF, 32},
     60 		{-0x01, 0}}
     61 
     62 	for _, tc := range nlzTests {
     63 		if got := nlz(tc.v); got != tc.exp {
     64 			t.Errorf("expected nlz(0x%X) = %d, got %d", tc.v, tc.exp, got)
     65 		}
     66 	}
     67 }
     68 
     69 func TestNto(t *testing.T) {
     70 	var ntoTests = []struct {
     71 		v   int64
     72 		exp int64
     73 	}{{0x00, 0},
     74 		{0x01, 1},
     75 		{0x0F, 4},
     76 		{0xFF, 8},
     77 		{0xffffFFFF, 32},
     78 		{-0x01, 64}}
     79 
     80 	for _, tc := range ntoTests {
     81 		if got := nto(tc.v); got != tc.exp {
     82 			t.Errorf("expected nto(0x%X) = %d, got %d", tc.v, tc.exp, got)
     83 		}
     84 	}
     85 }
     86 
     87 func TestLog2(t *testing.T) {
     88 	var log2Tests = []struct {
     89 		v   int64
     90 		exp int64
     91 	}{{0, -1}, // nlz expects log2(0) == -1
     92 		{1, 0},
     93 		{2, 1},
     94 		{4, 2},
     95 		{7, 2},
     96 		{8, 3},
     97 		{9, 3},
     98 		{1024, 10}}
     99 
    100 	for _, tc := range log2Tests {
    101 		if got := log2(tc.v); got != tc.exp {
    102 			t.Errorf("expected log2(%d) = %d, got %d", tc.v, tc.exp, got)
    103 		}
    104 	}
    105 }
    106 
    107 // We generate memmove for copy(x[1:], x[:]), however we may change it to OpMove,
    108 // because size is known. Check that OpMove is alias-safe, or we did call memmove.
    109 func TestMove(t *testing.T) {
    110 	x := [...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}
    111 	copy(x[1:], x[:])
    112 	for i := 1; i < len(x); i++ {
    113 		if int(x[i]) != i {
    114 			t.Errorf("Memmove got converted to OpMove in alias-unsafe way. Got %d insted of %d in position %d", int(x[i]), i, i+1)
    115 		}
    116 	}
    117 }
    118 
    119 func TestMoveSmall(t *testing.T) {
    120 	x := [...]byte{1, 2, 3, 4, 5, 6, 7}
    121 	copy(x[1:], x[:])
    122 	for i := 1; i < len(x); i++ {
    123 		if int(x[i]) != i {
    124 			t.Errorf("Memmove got converted to OpMove in alias-unsafe way. Got %d instead of %d in position %d", int(x[i]), i, i+1)
    125 		}
    126 	}
    127 }
    128