forked from sbinet/npyio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float16.go
67 lines (60 loc) · 1.51 KB
/
float16.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
62
63
64
65
66
67
// Copyright 2023 The npyio Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package float16
import (
"math"
"strconv"
)
// Num represents a half-precision floating point value (float16)
// stored on 16 bits.
//
// See https://en.wikipedia.org/wiki/Half-precision_floating-point_format for more informations.
type Num struct {
bits uint16
}
// New creates a new half-precision floating point value from the provided
// float32 value.
func New(f float32) Num {
var (
bits = math.Float32bits(f)
sign = uint16((bits >> 31) & 0x1)
exp = (bits >> 23) & 0xff
res = int16(exp) - 127 + 15
fc = uint16(bits>>13) & 0x3ff
)
switch {
case exp == 0:
res = 0
case exp == 0xff:
res = 0x1f
case res > 0x1e:
res = 0x1f
fc = 0
case res < 0x01:
res = 0
fc = 0
}
return Num{bits: (sign << 15) | uint16(res<<10) | fc}
}
// Float16frombits returns a new half-precision floating point value from the provided bits.
func Float16Frombits(bits uint16) Num {
return Num{bits: bits}
}
func (f Num) Float32() float32 {
var (
sign = uint32((f.bits >> 15) & 0x1)
exp = (f.bits >> 10) & 0x1f
res = uint32(exp) + 127 - 15
fc = uint32(f.bits & 0x3ff)
)
switch {
case exp == 0:
res = 0
case exp == 0x1f:
res = 0xff
}
return math.Float32frombits((sign << 31) | (res << 23) | (fc << 13))
}
func (f Num) Uint16() uint16 { return f.bits }
func (f Num) String() string { return strconv.FormatFloat(float64(f.Float32()), 'g', -1, 32) }