2 * Copyright (c) 1985 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted provided
6 * that: (1) source distributions retain this entire copyright notice and
7 * comment, and (2) distributions including binaries display the following
8 * acknowledgement: ``This product includes software developed by the
9 * University of California, Berkeley and its contributors'' in the
10 * documentation or other materials provided with the distribution and in
11 * all advertising materials mentioning features or use of this software.
12 * Neither the name of the University nor the names of its contributors may
13 * be used to endorse or promote products derived from this software without
14 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 /* Return the square root of X. */
26 DEFUN (sqrt
, (x
), double x
)
29 CONST
double zero
= 0.0;
32 /* sqrt (NaN) is NaN; sqrt (+-0) is +-0. */
33 if (__isnan (x
) || x
== zero
)
39 /* sqrt (Inf) is Inf. */
43 /* Scale X to [1,4). */
48 /* Subnormal number. */
61 /* Generate sqrt (X) bit by bit (accumulating in Q). */
66 for (i
= 1; i
<= 51; i
++)
73 s
= t
+ t
+ 2, x
-= t
;
80 /* Generate the last bit and determine the final rounding. */
85 (void) (100 + r
); /* Trigger inexact flag. */
96 goto end
; /* B == 1: Round to zero. */
99 t
= 1; /* B > 1: Round to +Inf. */
102 } /* Else round to nearest. */
119 return __scalb (q
, n
);