OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libm/] [mathfp/] [s_sqrt.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 joel
 
2
/* @(#)z_sqrt.c 1.0 98/08/13 */
3
/*****************************************************************
4
 * The following routines are coded directly from the algorithms
5
 * and coefficients given in "Software Manual for the Elementary
6
 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7
 * Hall, 1980.
8
 *****************************************************************/
9
 
10
/*
11
FUNCTION
12
        <<sqrt>>, <<sqrtf>>---positive square root
13
 
14
INDEX
15
        sqrt
16
INDEX
17
        sqrtf
18
 
19
ANSI_SYNOPSIS
20
        #include <math.h>
21
        double sqrt(double <[x]>);
22
        float  sqrtf(float <[x]>);
23
 
24
TRAD_SYNOPSIS
25
        #include <math.h>
26
        double sqrt(<[x]>);
27
        float  sqrtf(<[x]>);
28
 
29
DESCRIPTION
30
        <<sqrt>> computes the positive square root of the argument.
31
 
32
RETURNS
33
        On success, the square root is returned. If <[x]> is real and
34
        positive, then the result is positive.  If <[x]> is real and
35
        negative, the global value <<errno>> is set to <<EDOM>> (domain error).
36
 
37
 
38
PORTABILITY
39
        <<sqrt>> is ANSI C.  <<sqrtf>> is an extension.
40
*/
41
 
42
/******************************************************************
43
 * Square Root
44
 *
45
 * Input:
46
 *   x - floating point value
47
 *
48
 * Output:
49
 *   square-root of x
50
 *
51
 * Description:
52
 *   This routine performs floating point square root.
53
 *
54
 *   The initial approximation is computed as
55
 *     y0 = 0.41731 + 0.59016 * f
56
 *   where f is a fraction such that x = f * 2^exp.
57
 *
58
 *   Three Newton iterations in the form of Heron's formula
59
 *   are then performed to obtain the final value:
60
 *     y[i] = (y[i-1] + f / y[i-1]) / 2, i = 1, 2, 3.
61
 *
62
 *****************************************************************/
63
 
64
#include "fdlibm.h"
65
#include "zmath.h"
66
 
67
#ifndef _DOUBLE_IS_32BITS
68
 
69
double
70
_DEFUN (sqrt, (double),
71
        double x)
72
{
73
  double f, y;
74
  int exp, i, odd;
75
 
76
  /* Check for special values. */
77
  switch (numtest (x))
78
    {
79
      case NAN:
80
        errno = EDOM;
81
        return (x);
82
      case INF:
83
        if (ispos (x))
84
          {
85
            errno = EDOM;
86
            return (z_notanum.d);
87
          }
88
        else
89
          {
90
            errno = ERANGE;
91
            return (z_infinity.d);
92
          }
93
    }
94
 
95
  /* Initial checks are performed here. */
96
  if (x == 0.0)
97
    return (0.0);
98
  if (x < 0)
99
    {
100
      errno = EDOM;
101
      return (z_notanum.d);
102
    }
103
 
104
  /* Find the exponent and mantissa for the form x = f * 2^exp. */
105
  f = frexp (x, &exp);
106
 
107
  odd = exp & 1;
108
 
109
  /* Get the initial approximation. */
110
  y = 0.41731 + 0.59016 * f;
111
 
112
  f /= 2.0;
113
  /* Calculate the remaining iterations. */
114
  for (i = 0; i < 3; ++i)
115
    y = y / 2.0 + f / y;
116
 
117
  /* Calculate the final value. */
118
  if (odd)
119
    {
120
      y *= __SQRT_HALF;
121
      exp++;
122
    }
123
  exp >>= 1;
124
  y = ldexp (y, exp);
125
 
126
  return (y);
127
}
128
 
129
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.