Coverage Report

Created: 2025-10-10 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/tools/fdlibm/e_atan2.c
Line
Count
Source
1
2
/*
3
 * ====================================================
4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7
 * Permission to use, copy, modify, and distribute this
8
 * software is freely granted, provided that this notice 
9
 * is preserved.
10
 * ====================================================
11
 *
12
 */
13
14
/* atan2(y,x)
15
 * Method :
16
 *  1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
17
 *  2. Reduce x to positive by (if x and y are unexceptional): 
18
 *    ARG (x+iy) = arctan(y/x)       ... if x > 0,
19
 *    ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
20
 *
21
 * Special cases:
22
 *
23
 *  ATAN2((anything), NaN ) is NaN;
24
 *  ATAN2(NAN , (anything) ) is NaN;
25
 *  ATAN2(+-0, +(anything but NaN)) is +-0  ;
26
 *  ATAN2(+-0, -(anything but NaN)) is +-pi ;
27
 *  ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
28
 *  ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
29
 *  ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
30
 *  ATAN2(+-INF,+INF ) is +-pi/4 ;
31
 *  ATAN2(+-INF,-INF ) is +-3pi/4;
32
 *  ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
33
 *
34
 * Constants:
35
 * The hexadecimal values are the intended ones for the following 
36
 * constants. The decimal values may be used, provided that the 
37
 * compiler will convert from decimal to binary accurately enough 
38
 * to produce the hexadecimal values shown.
39
 */
40
41
#include "math_private.h"
42
43
static volatile double
44
tiny  = 1.0e-300;
45
static const double
46
zero  = 0.0,
47
pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
48
pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
49
pi      = 3.1415926535897931160E+00; /* 0x400921FB, 0x54442D18 */
50
static volatile double
51
pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
52
53
double
54
__ieee754_atan2(double y, double x)
55
853k
{
56
853k
  double z;
57
853k
  int32_t k,m,hx,hy,ix,iy;
58
853k
  u_int32_t lx,ly;
59
60
853k
  EXTRACT_WORDS(hx,lx,x);
61
853k
  ix = hx&0x7fffffff;
62
853k
  EXTRACT_WORDS(hy,ly,y);
63
853k
  iy = hy&0x7fffffff;
64
853k
  if(((ix|((lx|-lx)>>31))>0x7ff00000)||
65
664k
     ((iy|((ly|-ly)>>31))>0x7ff00000))  /* x or y is NaN */
66
348k
      return nan_mix(x, y);
67
505k
  if(hx==0x3ff00000&&lx==0) return s_atan(y);   /* x=1.0 */
68
393k
  m = ((hy>>31)&1)|((hx>>30)&2);  /* 2*sign(x)+sign(y) */
69
70
    /* when y = 0 */
71
393k
  if((iy|ly)==0) {
72
18.3k
      switch(m) {
73
9.18k
    case 0: 
74
9.19k
    case 1: return y;  /* atan(+-0,+anything)=+-0 */
75
1
    case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
76
9.18k
    case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
77
18.3k
      }
78
18.3k
  }
79
    /* when x = 0 */
80
375k
  if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
81
      
82
    /* when x is INF */
83
330k
  if(ix==0x7ff00000) {
84
83.9k
      if(iy==0x7ff00000) {
85
18.3k
    switch(m) {
86
9.18k
        case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
87
1
        case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
88
1
        case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
89
9.18k
        case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
90
18.3k
    }
91
65.5k
      } else {
92
65.5k
    switch(m) {
93
14
        case 0: return  zero  ; /* atan(+...,+INF) */
94
6
        case 1: return -zero  ; /* atan(-...,+INF) */
95
65.5k
        case 2: return  pi+tiny  ; /* atan(+...,-INF) */
96
7
        case 3: return -pi-tiny  ; /* atan(-...,-INF) */
97
65.5k
    }
98
65.5k
      }
99
83.9k
  }
100
    /* when y is INF */
101
246k
  if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
102
103
    /* compute y/x */
104
246k
  k = (iy-ix)>>20;
105
246k
  if(k > 60) {     /* |y/x| >  2**60 */
106
2
      z=pi_o_2+0.5*pi_lo;
107
2
      m&=1;
108
2
  }
109
246k
  else if(hx<0&&k<-60) z=0.0;  /* 0 > |y|/x > -2**-60 */
110
246k
  else z=s_atan(fabs(y/x));   /* safe to do y/x */
111
246k
  switch (m) {
112
74.3k
      case 0: return       z  ; /* atan(+,+) */
113
1
      case 1: return      -z  ; /* atan(-,+) */
114
162k
      case 2: return  pi-(z-pi_lo);/* atan(+,-) */
115
9.18k
      default: /* case 3 */
116
9.18k
            return  (z-pi_lo)-pi;/* atan(-,-) */
117
246k
  }
118
246k
}