-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
bfloat16.go
61 lines (46 loc) · 1.31 KB
/
bfloat16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package dtype
import (
"encoding/binary"
"math"
"strconv"
)
//See: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
//See: https://en.wikipedia.org/wiki/Floating-point_arithmetic
//See: https://cloud.google.com/tpu/docs/bfloat16
type BFloat16 uint16
func (bf BFloat16) Bits() uint16 {
return uint16(bf)
}
func (bf BFloat16) Float32() float32 {
return math.Float32frombits(uint32(bf) << 16)
}
func (bf BFloat16) Float64() float64 {
return float64(bf.Float32())
}
func (bf *BFloat16) String() string {
return strconv.FormatFloat(bf.Float64(), 'f', -1, 32)
}
func BFloat16fromFloat32(f32 float32) BFloat16 {
return BFloat16(math.Float32bits(f32) >> 16)
}
func BFloat16frombits(b16 uint16) BFloat16 {
return BFloat16(b16)
}
func ReadBFloat16LittleEndian(b []byte) BFloat16 {
return BFloat16(binary.LittleEndian.Uint16(b))
}
func ReadBFloat16BigEndian(b []byte) BFloat16 {
return BFloat16(binary.BigEndian.Uint16(b))
}
func WriteBFloat16LittleEndian(b []byte, v BFloat16) {
binary.LittleEndian.PutUint16(b, v.Bits())
}
func WriteBFloat16BigEndian(b []byte, v BFloat16) {
binary.BigEndian.PutUint16(b, v.Bits())
}
func BFloat16bitsToFloat32(b16 uint16) float32 {
return math.Float32frombits(uint32(b16) << 16)
}
func Float32ToBFloat16bits(f32 float32) uint16 {
return uint16(math.Float32bits(f32) >> 16)
}