| 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 |
// Issue 80577: on riscv64, sign extensions were elided after |
| 8 |
// 32-bit instructions that architecturally sign-extend their |
| 9 |
// result, but an unsigned-typed value spilled across a call is |
| 10 |
// restored with a zero-extending load, losing the elided |
| 11 |
// sign extension. |
| 12 |
|
| 13 |
package main |
| 14 |
|
| 15 |
import "math/bits" |
| 16 |
|
| 17 |
var sink uint32 |
| 18 |
|
| 19 |
//go:noinline |
| 20 |
func use(x uint32) { sink = x } |
| 21 |
|
| 22 |
//go:noinline |
| 23 |
func mul(a, b uint32) int64 { |
| 24 |
p := a * b |
| 25 |
use(p) // p live across the call, forcing a spill |
| 26 |
return int64(int32(p)) |
| 27 |
} |
| 28 |
|
| 29 |
//go:noinline |
| 30 |
func div(a, b uint32) int64 { |
| 31 |
p := a / b |
| 32 |
use(p) |
| 33 |
return int64(int32(p)) |
| 34 |
} |
| 35 |
|
| 36 |
//go:noinline |
| 37 |
func rem(a, b uint32) int64 { |
| 38 |
p := a % b |
| 39 |
use(p) |
| 40 |
return int64(int32(p)) |
| 41 |
} |
| 42 |
|
| 43 |
//go:noinline |
| 44 |
func rot(a uint32, k int) int64 { |
| 45 |
p := bits.RotateLeft32(a, k) |
| 46 |
use(p) |
| 47 |
return int64(int32(p)) |
| 48 |
} |
| 49 |
|
| 50 |
func main() { |
| 51 |
const want = -2147483648 |
| 52 |
if got := mul(0x8000, 0x10000); got != want { |
| 53 |
println("mul: got", got, "want", want) |
| 54 |
panic("bad mul") |
| 55 |
} |
| 56 |
if got := div(0x80000000, 1); got != want { |
| 57 |
println("div: got", got, "want", want) |
| 58 |
panic("bad div") |
| 59 |
} |
| 60 |
if got := rem(0x80000000, 0xffffffff); got != want { |
| 61 |
println("rem: got", got, "want", want) |
| 62 |
panic("bad rem") |
| 63 |
} |
| 64 |
if got := rot(1, 31); got != want { |
| 65 |
println("rot: got", got, "want", want) |
| 66 |
panic("bad rot") |
| 67 |
} |
| 68 |
} |