mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 06:20:26 +00:00
better way to do wire signed ints (no negative zero, remove conditionals)
This commit is contained in:
parent
b7e4ff5d5a
commit
f5c850f098
@ -66,23 +66,14 @@ func wire_decode_uint64(bs []byte) (uint64, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func wire_intToUint(i int64) uint64 {
|
func wire_intToUint(i int64) uint64 {
|
||||||
var u uint64
|
// Non-negative integers mapped to even integers: 0 -> 0, 1 -> 2, etc.
|
||||||
if i < 0 {
|
// Negative integres mapped to odd integes: -1 -> 1, -2 -> 3, etc.
|
||||||
u = uint64(-i) << 1
|
// This means the least significant bit is a sign bit.
|
||||||
u |= 0x01 // sign bit
|
return ((uint64(-(i+1))<<1)|0x01)*(uint64(i)>>63) + (uint64(i)<<1)*(^uint64(i)>>63)
|
||||||
} else {
|
|
||||||
u = uint64(i) << 1
|
|
||||||
}
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func wire_intFromUint(u uint64) int64 {
|
func wire_intFromUint(u uint64) int64 {
|
||||||
var i int64
|
return int64(u&0x01)*(-int64(u>>1)-1) + int64(^u&0x01)*int64(u>>1)
|
||||||
i = int64(u >> 1)
|
|
||||||
if u&0x01 != 0 {
|
|
||||||
i *= -1
|
|
||||||
}
|
|
||||||
return i
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user