unsafe.mx raw

   1  // Copyright 2009 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package math
   6  
   7  import "unsafe"
   8  
   9  // Despite being an exported symbol,
  10  // Float32bits is linknamed by widely used packages.
  11  // Notable members of the hall of shame include:
  12  //   - gitee.com/quant1x/num
  13  //
  14  // Do not remove or change the type signature.
  15  // See go.dev/issue/67401.
  16  //
  17  // Note that this comment is not part of the doc comment.
  18  //
  19  //go:linkname Float32bits
  20  
  21  // Float32bits returns the IEEE 754 binary representation of f,
  22  // with the sign bit of f and the result in the same bit position.
  23  // Float32bits(Float32frombits(x)) == x.
  24  func Float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }
  25  
  26  // Float32frombits returns the floating-point number corresponding
  27  // to the IEEE 754 binary representation b, with the sign bit of b
  28  // and the result in the same bit position.
  29  // Float32frombits(Float32bits(x)) == x.
  30  func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }
  31  
  32  // Float64bits returns the IEEE 754 binary representation of f,
  33  // with the sign bit of f and the result in the same bit position,
  34  // and Float64bits(Float64frombits(x)) == x.
  35  func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
  36  
  37  // Float64frombits returns the floating-point number corresponding
  38  // to the IEEE 754 binary representation b, with the sign bit of b
  39  // and the result in the same bit position.
  40  // Float64frombits(Float64bits(x)) == x.
  41  func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) }
  42