shift_context_width.mx raw

   1  // A shift whose left operand is an untyped constant takes its type from the
   2  // context, not from the count. `r = 1 << m` with r uint64 is a 64-bit shift;
   3  // stage4 typed it from the uint32 count and shifted at 32 bits, so 1<<52 came
   4  // out as 1<<20 (and the assignment was rejected outright as uint32 to uint64).
   5  // strconv's atofHex has exactly this shape (mantissa = 1 << flt.mantbits,
   6  // mantbits 52 for float64).
   7  package main
   8  
   9  func wide(m uint32) (r uint64) {
  10  	r = 1 << m
  11  	return r
  12  }
  13  
  14  func narrow(m uint32) (r int32) {
  15  	r = 1 << m
  16  	return r
  17  }
  18  
  19  func mixed(m uint32, bias int32) (r int32) {
  20  	r = 1<<m + bias
  21  	return r
  22  }
  23  
  24  func main() {
  25  	if wide(52) != 4503599627370496 {
  26  		panic("uint64 shift computed at the wrong width")
  27  	}
  28  	if wide(23) != 8388608 {
  29  		panic("uint64 shift by 23 wrong")
  30  	}
  31  	if narrow(10) != 1024 {
  32  		panic("int32 shift wrong")
  33  	}
  34  	if mixed(4, 5) != 21 {
  35  		panic("shift plus int32 sibling wrong")
  36  	}
  37  	println(wide(52))
  38  }
  39