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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libm/] [mathfp/] [sf_sqrt.c] - Blame information for rev 207

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
 
2
/* @(#)z_sqrtf.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
 * Square Root
11
 *
12
 * Input:
13
 *   x - floating point value
14
 *
15
 * Output:
16
 *   square-root of x
17
 *
18
 * Description:
19
 *   This routine performs floating point square root.
20
 *
21
 *   The initial approximation is computed as
22
 *     y0 = 0.41731 + 0.59016 * f
23
 *   where f is a fraction such that x = f * 2^exp.
24
 *
25
 *   Three Newton iterations in the form of Heron's formula
26
 *   are then performed to obtain the final value:
27
 *     y[i] = (y[i-1] + f / y[i-1]) / 2, i = 1, 2, 3.
28
 *
29
 *****************************************************************/
30
 
31
#include "fdlibm.h"
32
#include "zmath.h"
33
 
34
float
35
_DEFUN (sqrtf, (float),
36
        float x)
37
{
38
  float f, y;
39
  int exp, i, odd;
40
 
41
  /* Check for special values. */
42
  switch (numtestf (x))
43
    {
44
      case NAN:
45
        errno = EDOM;
46
        return (x);
47
      case INF:
48
        if (isposf (x))
49
          {
50
            errno = EDOM;
51
            return (z_notanum_f.f);
52
          }
53
        else
54
          {
55
            errno = ERANGE;
56
            return (z_infinity_f.f);
57
          }
58
    }
59
 
60
  /* Initial checks are performed here. */
61
  if (x == 0.0)
62
    return (0.0);
63
  if (x < 0)
64
    {
65
      errno = EDOM;
66
      return (z_notanum_f.f);
67
    }
68
 
69
  /* Find the exponent and mantissa for the form x = f * 2^exp. */
70
  f = frexpf (x, &exp);
71
  odd = exp & 1;
72
 
73
  /* Get the initial approximation. */
74
  y = 0.41731 + 0.59016 * f;
75
 
76
  f *= 0.5;
77
  /* Calculate the remaining iterations. */
78
  for (i = 0; i < 2; ++i)
79
    y = y * 0.5 + f / y;
80
 
81
  /* Calculate the final value. */
82
  if (odd)
83
    {
84
      y *= __SQRT_HALF;
85
      exp++;
86
    }
87
  exp >>= 1;
88
  y = ldexpf (y, exp);
89
 
90
  return (y);
91
}
92
 
93
#ifdef _DOUBLE_IS_32BITS
94
 
95
double sqrt (double x)
96
{
97
  return (double) sqrtf ((float) x);
98
}
99
 
100
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

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