home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / e_hypot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-16  |  3.5 KB  |  131 lines

  1. // 15 August 1997, Rick Huebner:  Small changes made to adapt for MathLib
  2.  
  3. /* @(#)e_hypot.c 5.1 93/09/24 */
  4. /*
  5.  * ====================================================
  6.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7.  *
  8.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software is freely granted, provided that this notice
  11.  * is preserved.
  12.  * ====================================================
  13.  */
  14.  
  15. #if defined(LIBM_SCCS) && !defined(lint)
  16. static char rcsid[] = "$NetBSD: e_hypot.c,v 1.9 1995/05/12 04:57:27 jtc Exp $";
  17. #endif
  18.  
  19. /* __ieee754_hypot(x,y)
  20.  *
  21.  * Method :
  22.  *    If (assume round-to-nearest) z=x*x+y*y
  23.  *    has error less than sqrt(2)/2 ulp, than
  24.  *    sqrt(z) has error less than 1 ulp (exercise).
  25.  *
  26.  *    So, compute sqrt(x*x+y*y) with some care as
  27.  *    follows to get the error below 1 ulp:
  28.  *
  29.  *    Assume x>y>0;
  30.  *    (if possible, set rounding to round-to-nearest)
  31.  *    1. if x > 2y  use
  32.  *        x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
  33.  *    where x1 = x with lower 32 bits cleared, x2 = x-x1; else
  34.  *    2. if x <= 2y use
  35.  *        t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
  36.  *    where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
  37.  *    y1= y with lower 32 bits chopped, y2 = y-y1.
  38.  *
  39.  *    NOTE: scaling may be necessary if some argument is too
  40.  *          large or too tiny
  41.  *
  42.  * Special cases:
  43.  *    hypot(x,y) is INF if x or y is +INF or -INF; else
  44.  *    hypot(x,y) is NAN if x or y is NAN.
  45.  *
  46.  * Accuracy:
  47.  *     hypot(x,y) returns sqrt(x^2+y^2) with error less
  48.  *     than 1 ulps (units in the last place)
  49.  */
  50.  
  51. #include "math.h"
  52. #include "math_private.h"
  53.  
  54. #ifdef __STDC__
  55.     double __ieee754_hypot(double x, double y)
  56. #else
  57.     double __ieee754_hypot(x,y)
  58.     double x, y;
  59. #endif
  60. {
  61.     double a,b,t1,t2,y1,y2,w;
  62.     int32_t j,k,ha,hb;
  63.  
  64.     GET_HIGH_WORD(ha,x);
  65.     ha &= 0x7fffffff;
  66.     GET_HIGH_WORD(hb,y);
  67.     hb &= 0x7fffffff;
  68.     if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
  69.     SET_HIGH_WORD(a,ha);    /* a <- |a| */
  70.     SET_HIGH_WORD(b,hb);    /* b <- |b| */
  71.     if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
  72.     k=0;
  73.     if(ha > 0x5f300000) {    /* a>2**500 */
  74.        if(ha >= 0x7ff00000) {    /* Inf or NaN */
  75.            u_int32_t low;
  76.            w = a+b;            /* for sNaN */
  77.            GET_LOW_WORD(low,a);
  78.            if(((ha&0xfffff)|low)==0) w = a;
  79.            GET_LOW_WORD(low,b);
  80.            if(((hb^0x7ff00000)|low)==0) w = b;
  81.            return w;
  82.        }
  83.        /* scale a and b by 2**-600 */
  84.        ha -= 0x25800000; hb -= 0x25800000;    k += 600;
  85.        SET_HIGH_WORD(a,ha);
  86.        SET_HIGH_WORD(b,hb);
  87.     }
  88.     if(hb < 0x20b00000) {    /* b < 2**-500 */
  89.         if(hb <= 0x000fffff) {    /* subnormal b or 0 */
  90.             u_int32_t low;
  91.         GET_LOW_WORD(low,b);
  92.         if((hb|low)==0) return a;
  93.         t1=0;
  94.         SET_HIGH_WORD(t1,0x7fd00000);    /* t1=2^1022 */
  95.         b *= t1;
  96.         a *= t1;
  97.         k -= 1022;
  98.         } else {        /* scale a and b by 2^600 */
  99.             ha += 0x25800000;     /* a *= 2^600 */
  100.         hb += 0x25800000;    /* b *= 2^600 */
  101.         k -= 600;
  102.         SET_HIGH_WORD(a,ha);
  103.         SET_HIGH_WORD(b,hb);
  104.         }
  105.     }
  106.     /* medium size a and b */
  107.     w = a-b;
  108.     if (w>b) {
  109.         t1 = 0;
  110.         SET_HIGH_WORD(t1,ha);
  111.         t2 = a-t1;
  112.         w  = jumpto__ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
  113.     } else {
  114.         a  = a+a;
  115.         y1 = 0;
  116.         SET_HIGH_WORD(y1,hb);
  117.         y2 = b - y1;
  118.         t1 = 0;
  119.         SET_HIGH_WORD(t1,ha+0x00100000);
  120.         t2 = a - t1;
  121.         w  = jumpto__ieee754_sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
  122.     }
  123.     if(k!=0) {
  124.         u_int32_t high;
  125.         t1 = 1.0;
  126.         GET_HIGH_WORD(high,t1);
  127.         SET_HIGH_WORD(t1,high+(k<<20));
  128.         return t1*w;
  129.     } else return w;
  130. }
  131.