1 /* SPDX-License-Identifier: GPL-2.0 */
3 /*---------------------------------------------------------------------------+
6 | Fixed point arithmetic square root evaluation. |
8 | Copyright (C) 1992,1993,1995,1997 |
9 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
10 | Australia. E-mail billm@suburbia.net |
13 | int wm_sqrt(FPU_REG *n, unsigned int control_word) |
15 +---------------------------------------------------------------------------*/
17 /*---------------------------------------------------------------------------+
18 | wm_sqrt(FPU_REG *n, unsigned int control_word) |
19 | returns the square root of n in n. |
21 | Use Newton's method to compute the square root of a number, which must |
22 | be in the range [1.0 .. 4.0), to 64 bits accuracy. |
23 | Does not check the sign or tag of the argument. |
24 | Sets the exponent, but not the sign or tag of the result. |
26 | The guess is kept in %esi:%edi |
27 +---------------------------------------------------------------------------*/
29 #include "exception.h"
33 #ifndef NON_REENTRANT_FPU
34 /* Local storage on the stack: */
35 #define FPU_accum_3 -4(%ebp) /* ms word */
36 #define FPU_accum_2 -8(%ebp)
37 #define FPU_accum_1 -12(%ebp)
38 #define FPU_accum_0 -16(%ebp)
41 * The de-normalised argument:
43 * b b b b b b b ... b b b b b b .... b b b b 0 0 0 ... 0
46 #define FPU_fsqrt_arg_2 -20(%ebp) /* ms word */
47 #define FPU_fsqrt_arg_1 -24(%ebp)
48 #define FPU_fsqrt_arg_0 -28(%ebp) /* ls word, at most the ms bit is set */
51 /* Local storage in a static area: */
63 /* The de-normalised argument:
65 b b b b b b b ... b b b b b b .... b b b b 0 0 0 ... 0
73 .long 0 /* ls word, at most the ms bit is set */
74 #endif /* NON_REENTRANT_FPU */
78 SYM_FUNC_START(wm_sqrt)
81 #ifndef NON_REENTRANT_FPU
83 #endif /* NON_REENTRANT_FPU */
94 /* We use a rough linear estimate for the first guess.. */
96 cmpw EXP_BIAS,EXP(%esi)
99 shrl $1,%eax /* arg is in the range [1.0 .. 2.0) */
104 /* From here on, n is never accessed directly again until it is
105 replaced by the answer. */
107 movl %eax,FPU_fsqrt_arg_2 /* ms word of n */
108 movl %ecx,FPU_fsqrt_arg_1
109 movl %edx,FPU_fsqrt_arg_0
111 /* Make a linear first estimate */
113 addl $0x40000000,%eax
114 movl $0xaaaaaaaa,%ecx
116 shll %edx /* max result was 7fff... */
117 testl $0x80000000,%edx /* but min was 3fff... */
118 jnz sqrt_prelim_no_adjust
120 movl $0x80000000,%edx /* round up */
122 sqrt_prelim_no_adjust:
123 movl %edx,%esi /* Our first guess */
125 /* We have now computed (approx) (2 + x) / 3, which forms the basis
126 for a few iterations of Newton's method */
128 movl FPU_fsqrt_arg_2,%ecx /* ms word */
131 * From our initial estimate, three iterations are enough to get us
132 * to 30 bits or so. This will then allow two iterations at better
133 * precision to complete the process.
136 /* Compute (g + n/g)/2 at each iteration (g is the guess). */
137 shrl %ecx /* Doing this first will prevent a divide */
138 /* overflow later. */
140 movl %ecx,%edx /* msw of the arg / 2 */
141 divl %esi /* current estimate */
142 shrl %esi /* divide by 2 */
143 addl %eax,%esi /* the new estimate */
156 * Now that an estimate accurate to about 30 bits has been obtained (in %esi),
157 * we improve it to 60 bits or so.
159 * The strategy from now on is to compute new estimates from
160 * guess := guess + (n - guess^2) / (2 * guess)
163 /* First, find the square of the guess */
166 /* guess^2 now in %edx:%eax */
168 movl FPU_fsqrt_arg_1,%ecx
170 movl FPU_fsqrt_arg_2,%ecx /* ms word of normalized n */
172 jnc sqrt_stage_2_positive
174 /* Subtraction gives a negative result,
175 negate the result before division. */
186 jmp sqrt_stage_2_finish
188 sqrt_stage_2_positive:
201 sarl $1,%ecx /* divide by 2 */
204 /* Form the new estimate in %esi:%edi */
208 jnz sqrt_stage_2_done /* result should be [1..2) */
211 /* It should be possible to get here only if the arg is ffff....ffff */
212 cmpl $0xffffffff,FPU_fsqrt_arg_1
213 jnz sqrt_stage_2_error
214 #endif /* PARANOID */
216 /* The best rounded result. */
221 movl $0x7fffffff,%eax
222 jmp sqrt_round_result
226 pushl EX_INTERNAL|0x213
228 #endif /* PARANOID */
232 /* Now the square root has been computed to better than 60 bits. */
234 /* Find the square of the guess. */
235 movl %edi,%eax /* ls word of guess */
237 movl %edx,FPU_accum_1
241 movl %edx,FPU_accum_3
242 movl %eax,FPU_accum_2
246 addl %eax,FPU_accum_1
247 adcl %edx,FPU_accum_2
252 addl %eax,FPU_accum_1
253 adcl %edx,FPU_accum_2
256 /* guess^2 now in FPU_accum_3:FPU_accum_2:FPU_accum_1 */
258 movl FPU_fsqrt_arg_0,%eax /* get normalized n */
259 subl %eax,FPU_accum_1
260 movl FPU_fsqrt_arg_1,%eax
261 sbbl %eax,FPU_accum_2
262 movl FPU_fsqrt_arg_2,%eax /* ms word of normalized n */
263 sbbl %eax,FPU_accum_3
264 jnc sqrt_stage_3_positive
266 /* Subtraction gives a negative result,
267 negate the result before division */
275 adcl $0,FPU_accum_3 /* This must be zero */
276 jz sqrt_stage_3_no_error
279 pushl EX_INTERNAL|0x207
282 sqrt_stage_3_no_error:
283 #endif /* PARANOID */
285 movl FPU_accum_2,%edx
286 movl FPU_accum_1,%eax
293 sarl $1,%ecx /* divide by 2 */
296 /* prepare to round the result */
301 jmp sqrt_stage_3_finished
303 sqrt_stage_3_positive:
304 movl FPU_accum_2,%edx
305 movl FPU_accum_1,%eax
312 sarl $1,%ecx /* divide by 2 */
315 /* prepare to round the result */
317 notl %eax /* Negate the correction term */
320 adcl $0,%ecx /* carry here ==> correction == 0 */
321 adcl $0xffffffff,%esi
326 sqrt_stage_3_finished:
329 * The result in %esi:%edi:%esi should be good to about 90 bits here,
330 * and the rounding information here does not have sufficient accuracy
331 * in a few rare cases.
333 cmpl $0xffffffe0,%eax
336 cmpl $0x00000020,%eax
339 cmpl $0x7fffffe0,%eax
342 cmpl $0x80000020,%eax
343 jb sqrt_get_more_precision
346 /* Set up for rounding operations */
351 movw EXP_BIAS,EXP(%edi) /* Result is in [1.0 .. 2.0) */
356 /* First, the estimate must be rounded up. */
362 * This is an easy case because x^1/2 is monotonic.
363 * We need just find the square of our estimate, compare it
364 * with the argument, and deduce whether our estimate is
365 * above, below, or exact. We use the fact that the estimate
366 * is known to be accurate to about 90 bits.
368 movl %edi,%eax /* ls word of guess */
370 movl %edx,%ebx /* 2nd ls word of square */
371 movl %eax,%ecx /* ls word of square */
380 jb sqrt_near_exact_ok
383 ja sqrt_near_exact_ok
385 pushl EX_INTERNAL|0x214
389 #endif /* PARANOID */
392 js sqrt_near_exact_small
394 jnz sqrt_near_exact_large
397 jnz sqrt_near_exact_large
399 /* Our estimate is exactly the right answer */
401 jmp sqrt_round_result
403 sqrt_near_exact_small:
404 /* Our estimate is too small */
405 movl $0x000000ff,%eax
406 jmp sqrt_round_result
408 sqrt_near_exact_large:
409 /* Our estimate is too large, we need to decrement it */
412 movl $0xffffff00,%eax
413 jmp sqrt_round_result
416 sqrt_get_more_precision:
417 /* This case is almost the same as the above, except we start
418 with an extra bit of precision in the estimate. */
419 stc /* The extra bit. */
420 rcll $1,%edi /* Shift the estimate left one bit */
423 movl %edi,%eax /* ls word of guess */
425 movl %edx,%ebx /* 2nd ls word of square */
426 movl %eax,%ecx /* ls word of square */
433 /* Put our estimate back to its original value */
434 stc /* The ms bit. */
435 rcrl $1,%esi /* Shift the estimate left one bit */
445 pushl EX_INTERNAL|0x215
449 #endif /* PARANOID */
452 js sqrt_more_prec_small
454 jnz sqrt_more_prec_large
457 jnz sqrt_more_prec_large
459 /* Our estimate is exactly the right answer */
460 movl $0x80000000,%eax
461 jmp sqrt_round_result
463 sqrt_more_prec_small:
464 /* Our estimate is too small */
465 movl $0x800000ff,%eax
466 jmp sqrt_round_result
468 sqrt_more_prec_large:
469 /* Our estimate is too large */
470 movl $0x7fffff00,%eax
471 jmp sqrt_round_result
472 SYM_FUNC_END(wm_sqrt)