-
Notifications
You must be signed in to change notification settings - Fork 20
/
functions_specific_values.go
127 lines (107 loc) · 2.55 KB
/
functions_specific_values.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package fpu
// Generation of specific floating-point values
//
// The following functions generate specific floating-point values. The sign
// argument of FPZero() , FPMaxNormal() , and FPInfinity() is '0' for the
// positive version and '1' for the negative version.
func (fpu *FPU) FPZero(sign bool, N int) uint64 {
// page A2-46 of "ARMv7-M"
// bits(N) FPZero(bit sign, integer N)
// assert N IN {16,32,64};
// if N == 16 then
// E = 5;
// elsif N == 32 then
// E = 8;
// else E = 11;
// F = N - E - 1;
// exp = Zeros(E);
// frac = Zeros(F);
// return sign:exp:frac;
if !sign {
return 0
}
switch N {
case 16:
return 0xfffffffffff8000
case 32:
return 0xfffffff80000000
case 64:
return 0x800000000000000
}
panic("unsupported number of bits in FPZero()")
}
func (fpu *FPU) FPInfinity(sign bool, N int) uint64 {
// page A2-46 of "ARMv7-M"
var E int
switch N {
case 16:
E = 5
case 32:
E = 8
case 64:
E = 11
default:
panic("unsupported number of bits in FPInfinity()")
}
F := N - E - 1
exp := uint64((1 << E) - 1)
var S uint64
if sign {
S = uint64(1<<(E+F)-1) ^ 0xffffffffffffffff
}
return S | (exp << F)
}
func (fpu *FPU) FPMaxNormal(sign bool, N int) uint64 {
// page A2-47 of "ARMv7-M"
var E int
switch N {
case 16:
E = 5
case 32:
E = 8
case 64:
E = 11
default:
panic("unsupported number of bits in FPMaxNormal()")
}
F := N - E - 1
exp := uint64((1<<(E-1))-1) << 1
frac := uint64((1 << F) - 1)
var S uint64
if sign {
S = uint64(1<<(E+F)-1) ^ 0xffffffffffffffff
}
return S | (exp << F) | frac
}
func (fpu *FPU) FPDefaultNaN(N int) uint64 {
// page A2-47 of "ARMv7-M"
var E int
switch N {
case 16:
E = 5
case 32:
E = 8
case 64:
E = 11
default:
panic("unsupported numbers of bits in FPDefaultNaN()")
}
F := N - E - 1
exp := uint64((1 << E) - 1)
frac := uint64(1 << (F - 1))
return (exp << F) | frac
}