unsigned_const_to_float.mx raw

   1  // Regression: an unsigned integer converts to float UNSIGNED.
   2  //
   3  // conversionOp already returns uitofp for a genuinely unsigned TYPE, but an
   4  // integer constant that does not fit the signed slot carries the Unsigned marker
   5  // on its VALUE while its type says nothing - an untyped integer constant is not
   6  // an unsigned type. The emitter therefore chose sitofp and sign-extended a bit
   7  // pattern that was never negative:
   8  //
   9  //	float64(1<<63)   -> sitofp i64 -9223372036854775808 -> -9.22337203685478e+18
  10  //	float64(1<<64-1) -> sitofp i64 -1                  -> -1.0
  11  //
  12  // `1<<64-1` is math.MaxUint64, which appears in math/const.mx, math/bits (three
  13  // sites), internal/runtime/math, internal/runtime/sys/intrinsics, saferio/io.mx,
  14  // debug/elf/file.mx and net/http/h2_bundle.mx - every one of them got -1 when it
  15  // reached a float.
  16  //
  17  // Negative signed values must stay negative, so the fix is keyed on the marker,
  18  // not on the width.
  19  package main
  20  
  21  func main() {
  22  	// Termination: a batch program must still be stoppable, so the entry
  23  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  24  	select {
  25  	case <-shutdown:
  26  		return
  27  	default:
  28  	}
  29  
  30  	if float64(1<<62) != 4611686018427387904.0 {
  31  		panic("1<<62")
  32  	}
  33  	if float64(1<<63) != 9223372036854775808.0 {
  34  		panic("1<<63")
  35  	}
  36  	if float64(1<<63+1) != 9223372036854775808.0 {
  37  		panic("1<<63+1")
  38  	}
  39  	if float64(1<<64-1) != 18446744073709551616.0 {
  40  		panic("1<<64-1 (MaxUint64)")
  41  	}
  42  
  43  	// genuinely signed values keep their sign
  44  	if float64(-1) != -1.0 {
  45  		panic("-1")
  46  	}
  47  	if float64(-(1 << 62)) != -4611686018427387904.0 {
  48  		panic("-(1<<62)")
  49  	}
  50  
  51  	// a typed uint64 takes the same path
  52  	u := uint64(1) << 63
  53  	if float64(u) != 9223372036854775808.0 {
  54  		panic("typed uint64")
  55  	}
  56  
  57  	// and so does a typed uint32 above 2^31, where sitofp also disagreed
  58  	var w uint32 = 3000000000
  59  	if float64(w) != 3000000000.0 {
  60  		panic("typed uint32")
  61  	}
  62  
  63  	println("ok")
  64  }
  65