5 * Fixed point arithmetic square root evaluation.
8 * void wm_sqrt(FPU_REG *n, unsigned int control_word)
11 * Copyright (C) 1992,1993,1994
12 * W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
13 * Australia. E-mail billm@vaxc.cc.monash.edu.au
14 * All rights reserved.
16 * This copyright notice covers the redistribution and use of the
17 * FPU emulator developed by W. Metzenthen. It covers only its use
18 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
19 * use is not permitted under this copyright.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must include information specifying
27 * that source code for the emulator is freely available and include
29 * a) an offer to provide the source code for a nominal distribution
31 * b) list at least two alternative methods whereby the source
32 * can be obtained, e.g. a publically accessible bulletin board
33 * and an anonymous ftp site from which the software can be
35 * 3. All advertising materials specifically mentioning features or use of
36 * this emulator must acknowledge that it was developed by W. Metzenthen.
37 * 4. The name of W. Metzenthen may not be used to endorse or promote
38 * products derived from this software without specific prior written
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
42 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
43 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
44 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 * The purpose of this copyright, based upon the Berkeley copyright, is to
54 * ensure that the covered software remains freely available to everyone.
56 * The software (with necessary differences) is also available, but under
57 * the terms of the GNU copyleft, for the Linux operating system and for
58 * the djgpp ms-dos extender.
60 * W. Metzenthen June 1994.
63 * $FreeBSD: src/sys/gnu/i386/fpemul/wm_sqrt.s,v 1.9.2.1 2000/07/07 00:38:42 obrien Exp $
68 /*---------------------------------------------------------------------------+
69 | wm_sqrt(FPU_REG *n, unsigned int control_word) |
70 | returns the square root of n in n. |
72 | Use Newton's method to compute the square root of a number, which must |
73 | be in the range [1.0 .. 4.0), to 64 bits accuracy. |
74 | Does not check the sign or tag of the argument. |
75 | Sets the exponent, but not the sign or tag of the result. |
77 | The guess is kept in %esi:%edi |
78 +---------------------------------------------------------------------------*/
80 #include <gnu/i386/fpemul/fpu_asm.h>
97 /* The de-normalised argument:
99 // b b b b b b b ... b b b b b b .... b b b b 0 0 0 ... 0
100 // ^ binary point here */
102 .long 0 /* ms word */
106 .long 0 /* ls word, at most the ms bit is set */
123 /* We use a rough linear estimate for the first guess.. */
125 cmpl EXP_BIAS,EXP(%esi)
128 shrl $1,%eax /* arg is in the range [1.0 .. 2.0) */
133 /* From here on, n is never accessed directly again until it is
134 // replaced by the answer. */
136 movl %eax,fsqrt_arg_2 /* ms word of n */
137 movl %ecx,fsqrt_arg_1
138 movl %edx,fsqrt_arg_0
140 /* Make a linear first estimate */
142 addl $0x40000000,%eax
143 movl $0xaaaaaaaa,%ecx
145 shll %edx /* max result was 7fff... */
146 testl $0x80000000,%edx /* but min was 3fff... */
147 jnz sqrt_prelim_no_adjust
149 movl $0x80000000,%edx /* round up */
151 sqrt_prelim_no_adjust:
152 movl %edx,%esi /* Our first guess */
154 /* We have now computed (approx) (2 + x) / 3, which forms the basis
155 for a few iterations of Newton's method */
157 movl fsqrt_arg_2,%ecx /* ms word */
159 /* From our initial estimate, three iterations are enough to get us
160 // to 30 bits or so. This will then allow two iterations at better
161 // precision to complete the process.
163 // Compute (g + n/g)/2 at each iteration (g is the guess). */
164 shrl %ecx /* Doing this first will prevent a divide */
165 /* overflow later. */
167 movl %ecx,%edx /* msw of the arg / 2 */
168 divl %esi /* current estimate */
169 shrl %esi /* divide by 2 */
170 addl %eax,%esi /* the new estimate */
182 /* Now that an estimate accurate to about 30 bits has been obtained (in %esi),
183 // we improve it to 60 bits or so.
185 // The strategy from now on is to compute new estimates from
186 // guess := guess + (n - guess^2) / (2 * guess) */
188 /* First, find the square of the guess */
191 /* guess^2 now in %edx:%eax */
193 movl fsqrt_arg_1,%ecx
195 movl fsqrt_arg_2,%ecx /* ms word of normalized n */
197 jnc sqrt_stage_2_positive
198 /* subtraction gives a negative result
199 // negate the result before division */
210 jmp sqrt_stage_2_finish
212 sqrt_stage_2_positive:
225 sarl $1,%ecx /* divide by 2 */
228 /* Form the new estimate in %esi:%edi */
232 jnz sqrt_stage_2_done /* result should be [1..2) */
235 /* It should be possible to get here only if the arg is ffff....ffff*/
236 cmp $0xffffffff,fsqrt_arg_1
237 jnz sqrt_stage_2_error
240 /* The best rounded result.*/
245 movl $0x7fffffff,%eax
246 jmp sqrt_round_result
250 pushl EX_INTERNAL|0x213
256 /* Now the square root has been computed to better than 60 bits */
258 /* Find the square of the guess*/
259 movl %edi,%eax /* ls word of guess*/
280 /* guess^2 now in accum_3:accum_2:accum_1*/
282 movl fsqrt_arg_0,%eax /* get normalized n*/
284 movl fsqrt_arg_1,%eax
286 movl fsqrt_arg_2,%eax /* ms word of normalized n*/
288 jnc sqrt_stage_3_positive
290 /* subtraction gives a negative result*/
291 /* negate the result before division */
299 adcl $0,accum_3 /* This must be zero */
300 jz sqrt_stage_3_no_error
303 pushl EX_INTERNAL|0x207
306 sqrt_stage_3_no_error:
317 sarl $1,%ecx /* divide by 2*/
320 /* prepare to round the result*/
325 jmp sqrt_stage_3_finished
327 sqrt_stage_3_positive:
336 sarl $1,%ecx /* divide by 2*/
339 /* prepare to round the result*/
341 notl %eax /* Negate the correction term*/
344 adcl $0,%ecx /* carry here ==> correction == 0*/
345 adcl $0xffffffff,%esi
350 sqrt_stage_3_finished:
352 /* The result in %esi:%edi:%esi should be good to about 90 bits here,
353 // and the rounding information here does not have sufficient accuracy
354 // in a few rare cases. */
355 cmpl $0xffffffe0,%eax
358 cmpl $0x00000020,%eax
361 cmpl $0x7fffffe0,%eax
364 cmpl $0x80000020,%eax
365 jb sqrt_get_more_precision
368 /* Set up for rounding operations*/
373 movl EXP_BIAS,EXP(%edi) /* Result is in [1.0 .. 2.0)*/
379 /* First, the estimate must be rounded up.*/
384 /* This is an easy case because x^1/2 is monotonic.
385 // We need just find the square of our estimate, compare it
386 // with the argument, and deduce whether our estimate is
387 // above, below, or exact. We use the fact that the estimate
388 // is known to be accurate to about 90 bits. */
389 movl %edi,%eax /* ls word of guess*/
391 movl %edx,%ebx /* 2nd ls word of square*/
392 movl %eax,%ecx /* ls word of square*/
401 jb sqrt_near_exact_ok
404 ja sqrt_near_exact_ok
406 pushl EX_INTERNAL|0x214
413 js sqrt_near_exact_small
415 jnz sqrt_near_exact_large
418 jnz sqrt_near_exact_large
420 /* Our estimate is exactly the right answer*/
422 jmp sqrt_round_result
424 sqrt_near_exact_small:
425 /* Our estimate is too small*/
426 movl $0x000000ff,%eax
427 jmp sqrt_round_result
429 sqrt_near_exact_large:
430 /* Our estimate is too large, we need to decrement it*/
433 movl $0xffffff00,%eax
434 jmp sqrt_round_result
437 sqrt_get_more_precision:
438 /* This case is almost the same as the above, except we start*/
439 /* with an extra bit of precision in the estimate.*/
440 stc /* The extra bit.*/
441 rcll $1,%edi /* Shift the estimate left one bit*/
444 movl %edi,%eax /* ls word of guess*/
446 movl %edx,%ebx /* 2nd ls word of square*/
447 movl %eax,%ecx /* ls word of square*/
454 /* Put our estimate back to its original value*/
456 rcrl $1,%esi /* Shift the estimate left one bit*/
466 pushl EX_INTERNAL|0x215
473 js sqrt_more_prec_small
475 jnz sqrt_more_prec_large
478 jnz sqrt_more_prec_large
480 /* Our estimate is exactly the right answer*/
481 movl $0x80000000,%eax
482 jmp sqrt_round_result
484 sqrt_more_prec_small:
485 /* Our estimate is too small*/
486 movl $0x800000ff,%eax
487 jmp sqrt_round_result
489 sqrt_more_prec_large:
490 /* Our estimate is too large*/
491 movl $0x7fffff00,%eax
492 jmp sqrt_round_result