aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/test/memcombine_test.go
blob: 3fc4a004a3d583e4a61ca0f18714f7fd2a225d2e (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2023 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 test

import (
	"encoding/binary"
	"testing"
)

var gv = [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8}

//go:noinline
func readGlobalUnaligned() uint64 {
	return binary.LittleEndian.Uint64(gv[1:])
}

func TestUnalignedGlobal(t *testing.T) {
	// Note: this is a test not so much of the result of the read, but of
	// the correct compilation of that read. On s390x unaligned global
	// accesses fail to compile.
	if got, want := readGlobalUnaligned(), uint64(0x0807060504030201); got != want {
		t.Errorf("read global %x, want %x", got, want)
	}
}

func TestSpillOfExtendedEndianLoads(t *testing.T) {
	b := []byte{0xaa, 0xbb, 0xcc, 0xdd}

	var testCases = []struct {
		fn   func([]byte) uint64
		want uint64
	}{
		{readUint16le, 0xbbaa},
		{readUint16be, 0xaabb},
		{readUint32le, 0xddccbbaa},
		{readUint32be, 0xaabbccdd},
	}
	for _, test := range testCases {
		if got := test.fn(b); got != test.want {
			t.Errorf("got %x, want %x", got, test.want)
		}
	}
}

func readUint16le(b []byte) uint64 {
	y := uint64(binary.LittleEndian.Uint16(b))
	nop() // force spill
	return y
}

func readUint16be(b []byte) uint64 {
	y := uint64(binary.BigEndian.Uint16(b))
	nop() // force spill
	return y
}

func readUint32le(b []byte) uint64 {
	y := uint64(binary.LittleEndian.Uint32(b))
	nop() // force spill
	return y
}

func readUint32be(b []byte) uint64 {
	y := uint64(binary.BigEndian.Uint32(b))
	nop() // force spill
	return y
}

//go:noinline
func nop() {
}

type T32 struct {
	a, b uint32
}

//go:noinline
func (t *T32) bigEndianLoad() uint64 {
	return uint64(t.a)<<32 | uint64(t.b)
}

//go:noinline
func (t *T32) littleEndianLoad() uint64 {
	return uint64(t.a) | (uint64(t.b) << 32)
}

//go:noinline
func (t *T32) bigEndianStore(x uint64) {
	t.a = uint32(x >> 32)
	t.b = uint32(x)
}

//go:noinline
func (t *T32) littleEndianStore(x uint64) {
	t.a = uint32(x)
	t.b = uint32(x >> 32)
}

type T16 struct {
	a, b uint16
}

//go:noinline
func (t *T16) bigEndianLoad() uint32 {
	return uint32(t.a)<<16 | uint32(t.b)
}

//go:noinline
func (t *T16) littleEndianLoad() uint32 {
	return uint32(t.a) | (uint32(t.b) << 16)
}

//go:noinline
func (t *T16) bigEndianStore(x uint32) {
	t.a = uint16(x >> 16)
	t.b = uint16(x)
}

//go:noinline
func (t *T16) littleEndianStore(x uint32) {
	t.a = uint16(x)
	t.b = uint16(x >> 16)
}

type T8 struct {
	a, b uint8
}

//go:noinline
func (t *T8) bigEndianLoad() uint16 {
	return uint16(t.a)<<8 | uint16(t.b)
}

//go:noinline
func (t *T8) littleEndianLoad() uint16 {
	return uint16(t.a) | (uint16(t.b) << 8)
}

//go:noinline
func (t *T8) bigEndianStore(x uint16) {
	t.a = uint8(x >> 8)
	t.b = uint8(x)
}

//go:noinline
func (t *T8) littleEndianStore(x uint16) {
	t.a = uint8(x)
	t.b = uint8(x >> 8)
}

func TestIssue64468(t *testing.T) {
	t32 := T32{1, 2}
	if got, want := t32.bigEndianLoad(), uint64(1<<32+2); got != want {
		t.Errorf("T32.bigEndianLoad got %x want %x\n", got, want)
	}
	if got, want := t32.littleEndianLoad(), uint64(1+2<<32); got != want {
		t.Errorf("T32.littleEndianLoad got %x want %x\n", got, want)
	}
	t16 := T16{1, 2}
	if got, want := t16.bigEndianLoad(), uint32(1<<16+2); got != want {
		t.Errorf("T16.bigEndianLoad got %x want %x\n", got, want)
	}
	if got, want := t16.littleEndianLoad(), uint32(1+2<<16); got != want {
		t.Errorf("T16.littleEndianLoad got %x want %x\n", got, want)
	}
	t8 := T8{1, 2}
	if got, want := t8.bigEndianLoad(), uint16(1<<8+2); got != want {
		t.Errorf("T8.bigEndianLoad got %x want %x\n", got, want)
	}
	if got, want := t8.littleEndianLoad(), uint16(1+2<<8); got != want {
		t.Errorf("T8.littleEndianLoad got %x want %x\n", got, want)
	}
	t32.bigEndianStore(1<<32 + 2)
	if got, want := t32, (T32{1, 2}); got != want {
		t.Errorf("T32.bigEndianStore got %x want %x\n", got, want)
	}
	t32.littleEndianStore(1<<32 + 2)
	if got, want := t32, (T32{2, 1}); got != want {
		t.Errorf("T32.littleEndianStore got %x want %x\n", got, want)
	}
	t16.bigEndianStore(1<<16 + 2)
	if got, want := t16, (T16{1, 2}); got != want {
		t.Errorf("T16.bigEndianStore got %x want %x\n", got, want)
	}
	t16.littleEndianStore(1<<16 + 2)
	if got, want := t16, (T16{2, 1}); got != want {
		t.Errorf("T16.littleEndianStore got %x want %x\n", got, want)
	}
	t8.bigEndianStore(1<<8 + 2)
	if got, want := t8, (T8{1, 2}); got != want {
		t.Errorf("T8.bigEndianStore got %x want %x\n", got, want)
	}
	t8.littleEndianStore(1<<8 + 2)
	if got, want := t8, (T8{2, 1}); got != want {
		t.Errorf("T8.littleEndianStore got %x want %x\n", got, want)
	}
}