xref: /haiku/src/system/libroot/posix/glibc/arch/generic/s_csin.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1ba41c650SJérôme Duval /* Complex sine function for double.
2ba41c650SJérôme Duval    Copyright (C) 1997 Free Software Foundation, Inc.
3ba41c650SJérôme Duval    This file is part of the GNU C Library.
4ba41c650SJérôme Duval    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5ba41c650SJérôme Duval 
6ba41c650SJérôme Duval    The GNU C Library is free software; you can redistribute it and/or
7ba41c650SJérôme Duval    modify it under the terms of the GNU Lesser General Public
8ba41c650SJérôme Duval    License as published by the Free Software Foundation; either
9ba41c650SJérôme Duval    version 2.1 of the License, or (at your option) any later version.
10ba41c650SJérôme Duval 
11ba41c650SJérôme Duval    The GNU C Library is distributed in the hope that it will be useful,
12ba41c650SJérôme Duval    but WITHOUT ANY WARRANTY; without even the implied warranty of
13ba41c650SJérôme Duval    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ba41c650SJérôme Duval    Lesser General Public License for more details.
15ba41c650SJérôme Duval 
16ba41c650SJérôme Duval    You should have received a copy of the GNU Lesser General Public
17ba41c650SJérôme Duval    License along with the GNU C Library; if not, write to the Free
18ba41c650SJérôme Duval    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19ba41c650SJérôme Duval    02111-1307 USA.  */
20ba41c650SJérôme Duval 
21ba41c650SJérôme Duval #include <complex.h>
22ba41c650SJérôme Duval #include <fenv.h>
23ba41c650SJérôme Duval #include <math.h>
24ba41c650SJérôme Duval 
25ba41c650SJérôme Duval #include "math_private.h"
26ba41c650SJérôme Duval 
27ba41c650SJérôme Duval 
28ba41c650SJérôme Duval __complex__ double
__csin(__complex__ double x)29ba41c650SJérôme Duval __csin (__complex__ double x)
30ba41c650SJérôme Duval {
31ba41c650SJérôme Duval   __complex__ double retval;
32ba41c650SJérôme Duval   int negate = signbit (__real__ x);
33ba41c650SJérôme Duval   int rcls = fpclassify (__real__ x);
34ba41c650SJérôme Duval   int icls = fpclassify (__imag__ x);
35ba41c650SJérôme Duval 
36ba41c650SJérôme Duval   __real__ x = fabs (__real__ x);
37ba41c650SJérôme Duval 
38ba41c650SJérôme Duval   if (icls >= FP_ZERO)
39ba41c650SJérôme Duval     {
40ba41c650SJérôme Duval       /* Imaginary part is finite.  */
41ba41c650SJérôme Duval       if (rcls >= FP_ZERO)
42ba41c650SJérôme Duval 	{
43ba41c650SJérôme Duval 	  /* Real part is finite.  */
44*f504f610SAugustin Cavalier 	  double sinh_val = sinh (__imag__ x);
45*f504f610SAugustin Cavalier 	  double cosh_val = cosh (__imag__ x);
46ba41c650SJérôme Duval 	  double sinix, cosix;
47ba41c650SJérôme Duval 
48*f504f610SAugustin Cavalier 	  sincos (__real__ x, &sinix, &cosix);
49ba41c650SJérôme Duval 
50ba41c650SJérôme Duval 	  __real__ retval = cosh_val * sinix;
51ba41c650SJérôme Duval 	  __imag__ retval = sinh_val * cosix;
52ba41c650SJérôme Duval 
53ba41c650SJérôme Duval 	  if (negate)
54ba41c650SJérôme Duval 	    __real__ retval = -__real__ retval;
55ba41c650SJérôme Duval 	}
56ba41c650SJérôme Duval       else
57ba41c650SJérôme Duval 	{
58ba41c650SJérôme Duval 	  if (icls == FP_ZERO)
59ba41c650SJérôme Duval 	    {
60ba41c650SJérôme Duval 	      /* Imaginary part is 0.0.  */
61*f504f610SAugustin Cavalier 	      __real__ retval = nan ("");
62ba41c650SJérôme Duval 	      __imag__ retval = __imag__ x;
63ba41c650SJérôme Duval 
64ba41c650SJérôme Duval #ifdef FE_INVALID
65ba41c650SJérôme Duval 	      if (rcls == FP_INFINITE)
66ba41c650SJérôme Duval 		feraiseexcept (FE_INVALID);
67ba41c650SJérôme Duval #endif
68ba41c650SJérôme Duval 	    }
69ba41c650SJérôme Duval 	  else
70ba41c650SJérôme Duval 	    {
71*f504f610SAugustin Cavalier 	      __real__ retval = nan ("");
72*f504f610SAugustin Cavalier 	      __imag__ retval = nan ("");
73ba41c650SJérôme Duval 
74ba41c650SJérôme Duval #ifdef FE_INVALID
75ba41c650SJérôme Duval 	      feraiseexcept (FE_INVALID);
76ba41c650SJérôme Duval #endif
77ba41c650SJérôme Duval 	    }
78ba41c650SJérôme Duval 	}
79ba41c650SJérôme Duval     }
80ba41c650SJérôme Duval   else if (icls == FP_INFINITE)
81ba41c650SJérôme Duval     {
82ba41c650SJérôme Duval       /* Imaginary part is infinite.  */
83ba41c650SJérôme Duval       if (rcls == FP_ZERO)
84ba41c650SJérôme Duval 	{
85ba41c650SJérôme Duval 	  /* Real part is 0.0.  */
86*f504f610SAugustin Cavalier 	  __real__ retval = copysign (0.0, negate ? -1.0 : 1.0);
87ba41c650SJérôme Duval 	  __imag__ retval = __imag__ x;
88ba41c650SJérôme Duval 	}
89ba41c650SJérôme Duval       else if (rcls > FP_ZERO)
90ba41c650SJérôme Duval 	{
91ba41c650SJérôme Duval 	  /* Real part is finite.  */
92ba41c650SJérôme Duval 	  double sinix, cosix;
93ba41c650SJérôme Duval 
94*f504f610SAugustin Cavalier 	  sincos (__real__ x, &sinix, &cosix);
95ba41c650SJérôme Duval 
96*f504f610SAugustin Cavalier 	  __real__ retval = copysign (HUGE_VAL, sinix);
97*f504f610SAugustin Cavalier 	  __imag__ retval = copysign (HUGE_VAL, cosix);
98ba41c650SJérôme Duval 
99ba41c650SJérôme Duval 	  if (negate)
100ba41c650SJérôme Duval 	    __real__ retval = -__real__ retval;
101ba41c650SJérôme Duval 	  if (signbit (__imag__ x))
102ba41c650SJérôme Duval 	    __imag__ retval = -__imag__ retval;
103ba41c650SJérôme Duval 	}
104ba41c650SJérôme Duval       else
105ba41c650SJérôme Duval 	{
106ba41c650SJérôme Duval 	  /* The addition raises the invalid exception.  */
107*f504f610SAugustin Cavalier 	  __real__ retval = nan ("");
108ba41c650SJérôme Duval 	  __imag__ retval = HUGE_VAL;
109ba41c650SJérôme Duval 
110ba41c650SJérôme Duval #ifdef FE_INVALID
111ba41c650SJérôme Duval 	  if (rcls == FP_INFINITE)
112ba41c650SJérôme Duval 	    feraiseexcept (FE_INVALID);
113ba41c650SJérôme Duval #endif
114ba41c650SJérôme Duval 	}
115ba41c650SJérôme Duval     }
116ba41c650SJérôme Duval   else
117ba41c650SJérôme Duval     {
118ba41c650SJérôme Duval       if (rcls == FP_ZERO)
119*f504f610SAugustin Cavalier 	__real__ retval = copysign (0.0, negate ? -1.0 : 1.0);
120ba41c650SJérôme Duval       else
121*f504f610SAugustin Cavalier 	__real__ retval = nan ("");
122*f504f610SAugustin Cavalier       __imag__ retval = nan ("");
123ba41c650SJérôme Duval     }
124ba41c650SJérôme Duval 
125ba41c650SJérôme Duval   return retval;
126ba41c650SJérôme Duval }
127ba41c650SJérôme Duval weak_alias (__csin, csin)
128ba41c650SJérôme Duval #ifdef NO_LONG_DOUBLE
129ba41c650SJérôme Duval strong_alias (__csin, __csinl)
130ba41c650SJérôme Duval weak_alias (__csin, csinl)
131ba41c650SJérôme Duval #endif
132