summaryrefslogtreecommitdiffstats
path: root/src/runtime/internal/math/math_test.go
blob: 303eb63405a1d4b2ce966c6b3fb9cbc94ce36ea4 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package math_test

import (
	. "runtime/internal/math"
	"testing"
)

const (
	UintptrSize = 32 << (^uintptr(0) >> 63)
)

type mulUintptrTest struct {
	a        uintptr
	b        uintptr
	overflow bool
}

var mulUintptrTests = []mulUintptrTest{
	{0, 0, false},
	{1000, 1000, false},
	{MaxUintptr, 0, false},
	{MaxUintptr, 1, false},
	{MaxUintptr / 2, 2, false},
	{MaxUintptr / 2, 3, true},
	{MaxUintptr, 10, true},
	{MaxUintptr, 100, true},
	{MaxUintptr / 100, 100, false},
	{MaxUintptr / 1000, 1001, true},
	{1<<(UintptrSize/2) - 1, 1<<(UintptrSize/2) - 1, false},
	{1 << (UintptrSize / 2), 1 << (UintptrSize / 2), true},
	{MaxUintptr >> 32, MaxUintptr >> 32, false},
	{MaxUintptr, MaxUintptr, true},
}

func TestMulUintptr(t *testing.T) {
	for _, test := range mulUintptrTests {
		a, b := test.a, test.b
		for i := 0; i < 2; i++ {
			mul, overflow := MulUintptr(a, b)
			if mul != a*b || overflow != test.overflow {
				t.Errorf("MulUintptr(%v, %v) = %v, %v want %v, %v",
					a, b, mul, overflow, a*b, test.overflow)
			}
			a, b = b, a
		}
	}
}

var SinkUintptr uintptr
var SinkBool bool

var x, y uintptr

func BenchmarkMulUintptr(b *testing.B) {
	x, y = 1, 2
	b.Run("small", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			var overflow bool
			SinkUintptr, overflow = MulUintptr(x, y)
			if overflow {
				SinkUintptr = 0
			}
		}
	})
	x, y = MaxUintptr, MaxUintptr-1
	b.Run("large", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			var overflow bool
			SinkUintptr, overflow = MulUintptr(x, y)
			if overflow {
				SinkUintptr = 0
			}
		}
	})
}