Home | History | Annotate | Download | only in testdata
      1 // run
      2 
      3 // Copyright 2016 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 
      7 // This test makes sure that we don't split a single
      8 // load up into two separate loads.
      9 
     10 package main
     11 
     12 import "fmt"
     13 
     14 //go:noinline
     15 func read1(b []byte) (uint16, uint16) {
     16 	// There is only a single read of b[0].  The two
     17 	// returned values must have the same low byte.
     18 	v := b[0]
     19 	return uint16(v), uint16(v) | uint16(b[1])<<8
     20 }
     21 
     22 const N = 100000
     23 
     24 func main1() {
     25 	done := make(chan struct{})
     26 	b := make([]byte, 2)
     27 	go func() {
     28 		for i := 0; i < N; i++ {
     29 			b[0] = byte(i)
     30 			b[1] = byte(i)
     31 		}
     32 		done <- struct{}{}
     33 	}()
     34 	go func() {
     35 		for i := 0; i < N; i++ {
     36 			x, y := read1(b)
     37 			if byte(x) != byte(y) {
     38 				fmt.Printf("x=%x y=%x\n", x, y)
     39 				panic("bad")
     40 			}
     41 		}
     42 		done <- struct{}{}
     43 	}()
     44 	<-done
     45 	<-done
     46 }
     47 
     48 //go:noinline
     49 func read2(b []byte) (uint16, uint16) {
     50 	// There is only a single read of b[1].  The two
     51 	// returned values must have the same high byte.
     52 	v := uint16(b[1]) << 8
     53 	return v, uint16(b[0]) | v
     54 }
     55 
     56 func main2() {
     57 	done := make(chan struct{})
     58 	b := make([]byte, 2)
     59 	go func() {
     60 		for i := 0; i < N; i++ {
     61 			b[0] = byte(i)
     62 			b[1] = byte(i)
     63 		}
     64 		done <- struct{}{}
     65 	}()
     66 	go func() {
     67 		for i := 0; i < N; i++ {
     68 			x, y := read2(b)
     69 			if x&0xff00 != y&0xff00 {
     70 				fmt.Printf("x=%x y=%x\n", x, y)
     71 				panic("bad")
     72 			}
     73 		}
     74 		done <- struct{}{}
     75 	}()
     76 	<-done
     77 	<-done
     78 }
     79 
     80 func main() {
     81 	main1()
     82 	main2()
     83 }
     84