77
77
*/
78
78
79
79
use core:: f64;
80
+ use core:: num:: Wrapping ;
80
81
81
82
const TINY : f64 = 1.0e-300 ;
82
83
83
84
#[ inline]
84
85
pub fn sqrt ( x : f64 ) -> f64 {
85
86
let mut z: f64 ;
86
- let sign: u32 = 0x80000000 ;
87
+ let sign: Wrapping < u32 > = Wrapping ( 0x80000000 ) ;
87
88
let mut ix0: i32 ;
88
89
let mut s0: i32 ;
89
90
let mut q: i32 ;
90
91
let mut m: i32 ;
91
92
let mut t: i32 ;
92
93
let mut i: i32 ;
93
- let mut r: u32 ;
94
- let mut t1: u32 ;
95
- let mut s1: u32 ;
96
- let mut ix1: u32 ;
97
- let mut q1: u32 ;
94
+ let mut r: Wrapping < u32 > ;
95
+ let mut t1: Wrapping < u32 > ;
96
+ let mut s1: Wrapping < u32 > ;
97
+ let mut ix1: Wrapping < u32 > ;
98
+ let mut q1: Wrapping < u32 > ;
98
99
99
100
ix0 = ( x. to_bits ( ) >> 32 ) as i32 ;
100
- ix1 = x. to_bits ( ) as u32 ;
101
+ ix1 = Wrapping ( x. to_bits ( ) as u32 ) ;
101
102
102
103
/* take care of Inf and NaN */
103
104
if ( ix0 & 0x7ff00000 ) == 0x7ff00000 {
104
105
return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
105
106
}
106
107
/* take care of zero */
107
108
if ix0 <= 0 {
108
- if ( ( ix0 & !( sign as i32 ) ) | ix1 as i32 ) == 0 {
109
+ if ( ( ix0 & !( sign. 0 as i32 ) ) | ix1. 0 as i32 ) == 0 {
109
110
return x; /* sqrt(+-0) = +-0 */
110
111
}
111
112
if ix0 < 0 {
@@ -118,7 +119,7 @@ pub fn sqrt(x: f64) -> f64 {
118
119
/* subnormal x */
119
120
while ix0 == 0 {
120
121
m -= 21 ;
121
- ix0 |= ( ix1 >> 11 ) as i32 ;
122
+ ix0 |= ( ix1 >> 11 ) . 0 as i32 ;
122
123
ix1 <<= 21 ;
123
124
}
124
125
i = 0 ;
@@ -127,46 +128,46 @@ pub fn sqrt(x: f64) -> f64 {
127
128
ix0 <<= 1 ;
128
129
}
129
130
m -= i - 1 ;
130
- ix0 |= ( ix1 >> ( 32 - i) ) as i32 ;
131
- ix1 <<= i ;
131
+ ix0 |= ( ix1 >> ( 32 - i) as usize ) . 0 as i32 ;
132
+ ix1 = ix1 << i as usize ;
132
133
}
133
134
m -= 1023 ; /* unbias exponent */
134
135
ix0 = ( ix0 & 0x000fffff ) | 0x00100000 ;
135
136
if ( m & 1 ) == 1 {
136
137
/* odd m, double x to make it even */
137
- ix0 += ix0 + ( ( ix1 & sign) >> 31 ) as i32 ;
138
+ ix0 += ix0 + ( ( ix1 & sign) >> 31 ) . 0 as i32 ;
138
139
ix1 += ix1;
139
140
}
140
141
m >>= 1 ; /* m = [m/2] */
141
142
142
143
/* generate sqrt(x) bit by bit */
143
- ix0 += ix0 + ( ( ix1 & sign) >> 31 ) as i32 ;
144
+ ix0 += ix0 + ( ( ix1 & sign) >> 31 ) . 0 as i32 ;
144
145
ix1 += ix1;
145
146
q = 0 ; /* [q,q1] = sqrt(x) */
146
- q1 = 0 ;
147
+ q1 = Wrapping ( 0 ) ;
147
148
s0 = 0 ;
148
- s1 = 0 ;
149
- r = 0x00200000 ; /* r = moving bit from right to left */
149
+ s1 = Wrapping ( 0 ) ;
150
+ r = Wrapping ( 0x00200000 ) ; /* r = moving bit from right to left */
150
151
151
- while r != 0 {
152
- t = s0 + r as i32 ;
152
+ while r != Wrapping ( 0 ) {
153
+ t = s0 + r. 0 as i32 ;
153
154
if t <= ix0 {
154
- s0 = t + r as i32 ;
155
+ s0 = t + r. 0 as i32 ;
155
156
ix0 -= t;
156
- q += r as i32 ;
157
+ q += r. 0 as i32 ;
157
158
}
158
- ix0 += ix0 + ( ( ix1 & sign) >> 31 ) as i32 ;
159
+ ix0 += ix0 + ( ( ix1 & sign) >> 31 ) . 0 as i32 ;
159
160
ix1 += ix1;
160
161
r >>= 1 ;
161
162
}
162
163
163
164
r = sign;
164
- while r != 0 {
165
+ while r != Wrapping ( 0 ) {
165
166
t1 = s1 + r;
166
167
t = s0;
167
168
if t < ix0 || ( t == ix0 && t1 <= ix1) {
168
169
s1 = t1 + r;
169
- if ( t1 & sign) == sign && ( s1 & sign) == 0 {
170
+ if ( t1 & sign) == sign && ( s1 & sign) == Wrapping ( 0 ) {
170
171
s0 += 1 ;
171
172
}
172
173
ix0 -= t;
@@ -176,26 +177,26 @@ pub fn sqrt(x: f64) -> f64 {
176
177
ix1 -= t1;
177
178
q1 += r;
178
179
}
179
- ix0 += ix0 + ( ( ix1 & sign) >> 31 ) as i32 ;
180
+ ix0 += ix0 + ( ( ix1 & sign) >> 31 ) . 0 as i32 ;
180
181
ix1 += ix1;
181
182
r >>= 1 ;
182
183
}
183
184
184
185
/* use floating add to find out rounding direction */
185
- if ( ix0 as u32 | ix1) != 0 {
186
+ if ( ix0 as u32 | ix1. 0 ) != 0 {
186
187
z = 1.0 - TINY ; /* raise inexact flag */
187
188
if z >= 1.0 {
188
189
z = 1.0 + TINY ;
189
- if q1 == 0xffffffff {
190
- q1 = 0 ;
190
+ if q1. 0 == 0xffffffff {
191
+ q1 = Wrapping ( 0 ) ;
191
192
q += 1 ;
192
193
} else if z > 1.0 {
193
- if q1 == 0xfffffffe {
194
+ if q1. 0 == 0xfffffffe {
194
195
q += 1 ;
195
196
}
196
- q1 += 2 ;
197
+ q1 += Wrapping ( 2 ) ;
197
198
} else {
198
- q1 += q1 & 1 ;
199
+ q1 += q1 & Wrapping ( 1 ) ;
199
200
}
200
201
}
201
202
}
@@ -205,5 +206,5 @@ pub fn sqrt(x: f64) -> f64 {
205
206
ix1 |= sign;
206
207
}
207
208
ix0 += m << 20 ;
208
- f64:: from_bits ( ( ix0 as u64 ) << 32 | ix1 as u64 )
209
+ f64:: from_bits ( ( ix0 as u64 ) << 32 | ix1. 0 as u64 )
209
210
}
0 commit comments