Skip to content

Instantly share code, notes, and snippets.

@niemeyer niemeyer/comment.go Secret
Last active Dec 19, 2015

Embed
What would you like to do?
const signBit = 0x8000000000000000
const expBits = 0x7ff0000000000000
const expOffset = (64-12)
const expBase = 1023
const mantissaBits = 0x000fffffffffffff
const fourBytes = 0xffffffff
const twoBytes = 0xff
const oneByte = 0xff
var asUInt uint64 = (is there a way to get the raw bytes of a float without unsafe?)
var sign bool = asUInt & (signBit)
var exp int32 = int32((asUInt & expBits) >> expOffset) - expBase
var mantissa int64 = asUInt & mantissaBits
var last := lastBitSet(mantissa)
if lastBitSet 0; last -- {
if cur & 0x01 == 0x01 {
break
}
cur >>= 1
}
return last
}
func lastBitSet(mantissa int64) int {
// Something I think would be faster would be
var last = 52
var cur int64 = mantissa
if cur & fourBytes == 0 {
cur >>= 32
last -= 32
}
if cur & twoBytes == 0 {
cur >>= 16
last -= 16
}
if cur & twoBytes == 0 {
cur >>= 16
last -= 16
}
if cur & oneByte == 0 {
cur >>= 8
last -= 8
}
if cur & oneByte == 0 {
cur >>= 8
last -= 8
}
for ; last > 0; last-- {
if cur & 0x01 == 0x01 {
break
}
cur >>= 1
}
return last
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.