github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/math/ldexp.go (about)

     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  // Ldexp is the inverse of Frexp.
     8  // It returns frac × 2**exp.
     9  //
    10  // Special cases are:
    11  //	Ldexp(±0, exp) = ±0
    12  //	Ldexp(±Inf, exp) = ±Inf
    13  //	Ldexp(NaN, exp) = NaN
    14  
    15  //extern ldexp
    16  func libc_ldexp(float64, int) float64
    17  
    18  func Ldexp(frac float64, exp int) float64 {
    19  	r := libc_ldexp(frac, exp)
    20  	return r
    21  }
    22  
    23  func ldexp(frac float64, exp int) float64 {
    24  	// special cases
    25  	switch {
    26  	case frac == 0:
    27  		return frac // correctly return -0
    28  	case IsInf(frac, 0) || IsNaN(frac):
    29  		return frac
    30  	}
    31  	frac, e := normalize(frac)
    32  	exp += e
    33  	x := Float64bits(frac)
    34  	exp += int(x>>shift)&mask - bias
    35  	if exp < -1074 {
    36  		return Copysign(0, frac) // underflow
    37  	}
    38  	if exp > 1023 { // overflow
    39  		if frac < 0 {
    40  			return Inf(-1)
    41  		}
    42  		return Inf(1)
    43  	}
    44  	var m float64 = 1
    45  	if exp < -1022 { // denormal
    46  		exp += 52
    47  		m = 1.0 / (1 << 52) // 2**-52
    48  	}
    49  	x &^= mask << shift
    50  	x |= uint64(exp+bias) << shift
    51  	return m * Float64frombits(x)
    52  }