-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.go
More file actions
84 lines (69 loc) · 2.22 KB
/
flags.go
File metadata and controls
84 lines (69 loc) · 2.22 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package numeric
// Bit constants for fVal internal representation
// The integer values stored in f24 do not use the full bits so we borrow the high bit
// for logical flags. The lower 31 bits are used for the actual numeric value.
const (
flagBit = fVal(1) << 31 // MSB used as a boolean flag (sign, NaN, overflow, etc.)
maskBits = fVal(0x7FFFFFFF) // Lower 31 bits hold the actual numeric value
)
// Indices within f24 used for logical flags
const (
signFlag = iota // Index for negative sign flag
nanFlag // Index for NaN (Not-a-Number) flag
overflowFlag // Index for overflow flag
underflowFlag // Index for underflow flag
)
// fVal represents a 32-bit unit: high bit is a flag, lower 31 bits store value
type fVal uint32
// flag returns true if the high bit is set.
func (fv *fVal) flag() bool {
return (*fv & flagBit) != 0
}
// setFlag sets or clears the high bit (flag).
func (fv *fVal) setFlag(flag bool) {
if flag {
*fv |= flagBit // Set MSB
} else {
*fv &^= flagBit // Clear MSB (AND NOT)
}
}
// val returns the numeric portion (lower 31 bits).
func (fv *fVal) val() uint32 {
return uint32(*fv & maskBits)
}
// setVal sets the numeric portion (lower 31 bits), preserving the flag bit.
func (fv *fVal) setVal(val uint32) {
*fv = (*fv & flagBit) | (maskBits & fVal(val))
}
// isNeg checks if the number is marked negative.
func (f *f24) isNeg() bool {
return f[signFlag].flag()
}
// setNeg sets or clears the negative sign flag.
func (f *f24) setNeg(neg bool) {
f[signFlag].setFlag(neg)
}
// isNaN checks if the value is NaN.
func (f *f24) isNaN() bool {
return f[nanFlag].flag()
}
// setNaN sets or clears the NaN flag.
func (f *f24) setNaN(isNan bool) {
f[nanFlag].setFlag(isNan)
}
// isOverflow checks if the overflow flag is set.
func (f *f24) isOverflow() bool {
return f[overflowFlag].flag()
}
// setOverflow sets or clears the overflow flag.
func (f *f24) setOverflow(isOverflow bool) {
f[overflowFlag].setFlag(isOverflow)
}
// isUnderflow checks if the underflow flag is set.
func (f *f24) isUnderflow() bool {
return f[underflowFlag].flag()
}
// setUnderflow sets or clears the underflow flag.
func (f *f24) setUnderflow(isUnderflow bool) {
f[underflowFlag].setFlag(isUnderflow)
}