Last active
August 26, 2015 17:49
-
-
Save arnehormann/032446cca5c01afef686 to your computer and use it in GitHub Desktop.
benchmarks for possible implementations for github.com/golang/go/issues/6721
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"math" | |
"testing" | |
) | |
const bits32 = 24 | |
const step32 float32 = 1 / (1 << bits32) | |
const mask32 uint32 = (1 << bits32) - 1 | |
func Float32(i uint32) float32 { | |
return float32(i&mask32) * step32 | |
} | |
const bits64 = 53 | |
const step64 float64 = 1 / (1 << bits64) | |
const mask64 uint64 = (1 << bits64) - 1 | |
func Float64(i uint64) float64 { | |
return float64(i&mask64) * step64 | |
} | |
func Float64b(i uint64) float64 { | |
return (float64(i >> 10)) * (1.0 / 9007199254740992.0) | |
} | |
const range_1_2Exp64 = 0x3ff0000000000000 | |
const mantissaMask64 = 0x000fffffffffffff | |
func Float64c(i uint64) float64 { | |
return math.Float64frombits(range_1_2Exp64|(mantissaMask64&i)) - 1.0 | |
} | |
func BenchmarkF64a(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
Float64(uint64(i)) | |
} | |
} | |
func BenchmarkF64b(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
Float64b(uint64(i)) | |
} | |
} | |
func BenchmarkF64c(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
Float64c(uint64(i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment