github.com/ericlagergren/ctb@v0.0.0-20220810041818-96749d9c394d/xbits/ct/ct_test.go (about)

     1  package ct
     2  
     3  import (
     4  	"math/bits"
     5  	"testing"
     6  )
     7  
     8  const (
     9  	_M   = 1<<bits.UintSize - 1
    10  	_M32 = 1<<32 - 1
    11  	_M64 = 1<<64 - 1
    12  )
    13  
    14  func TestMulDiv64(t *testing.T) {
    15  	// testMul := func(msg string, f func(x, y uint64) (hi, lo uint64), x, y, hi, lo uint64) {
    16  	// 	hi1, lo1 := f(x, y)
    17  	// 	if hi1 != hi || lo1 != lo {
    18  	// 		t.Errorf("%s: got hi:lo = %#x:%#x; want %#x:%#x", msg, hi1, lo1, hi, lo)
    19  	// 	}
    20  	// }
    21  	testDiv := func(msg string, f func(hi, lo, y uint64) (q, r uint64), hi, lo, y, q, r uint64) {
    22  		q1, r1 := f(hi, lo, y)
    23  		if q1 != q || r1 != r {
    24  			t.Errorf("%s: got q:r = %#x:%#x; want %#x:%#x", msg, q1, r1, q, r)
    25  		}
    26  	}
    27  	for _, a := range []struct {
    28  		x, y      uint64
    29  		hi, lo, r uint64
    30  	}{
    31  		{3, 3, 0, 9, 1},
    32  		{1 << 63, 2, 1, 0, 1},
    33  		// {0x3626229738a3b9, 0xd8988a9f1cc4a61, 0x2dd0712657fe8, 0x9dd6a3364c358319, 13},
    34  		// {_M64, _M64, _M64 - 1, 1, 42},
    35  	} {
    36  		// testMul("Mul64", Mul64, a.x, a.y, a.hi, a.lo)
    37  		// testMul("Mul64 symmetric", Mul64, a.y, a.x, a.hi, a.lo)
    38  		testDiv("Div64", Div64, a.hi, a.lo+a.r, a.y, a.x, a.r)
    39  		testDiv("Div64 symmetric", Div64, a.hi, a.lo+a.r, a.x, a.y, a.r)
    40  	}
    41  }
    42  
    43  func TestMulDiv32(t *testing.T) {
    44  	// testMul := func(msg string, f func(x, y uint32) (hi, lo uint32), x, y, hi, lo uint32) {
    45  	// 	hi1, lo1 := f(x, y)
    46  	// 	if hi1 != hi || lo1 != lo {
    47  	// 		t.Errorf("%s: got hi:lo = %#x:%#x; want %#x:%#x", msg, hi1, lo1, hi, lo)
    48  	// 	}
    49  	// }
    50  	testDiv := func(msg string, f func(hi, lo, y uint32) (q, r uint32), hi, lo, y, q, r uint32) {
    51  		t.Helper()
    52  		q1, r1 := f(hi, lo, y)
    53  		if q1 != q || r1 != r {
    54  			t.Errorf("%s: got q:r = %#x:%#x; want %#x:%#x", msg, q1, r1, q, r)
    55  		}
    56  	}
    57  	for _, a := range []struct {
    58  		x, y      uint32
    59  		hi, lo, r uint32
    60  	}{
    61  		// {3, 3, 0, 9, 1},
    62  		// {1 << 31, 2, 1, 0, 1},
    63  		// {0xc47dfa8c, 50911, 0x98a4, 0x998587f4, 13},
    64  		// {_M32, _M32, _M32 - 1, 1, 42},
    65  	} {
    66  		// testMul("Mul32", Mul32, a.x, a.y, a.hi, a.lo)
    67  		// testMul("Mul32 symmetric", Mul32, a.y, a.x, a.hi, a.lo)
    68  		// testDiv("Div32", bits.Div32, a.hi, a.lo+a.r, a.y, a.x, a.r)
    69  		testDiv("Div32", Div32, a.hi, a.lo+a.r, a.y, a.x, a.r)
    70  		testDiv("Div32 symmetric", Div32, a.hi, a.lo+a.r, a.x, a.y, a.r)
    71  	}
    72  }