@@ -40,10 +40,17 @@ func float64toint64(d float64) (y uint64) {
40
40
}
41
41
42
42
func float64touint64 (d float64 ) (y uint64 ) {
43
- _d2v (& y , d )
43
+ _d2vu (& y , d )
44
44
return
45
45
}
46
46
47
+ func float64touint32 (a float64 ) uint32 {
48
+ if a >= 0xffffffff {
49
+ return 0xffffffff
50
+ }
51
+ return uint32 (float64touint64 (a ))
52
+ }
53
+
47
54
func int64tofloat64 (y int64 ) float64 {
48
55
if y < 0 {
49
56
return - uint64tofloat64 (- uint64 (y ))
@@ -117,12 +124,16 @@ func _d2v(y *uint64, d float64) {
117
124
} else {
118
125
/* v = (hi||lo) << -sh */
119
126
sh := uint32 (- sh )
120
- if sh <= 11 {
127
+ if sh <= 10 {
121
128
ylo = xlo << sh
122
129
yhi = xhi << sh | xlo >> (32 - sh )
123
130
} else {
124
- /* overflow */
125
- yhi = uint32 (d ) /* causes something awful */
131
+ if x & sign64 != 0 {
132
+ * y = 0x8000000000000000
133
+ } else {
134
+ * y = 0x7fffffffffffffff
135
+ }
136
+ return
126
137
}
127
138
}
128
139
if x & sign64 != 0 {
@@ -136,6 +147,50 @@ func _d2v(y *uint64, d float64) {
136
147
137
148
* y = uint64 (yhi )<< 32 | uint64 (ylo )
138
149
}
150
+ func _d2vu (y * uint64 , d float64 ) {
151
+ x := * (* uint64 )(unsafe .Pointer (& d ))
152
+ if x & sign64 != 0 {
153
+ * y = 0
154
+ return
155
+ }
156
+
157
+ xhi := uint32 (x >> 32 )& 0xfffff | 0x100000
158
+ xlo := uint32 (x )
159
+ sh := 1075 - int32 (uint32 (x >> 52 )& 0x7ff )
160
+
161
+ var ylo , yhi uint32
162
+ if sh >= 0 {
163
+ sh := uint32 (sh )
164
+ /* v = (hi||lo) >> sh */
165
+ if sh < 32 {
166
+ if sh == 0 {
167
+ ylo = xlo
168
+ yhi = xhi
169
+ } else {
170
+ ylo = xlo >> sh | xhi << (32 - sh )
171
+ yhi = xhi >> sh
172
+ }
173
+ } else {
174
+ if sh == 32 {
175
+ ylo = xhi
176
+ } else if sh < 64 {
177
+ ylo = xhi >> (sh - 32 )
178
+ }
179
+ }
180
+ } else {
181
+ /* v = (hi||lo) << -sh */
182
+ sh := uint32 (- sh )
183
+ if sh <= 11 {
184
+ ylo = xlo << sh
185
+ yhi = xhi << sh | xlo >> (32 - sh )
186
+ } else {
187
+ /* overflow */
188
+ * y = 0xffffffffffffffff
189
+ return
190
+ }
191
+ }
192
+ * y = uint64 (yhi )<< 32 | uint64 (ylo )
193
+ }
139
194
func uint64div (n , d uint64 ) uint64 {
140
195
// Check for 32 bit operands
141
196
if uint32 (n >> 32 ) == 0 && uint32 (d >> 32 ) == 0 {
0 commit comments