MCPcopy
hub / github.com/pquerna/ffjson / floatBits

Method floatBits

fflib/v1/extfloat.go:128–170  ·  view source on GitHub ↗

floatBits returns the bits of the float64 that best approximates the extFloat passed as receiver. Overflow is set to true if the resulting float64 is ±Inf.

(flt *floatInfo)

Source from the content-addressed store, hash-verified

126// the extFloat passed as receiver. Overflow is set to true if
127// the resulting float64 is ±Inf.
128func (f *extFloat) floatBits(flt *floatInfo) (bits uint64, overflow bool) {
129 f.Normalize()
130
131 exp := f.exp + 63
132
133 // Exponent too small.
134 if exp < flt.bias+1 {
135 n := flt.bias + 1 - exp
136 f.mant >>= uint(n)
137 exp += n
138 }
139
140 // Extract 1+flt.mantbits bits from the 64-bit mantissa.
141 mant := f.mant >> (63 - flt.mantbits)
142 if f.mant&(1<<(62-flt.mantbits)) != 0 {
143 // Round up.
144 mant += 1
145 }
146
147 // Rounding might have added a bit; shift down.
148 if mant == 2<<flt.mantbits {
149 mant >>= 1
150 exp++
151 }
152
153 // Infinities.
154 if exp-flt.bias >= 1<<flt.expbits-1 {
155 // ±Inf
156 mant = 0
157 exp = 1<<flt.expbits - 1 + flt.bias
158 overflow = true
159 } else if mant&(1<<flt.mantbits) == 0 {
160 // Denormalized?
161 exp = flt.bias
162 }
163 // Assemble bits.
164 bits = mant & (uint64(1)<<flt.mantbits - 1)
165 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
166 if f.neg {
167 bits |= 1 << (flt.mantbits + flt.expbits)
168 }
169 return
170}
171
172// AssignComputeBounds sets f to the floating point value
173// defined by mant, exp and precision given by flt. It returns

Callers

nothing calls this directly

Calls 1

NormalizeMethod · 0.95

Tested by

no test coverage detected