1// run
2
3// Copyright 2018 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
9import (
10	"sync/atomic"
11)
12
13var cnt32 int32
14
15//go:noinline
16func test32(a, b []int) bool {
17	// Try to generate flag value, issue atomic
18	// adds and then re-use the flag value to see if
19	// the atomic add has clobbered them.
20	atomic.AddInt32(&cnt32, 1)
21	if len(a) == len(b) {
22		atomic.AddInt32(&cnt32, 2)
23	}
24	atomic.AddInt32(&cnt32, 4)
25	if len(a) >= len(b) {
26		atomic.AddInt32(&cnt32, 8)
27	}
28	if len(a) <= len(b) {
29		atomic.AddInt32(&cnt32, 16)
30	}
31	return atomic.LoadInt32(&cnt32) == 31
32}
33
34var cnt64 int64
35
36//go:noinline
37func test64(a, b []int) bool {
38	// Try to generate flag value, issue atomic
39	// adds and then re-use the flag value to see if
40	// the atomic add has clobbered them.
41	atomic.AddInt64(&cnt64, 1)
42	if len(a) == len(b) {
43		atomic.AddInt64(&cnt64, 2)
44	}
45	atomic.AddInt64(&cnt64, 4)
46	if len(a) >= len(b) {
47		atomic.AddInt64(&cnt64, 8)
48	}
49	if len(a) <= len(b) {
50		atomic.AddInt64(&cnt64, 16)
51	}
52	return atomic.LoadInt64(&cnt64) == 31
53}
54
55func main() {
56	if !test32([]int{}, []int{}) {
57		panic("test32")
58	}
59	if !test64([]int{}, []int{}) {
60		panic("test64")
61	}
62}
63