Source file test/zeroext.go

     1  // run
     2  
     3  // Copyright 2026 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  // Test that 32-bit results are zero-extended
     8  // when an operand becomes constant late in compilation.
     9  
    10  package main
    11  
    12  import "fmt"
    13  
    14  // Indexing a string with idx0() or idx1() yields a constant byte,
    15  // but only once the load is lowered, after early constant folding.
    16  func idx0() int { return 0 }
    17  func idx1() int { return 1 }
    18  
    19  //go:noinline
    20  func addConst(x uint64) uint64 {
    21  	k := uint32("\x00"[idx0()])
    22  	return uint64(uint32(x) + k)
    23  }
    24  
    25  //go:noinline
    26  func negMulConst(x uint32) uint64 {
    27  	k := uint32("\x00\x02"[idx1()])
    28  	return uint64(-(x * k))
    29  }
    30  
    31  func main() {
    32  	if got, want := addConst(0xffffffff00000001), uint64(1); got != want {
    33  		panic(fmt.Sprintf("addConst(0xffffffff00000001) = %#x, want %#x", got, want))
    34  	}
    35  	if got, want := negMulConst(1), uint64(0xfffffffe); got != want {
    36  		panic(fmt.Sprintf("negMulConst(1) = %#x, want %#x", got, want))
    37  	}
    38  }
    39  

View as plain text