Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964/* * Copyright (c) 2002 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ *//****************************************************************************** File: complex.c**** Contains: C source code for implementations of floating-point** (double) complex functions defined in header file** "complex.h" for PowerPC Macintoshes in native mode.** Transcendental function algorithms are based on the** paper "Branch Cuts for Complex Elementary Functions"** by W. Kahan, May 17, 1987, and on Pascal and C source** code for the SANE 80-/96-bit extended type by Kenton** Hanson and Paul Finlayson, respectively.**** ** Written by: Jon Okada, SANEitation Engineer, ext. 4-4838** ** Copyright: c 1987-1993 by Apple Computer, Inc., all rights reserved.** ** Change History (most recent first):**** 25 Aug 93 ali Changed clog to cLog to avoid clashing with the** stream i/o definition clog.** 14 Jul 93 ali Added #pragma fenv_access on** 22 Feb 93 ali Added a nomaf #pragma.** 05 Feb 93 JPO Modified calls to feclearexcept and feraiseexcept** to reflect changes in "fenv.h".** 18 Dec 92 JPO First created.** ****************************************************************************/
#include "math.h"#include "complex.h"#include <stdint.h>
#define Real(z) (__real__ z)#define Imag(z) (__imag__ z)
/**************************************************************************** CONSTANTS used by complex functions
#include <stdio.h>#include <math.h>#include <float.h>main(){
float FPKASINHOM4f = asinhf(nextafterf(INFINITY,0.0f))/4.0f;float FPKTHETAf = sqrtf(nextafterf(INFINITY,0.0f))/4.0f;float FPKRHOf = 1.0f/FPKTHETAf;float FPKLOVEREf = FLT_MIN/FLT_EPSILON;
printf("FPKASINHOM4 %16.7e %x\n", FPKASINHOM4f, *(int *)(&FPKASINHOM4f));printf("FPKTHETA %16.7e %x\n", FPKTHETAf, *(int *)(&FPKTHETAf));printf("FPKRHO %16.7e %x\n", FPKRHOf, *(int *)(&FPKRHOf));printf("FPKLOVERE %16.7e %x\n", FPKLOVEREf, *(int *)(&FPKLOVEREf));} static const // underflow threshold / round threshold hexdouble FPKLOVERE = HEXDOUBLE(0x03600000,0x00000000);
static const // underflow threshold / round threshold hexsingle FPKLOVEREf = { 0xc000000 };
static const // exp(709.0) hexdouble FPKEXP709 = HEXDOUBLE(0x7fdd422d,0x2be5dc9b);
static const // asinh(nextafterd(+infinity,0.0))/4.0 hexdouble FPKASINHOM4 = HEXDOUBLE(0x406633ce,0x8fb9f87e);
static const // asinh(nextafterf(+infinity,0.0))/4.0 hexsingle FPKASINHOM4f = { 0x41b2d4fc };
static const // sqrt(nextafterd(+infinity,0.0))/4.0 hexdouble FPKTHETA = HEXDOUBLE(0x5fcfffff,0xffffffff);
static const // sqrt(nextafterf(+infinity,0.0))/4.0 hexsingle FPKTHETAf = { 0x5e7fffff };
static const // 4.0/sqrt(nextafterd(+infinity,0.0)) hexdouble FPKRHO = HEXDOUBLE(0x20100000,0x00000000);
static const // 4.0/sqrt(nextafterf(+infinity,0.0)) hexsingle FPKRHOf = { 0x20800001 };
****************************************************************************/
static const double expOverflowThreshold_d = 0x1.62e42fefa39efp+9;static const double expOverflowValue_d = 0x1.fffffffffff2ap+1023; // exp(overflowThreshold)static const double twiceExpOverflowThresh_d = 0x1.62e42fefa39efp+10;
static const double FPKASINHOM4 = 0x1.633ce8fb9f87ep+7;static const float FPKASINHOM4f = 0x1.65a9f8p+4f;static const double FPKTHETA = 0x1.fffffffffffffp+509;static const float FPKTHETAf = 0x1.fffffep+61f;static const double FPKRHO = 0x1p-510;static const float FPKRHOf = 0x1.000002p-62f;
static void cosisin(double x, double complex *z);static void cosisinf(float x, float complex *z);
staticdouble complex xdivc( double x, double complex y ) /* returns (real x) / (complex y) */{ double complex z; double r, denom; if ( fabs(Real(y)) >= fabs(Imag(y)) ) { /* |Real(y)| >= |Imag(y)| */ if (fabs(Real(y)) == INFINITY) { /* Imag(y) and Real(y) are infinite */ Real(z) = copysign(0.0,Real(y)); Imag(z) = copysign(0.0,-Imag(y)); } else { /* |Real(y)| >= finite |Imag(y)| */ r = Imag(y)/Real(y); denom = Real(y) + Imag(y)*r; Real(z) = x/denom; Imag(z) = (-x*r)/denom; } } else { /* |Real(y)| !>= |Imag(y)| */ r = Real(y)/Imag(y); denom = r*Real(y) + Imag(y); Real(z) = (r*x)/denom; Imag(z) = -x/denom; } return z;}
staticfloat complex xdivcf( float x, float complex y ) /* returns (real x) / (complex y) */{ float complex z; float r, denom; if ( fabsf(Real(y)) >= fabsf(Imag(y)) ) { /* |Real(y)| >= |Imag(y)| */ if (fabsf(Real(y)) == INFINITY) { /* Imag(y) and Real(y) are infinite */ Real(z) = copysignf(0.0f,Real(y)); Imag(z) = copysignf(0.0f,-Imag(y)); } else { /* |Real(y)| >= finite |Imag(y)| */ r = Imag(y)/Real(y); denom = Real(y) + Imag(y)*r; Real(z) = x/denom; Imag(z) = (-x*r)/denom; } } else { /* |Real(y)| !>= |Imag(y)| */ r = Real(y)/Imag(y); denom = r*Real(y) + Imag(y); Real(z) = (r*x)/denom; Imag(z) = -x/denom; } return z;}
/**************************************************************************** double cabs(double complex z) returns the absolute value (magnitude) of its complex argument z, avoiding spurious overflow, underflow, and invalid exceptions. The code is identical to hypot[fl].****************************************************************************/
double cabs( double complex z ) { return hypot(Real(z), Imag(z)); }
float cabsf( float complex z ) { return hypotf(Real(z), Imag(z)); }
/**************************************************************************** double carg(double complex z) returns the argument (in radians) of its complex argument z. The algorithm is from Kahan's paper. The argument of a complex number z = x + i*y is defined as atan2(y,x) for finite x and y. CONSTANTS: FPKPI2 = pi/2.0 to double precision FPKPI = pi to double precision Calls: fpclassify, copysign, fabs, atan****************************************************************************/
double carg ( double complex z ) { return atan2(Imag(z), Real(z)); } float cargf ( float complex z ) { return atan2f(Imag(z), Real(z)); }
/**************************************************************************** double complex csqrt(double complex z) returns the complex square root of its argument. The algorithm, which is from the Kahan paper, uses the following identities: sqrt(x + i*y) = sqrt((|z| + Real(z))/2) + i*sqrt((|z| - Real(z))/2) and sqrt(x - i*y) = sqrt((|z| + Real(z))/2) - i*sqrt((|z| - Real(z))/2), where y is positive and x may be positive or negative. CONSTANTS: FPKINF = +infinity Calls: cssqs, scalb, fabs, sqrt, copysign.****************************************************************************/
/* New Intel code written 9/26/06 by scanon * Uses extra precision to compute |z| instead of cssqs(), saving environment calls. * Note that we could also rescale using bits of the SSE2 code from ian's original intel hypot() routine, and will probably * want to do exactly that in the future, to move away from using x87 for this. */
double complex csqrt ( double complex z ){ static const double inf = __builtin_inf(); double u,v; // Special case for infinite y: if (__builtin_fabs(Imag(z)) == inf) return inf + I*Imag(z); // csqrt(x ± i∞) = ∞ ± i∞ for all x, including NaN. // Special cases for y = NaN: if (Imag(z) != Imag(z)) { if (Real(z) != Real(z)) // csqrt(NaN + iNaN) = NaN + iNaN, quietly. return z; else if (Real(z) == inf) // csqrt(∞ + iNaN) = ∞ + iNaN return z; else if (Real(z) == -inf) // csqrt(-∞ ± iNaN) = NaN ± i∞. return Imag(z) + I*copysign(inf,Imag(z)); else { // csqrt(x + iNaN) = NaN + iNaN if x is finite. return Imag(z) + I*Imag(z); } } // At this point, we know that y is finite. Deal with special cases for x: // Special case for x = NaN: if (Real(z) != Real(z)) { // csqrt(NaN + ix) = NaN + iNaN. return Real(z) + I*copysign(Real(z),Imag(z)); } // Special cases for x = 0: if (Real(z) == 0.0) { if (Imag(z) == 0.0) // csqrt(±0 + i0) = 0 + i0, csqrt(±0 - i0) = 0 - i0. return I*Imag(z); else { // csqrt(0 ± iy) = sqrt(y/2) ± i sqrt(y/2). u = __builtin_sqrt(0.5*__builtin_fabs(Imag(z))); return u + I*copysign(u, Imag(z) ); } } // Special cases for infinte x: if (Real(z) == inf) // csqrt(∞ ± iy) = ∞ ± i0 for finite y. return inf + I*copysign(0.0,Imag(z)); if (Real(z) == -inf) // csqrt(-∞ ± iy) = 0 ± i∞ for finite y. return I*copysign(inf,Imag(z)); // At this point, we know that x is finite, non-zero and y is finite. else { // We use extended (80-bit) precision to avoid over- or under-flow in computing |z|. long double x = __builtin_fabsl(Real(z)); long double y = Imag(z); /* Compute * +---------------- +---------------- * | |z| + |Re z| Im z | |z| - |Re z| * u = | -------------- v = ------ = ± | -------------- * \| 2 2u \| 2 * * There is no risk of drastic loss of precision due to cancellation using these formulas, * as there would be if we used the second expression (involving the square root) for v. * * Overflow or Underflow is possible, but only if the actual result does not fit in double width. */ u = (double)__builtin_sqrtl(0.5L*(__builtin_sqrtl(x*x + y*y) + x)); v = 0.5 * (Imag(z) / u); /* If x < 0, then sqrt(z) = |v| + I*copysign(u, Im z). * Otherwise, sqrt(z) = u + I*v. */ if (Real(z) < 0.0) { return __builtin_fabs(v) + I*copysign(u,Imag(z)); } else { return u + I*v; } }}
float complex csqrtf ( float complex z ) { static const float inf = __builtin_inff(); float u,v; // Special case for infinite y: if (__builtin_fabsf(Imag(z)) == inf) return inf + I*Imag(z); // csqrt(x ± i∞) = ∞ ± i∞ for all x, including NaN. // Special cases for y = NaN: if (Imag(z) != Imag(z)) { if (Real(z) != Real(z)) // csqrt(NaN + iNaN) = NaN + iNaN, quietly. return z; else if (Real(z) == inf) // csqrt(∞ + iNaN) = ∞ + iNaN return z; else if (Real(z) == -inf) // csqrt(-∞ ± iNaN) = NaN ± i∞. return Imag(z) + I*copysignf(inf,Imag(z)); else { // csqrt(x + iNaN) = NaN + iNaN if x is finite. return Imag(z) + I*Imag(z); } } // At this point, we know that y is finite. Deal with special cases for x: // Special case for x = NaN: if (Real(z) != Real(z)) { // csqrt(NaN + ix) = NaN + iNaN. return Real(z) + I*copysignf(Real(z),Imag(z)); } // Special cases for x = 0: if (Real(z) == 0.0f) { if (Imag(z) == 0.0f) // csqrt(±0 + i0) = 0 + i0, csqrt(±0 - i0) = 0 - i0. return I*Imag(z); else { // csqrt(0 ± iy) = sqrt(y/2) ± i sqrt(y/2). u = __builtin_sqrtf(0.5f*__builtin_fabsf(Imag(z))); return u + I*copysign(u, Imag(z) ); } } // Special cases for infinte x: if (Real(z) == inf) // csqrt(∞ ± iy) = ∞ ± i0 for finite y. return inf + I*copysign(0.0f,Imag(z)); if (Real(z) == -inf) // csqrt(-∞ ± iy) = 0 ± i∞ for finite y. return I*copysign(inf,Imag(z)); // At this point, we know that x is finite, non-zero and y is finite. else { // We use double (64-bit) precision to avoid over- or under-flow in computing |z|. double x = __builtin_fabs(Real(z)); double y = Imag(z); /* Compute * +---------------- +---------------- * | |z| + |Re z| Im z | |z| - |Re z| * u = | -------------- v = ------ = ± | -------------- * \| 2 2u \| 2 * * There is no risk of drastic loss of precision due to cancellation using these formulas, * as there would be if we used the second expression (involving the square root) for v. * * Overflow or Underflow is possible, but only if the actual result does not fit in double width. */ u = (float)__builtin_sqrt(0.5*(__builtin_sqrt(x*x + y*y) + x)); v = 0.5f * (Imag(z) / u); /* If x < 0, then sqrt(z) = |v| + I*copysign(u, Im z). * Otherwise, sqrt(z) = u + I*v. */ if (Real(z) < 0.0f) { return __builtin_fabsf(v) + I*copysignf(u,Imag(z)); } else { return u + I*v; } } }
/**************************************************************************** double complex clog(double complex z) returns the complex natural logarithm of its argument, using: clog(x + iy) = [ log(x) + 0.5 * log1p(y^2/x^2) ] + I*carg(x + iy) if x > y = [ log(y) + 0.5 * log1p(x^2/y^2) ] + I*carg(x + iy) otherwise the real part is "mathematically" equivalent to log |z|, but the alternative form is used to avoid spurious under/overflow.
Calls: fabs, log1p, log, carg. ****************************************************************************/
double complex clog ( double complex z ) { static const double inf = __builtin_inf(); double large, small, temp; double complex w; long double ratio; Imag(w) = carg(z); // handle x,y = ∞ if ((__builtin_fabs(Real(z)) == inf) || (__builtin_fabs(Imag(z)) == inf)) { Real(w) = inf; return w; } // handle x,y = NaN if (Real(z) != Real(z)) return Real(z) + I*copysign(Real(z),Imag(z)); if (Imag(z) != Imag(z)) return Imag(z) + I*Imag(z); large = __builtin_fabs(Real(z)); small = __builtin_fabs(Imag(z)); if (large < small) { temp = large; large = small; small = temp; } Real(w) = log(large); // if small == 0, then Re(clog(z)) = log(large). This sets div-by-zero when appropriate (if large is also 0). if (small == 0.0) return w; // if large == 1 if (large == 1.0) { Real(w) = 0.5*log1p(small*small); // any underflow here is deserved. return w; } // compute small/large in long double to avoid undue underflow. ratio = (long double)small / (long double)large; if (ratio > 0x1.0p-53L) { /* if ratio is smaller than 2^-53, then * 1/2 log1p(ratio^2) ~ 2^-106 < 1/2 an ulp of log(large), so it can't affect the final result. */ Real(w) += 0.5*log1p((double)(ratio*ratio)); } return w;}
float complex clogf ( float complex z ) { static const float inf = __builtin_inff(); float large, small, temp; float complex w; double ratio; Imag(w) = cargf(z); // handle x,y = ∞ if ((__builtin_fabsf(Real(z)) == inf) || (__builtin_fabsf(Imag(z)) == inf)) { Real(w) = inf; return w; } // handle x,y = NaN if (Real(z) != Real(z)) return Real(z) + I*copysignf(Real(z),Imag(z)); if (Imag(z) != Imag(z)) return Imag(z) + I*Imag(z); large = __builtin_fabsf(Real(z)); small = __builtin_fabsf(Imag(z)); if (large < small) { temp = large; large = small; small = temp; } Real(w) = logf(large); // if small == 0, then Re(clog(z)) = log(large). This sets div-by-zero when appropriate (if large is also 0). if (small == 0.0f) return w; // if large == 1 if (large == 1.0f) { Real(w) = 0.5f*log1pf(small*small); // underflow here is deserved. return w; } // compute small/large in double to avoid undue underflow. ratio = (double)small / (double)large; if (ratio > 0x1.0p-24) { Real(w) += 0.5f*log1pf((float)(ratio*ratio)); } return w;}
/**************************************************************************** void cosisin(x, complex *z) returns cos(x) + i sin(x) computed using the x87 fsincos instruction. Called by: cexp, csin, ccos, csinh, and ccosh. ****************************************************************************/static void cosisin(double x, double complex *z) { Real(*z) = cos(x); Imag(*z) = sin(x);}
static void cosisinf(float x, float complex *z) { Real(*z) = cosf(x); Imag(*z) = sinf(x);}
/**************************************************************************** double complex csin(double complex z) returns the complex sine of its argument. sin(z) = -i sinh(iz)
Calls: csinh****************************************************************************/
double complex csin ( double complex z ) { double complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = csinh(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
float complex csinf ( float complex z ) { float complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = csinhf(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
/**************************************************************************** double complex ccos(double complex z) returns the complex cosine of its argument. cos(z) = cosh(iz)
Calls: ccosh****************************************************************************/
double complex ccos ( double complex z ) { double complex iz; Real(iz) = -Imag(z); Imag(iz) = Real(z); return ccosh(iz);}
float complex ccosf ( float complex z ) { float complex iz; Real(iz) = -Imag(z); Imag(iz) = Real(z); return ccoshf(iz);}
/**************************************************************************** double complex csinh(double complex z) returns the complex hyperbolic sine of its argument. The algorithm is based upon the identity: sinh(x + i*y) = cos(y)*sinh(x) + i*sin(y)*cosh(x). Signaling of spurious overflows, underflows, and invalids is avoided where possible.
Calls: expm1, cosisin****************************************************************************/
double complex csinh ( double complex z ) { static const double INF = __builtin_inf(); double complex w; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysign(Real(z), Imag(z)); return w; } // At this stage, x ≠ NaN. double absx = __builtin_fabs(Real(z)); double reducedx = absx; cosisin(Imag(z), &w); // set w = cos y + i sin y. Real(w) *= copysign(1.0, Real(z)); // w = signof(x) cos y + i sin y // Handle x = ±∞ cases. if ((absx == INF) && ((Imag(z) == INF) || (Imag(z) != Imag(z)) || (Imag(z) == 0.0))) { Real(w) = copysign(INF, Real(z)); return w; } // Handle x = 0 cases. if (absx == 0.0) { Real(w) = Real(z); // sign of zero needs to be right. return w; } // Argument reduction, if need be. (x is now a finite non-zero number) if ((reducedx < twiceExpOverflowThresh_d) && (reducedx > expOverflowThreshold_d)) { reducedx -= expOverflowThreshold_d; // argument reduction, this is exact. Real(w) *= expOverflowValue_d; // not exact, but good enough. Imag(w) *= expOverflowValue_d; // ditto. } double exm1 = expm1(reducedx); // any overflow here is deserved. if (absx < 0x1p-27) { // |x|^2 is less than an ulp of 1, so only the leading terms of the series for Real(w) *= absx; // cosh = 1 + .... and sinh = x + .... has any effect on the result. } else if (absx > 19.0) { // if |x| > 19, then e^-x is less than an ulp of e^x, so the smaller term in double halfExpX = 0.5 * (exm1 + 1.0); // cosh = (e^x + e^-x) / 2 has no effect, and similarly for Real(w) *= halfExpX; // sinh = (e^x - e^-x) / 2. // only scale the imag part if non-zero (to prevent NaN in overflow*zero) if (Imag(z) != 0.0) Imag(w) *= halfExpX; } else { // the "normal" case, we need to be careful. double twiceExpX = 2.0 * (exm1 + 1.0); /* we use the following to get cosh(x): * * expm1(x)*expm1(x) 2e^x + e^(2x) - 2e^x + 1 e^x + e^-x * 1 + ------------------- = -------------------------- = ------------ = cosh(x) * 2*(1 + expm1(x)) 2e^x 2 */ Imag(w) *= 1.0 + (exm1*exm1)/twiceExpX; /* we use the following to get sinh(x) (up to sign): * * 1 / expm1(x) \ e^(2x) - e^x + e^x - 1 e^x - e^-x * --- | expm1(x) + ------------- | = ------------------------ = ------------ = sinh(x) * 2 \ 1 + expm1(x) / 2e^x 2 */ Real(w) *= 0.5*exm1 + exm1/twiceExpX; } return w;}
float complex csinhf ( float complex z ) { static const float INFf = __builtin_inff(); static const double INF = __builtin_inf(); float complex w; double complex wd; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0f) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysignf(Real(z), Imag(z)); return w; } // At this stage, x ≠ NaN. double absx = (double)__builtin_fabsf(Real(z)); cosisin((double)Imag(z), &wd); // set w = cos y + i sin y. Real(wd) *= copysign(1.0, (double)Real(z)); // w = signof(x) cos y + i sin y // Handle x = ±∞ cases. if ((absx == INF) && ((Imag(z) == INFf) || (Imag(z) != Imag(z)) || (Imag(z) == 0.0f))) { Real(w) = copysignf(INFf, Real(z)); Imag(w) = (float)Imag(wd); return w; } // Handle x = 0 cases. if (absx == 0.0) { Real(w) = Real(z); // sign of zero needs to be right. Imag(w) = (float)Imag(wd); return w; } double exm1 = expm1(absx); // any overflow here is deserved. if (absx < 0x1p-27) { // |x|^2 is less than an ulp of 1, so only the leading terms of the series for Real(wd) *= absx; // cosh = 1 + .... and sinh = x + .... has any effect on the result. } else if (absx > 19.0) { // if |x| > 19, then e^-x is less than an ulp of e^x, so the smaller term in double halfExpX = 0.5 * (exm1 + 1.0); // cosh = (e^x + e^-x) / 2 has no effect, and similarly for Real(wd) *= halfExpX; // sinh = (e^x - e^-x) / 2. // only scale the imag part if non-zero (to prevent NaN in overflow*zero) if (Imag(z) != 0.0f) Imag(wd) *= halfExpX; } else { // the "normal" case, we need to be careful. double twiceExpX = 2.0 * (exm1 + 1.0); /* we use the following to get cosh(x): * * expm1(x)*expm1(x) 2e^x + e^(2x) - 2e^x + 1 e^x + e^-x * 1 + ------------------- = -------------------------- = ------------ = cosh(x) * 2*(1 + expm1(x)) 2e^x 2 */ Imag(wd) *= 1.0 + (exm1*exm1)/twiceExpX; /* we use the following to get sinh(x) (up to sign): * * 1 / expm1(x) \ e^(2x) - e^x + e^x - 1 e^x - e^-x * --- | expm1(x) + ------------- | = ------------------------ = ------------ = sinh(x) * 2 \ 1 + expm1(x) / 2e^x 2 */ Real(wd) *= 0.5*exm1 + exm1/twiceExpX; } Real(w) = (float)Real(wd); Imag(w) = (float)Imag(wd); return w;}
/**************************************************************************** double complex ccosh(double complex z) returns the complex hyperbolic cosine of its argument. The algorithm is based upon the identity: cosh(x + i*y) = cos(y)*cosh(x) + i*sin(y)*sinh(x). Signaling of spurious overflows, underflows, and invalids is avoided where possible.
Calls: expm1, cosisin****************************************************************************/
double complex ccosh ( double complex z ) { static const double INF = __builtin_inf(); double complex w; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysign(Real(z), Imag(z)); return w; } // At this stage, x ≠ NaN. double absx = __builtin_fabs(Real(z)); double reducedx = absx; cosisin(Imag(z), &w); // set w = cos y + i sin y. Imag(w) *= copysign(1.0, Real(z)); // w = cos y + i sin y * signof(x) // Handle x = ±∞ cases. if ((absx == INF) && ((Imag(z) == INF) || (Imag(z) != Imag(z)) || (Imag(z) == 0.0))) { Real(w) = INF; return w; } // Handle x = 0 cases. if (absx == 0.0) { Imag(w) = Real(z) * copysign(1.0, Imag(z)); // finesse the sign of zero. return w; } // Argument reduction, if need be. (x is now a finite non-zero number) if ((reducedx < twiceExpOverflowThresh_d) && (reducedx > expOverflowThreshold_d)) { reducedx -= expOverflowThreshold_d; // argument reduction, this is exact. Real(w) *= expOverflowValue_d; // not exact, but good enough. Imag(w) *= expOverflowValue_d; // ditto. } double exm1 = expm1(reducedx); // any overflow here is deserved. if (absx < 0x1p-27) { // |x|^2 is less than an ulp of 1, so only the leading terms of the series for Imag(w) *= absx; // cosh = 1 + .... and sinh = x + .... has any effect on the result. } else if (absx > 19.0) { // if |x| > 19, then e^-x is less than an ulp of e^x, so the smaller term in double halfExpX = 0.5 * (exm1 + 1.0); // cosh = (e^x + e^-x) / 2 has no effect, and similarly for Real(w) *= halfExpX; // sinh = (e^x - e^-x) / 2. // only scale the imag part if non-zero (to prevent NaN in overflow*zero) if (Imag(z) != 0.0) Imag(w) *= halfExpX; } else { // the "normal" case, we need to be careful. double twiceExpX = 2.0 * (exm1 + 1.0); /* we use the following to get cosh(x): * * expm1(x)*expm1(x) 2e^x + e^(2x) - 2e^x + 1 e^x + e^-x * 1 + ------------------- = -------------------------- = ------------ = cosh(x) * 2*(1 + expm1(x)) 2e^x 2 */ Real(w) *= 1.0 + (exm1*exm1)/twiceExpX; /* we use the following to get sinh(x) (up to sign): * * 1 / expm1(x) \ e^(2x) - e^x + e^x - 1 e^x - e^-x * --- | expm1(x) + ------------- | = ------------------------ = ------------ = sinh(x) * 2 \ 1 + expm1(x) / 2e^x 2 */ Imag(w) *= 0.5*exm1 + exm1/twiceExpX; } return w;}
float complex ccoshf ( float complex z ) { static const float INFf = __builtin_inff(); static const double INF = __builtin_inf(); double complex wd; float complex w; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0f) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysignf(Real(z), Imag(z)); return w; } // At this stage, x ≠ NaN. double absx = (double)__builtin_fabsf(Real(z)); cosisin((double)Imag(z), &wd); // set w = cos y + i sin y. Imag(wd) *= copysign(1.0, (double)Real(z)); // w = cos y + i sin y * signof(x) // Handle x = ±∞ cases. if ((absx == INF) && ((Imag(z) == INFf) || (Imag(z) != Imag(z)) || (Imag(z) == 0.0f))) { Real(w) = INFf; Imag(w) = (float)Imag(wd); return w; } // Handle x = 0 cases. if (absx == 0.0) { Imag(w) = Real(z) * copysignf(1.0f, Imag(z)); // finesse the sign of zero. Real(w) = (float)Real(wd); return w; } double exm1 = expm1(absx); // any overflow here is deserved. if (absx < 0x1p-27) { // |x|^2 is less than an ulp of 1, so only the leading terms of the series for Imag(wd) *= absx; // cosh = 1 + .... and sinh = x + .... has any effect on the result. } else if (absx > 19.0) { // if |x| > 19, then e^-x is less than an ulp of e^x, so the smaller term in double halfExpX = 0.5 * (exm1 + 1.0); // cosh = (e^x + e^-x) / 2 has no effect, and similarly for Real(wd) *= halfExpX; // sinh = (e^x - e^-x) / 2. // only scale the imag part if non-zero (to prevent NaN in overflow*zero) if (Imag(z) != 0.0) Imag(wd) *= halfExpX; } else { // the "normal" case, we need to be careful. double twiceExpX = 2.0 * (exm1 + 1.0); /* we use the following to get cosh(x): * * expm1(x)*expm1(x) 2e^x + e^(2x) - 2e^x + 1 e^x + e^-x * 1 + ------------------- = -------------------------- = ------------ = cosh(x) * 2*(1 + expm1(x)) 2e^x 2 */ Real(wd) *= 1.0 + (exm1*exm1)/twiceExpX; /* we use the following to get sinh(x) (up to sign): * * 1 / expm1(x) \ e^(2x) - e^x + e^x - 1 e^x - e^-x * --- | expm1(x) + ------------- | = ------------------------ = ------------ = sinh(x) * 2 \ 1 + expm1(x) / 2e^x 2 */ Imag(wd) *= 0.5*exm1 + exm1/twiceExpX; } Real(w) = (float)Real(wd); Imag(w) = (float)Imag(wd); return w;}
/**************************************************************************** double complex cexp(double complex z) returns the complex exponential of its argument. The algorithm is based upon the identity: exp(x + i*y) = cos(y)*exp(x) + i*sin(y)*exp(x). Signaling of spurious overflows and invalids is avoided where possible. CONSTANTS: expOverflowValue_d = exp(709.0) to double precision
Calls: cosisin and exp.****************************************************************************/
double complex cexp ( double complex z ) { static const double INF = __builtin_inf(); double complex w; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysign(Real(z), Imag(z)); return w; } // Handle x = -∞, y = ∞ or NaN: if ((Real(z) == -INF) && ((__builtin_fabs(Imag(z)) == INF) || (Imag(z) != Imag(z)))) { Real(w) = 0.0; Imag(w) = copysign(0.0, Imag(z)); return w; } if (Imag(z) == 0.0) { // exact exp(x + 0i) case. Real(w) = exp(Real(z)); Imag(w) = 0.0; return w; } // At this stage, x ≠ NaN, and extraordinary x = -∞ cases are sorted. y ≠ 0. cosisin(Imag(z), &w); // set w = cos(y) + i sin(y) // Handle x = +∞ cases. if ((Real(z) == INF) && ((Imag(z) == INF) || (Imag(z) != Imag(z)))) { Real(w) = INF; // cexp(∞ + yi) = ∞ + NaNi for y = NaN or ∞. return w; // cexp(∞ + yi) for finite y falls through. } // At this point, x ≠ NaN, +inf, y ≠ 0, and all remaining cases fall through double x = Real(z); if ((x < twiceExpOverflowThresh_d) && (x > expOverflowThreshold_d)) { x -= expOverflowThreshold_d; // argument reduction, this is exact. Real(w) *= expOverflowValue_d; // not exact, but good enough. Imag(w) *= expOverflowValue_d; // ditto. } double scale = exp(x); Real(w) *= scale; Imag(w) *= scale; return w;}
float complex cexpf ( float complex z ) { static const float INFf = __builtin_inff(); float complex w; // Handle x = NaN first if (Real(z) != Real(z)) { Real(w) = Real(z); if (Imag(z) == 0.0f) // cexp(NaN + 0i) = NaN + 0i Imag(w) = Imag(z); else // cexp(NaN + yi) = NaN + NaNi, for y ≠ 0 Imag(w) = copysignf(Real(z), Imag(z)); return w; } // Handle x = -∞, y = ∞ or NaN: if ((Real(z) == -INFf) && ((__builtin_fabsf(Imag(z)) == INFf) || (Imag(z) != Imag(z)))) { Real(w) = 0.0f; Imag(w) = copysignf(0.0f, Imag(z)); return w; } if (Imag(z) == 0.0f) { // exact exp(x + 0i) case. Real(w) = expf(Real(z)); Imag(w) = 0.0f; return w; } double complex wd; // At this stage, x ≠ NaN, and extraordinary x = -∞ cases are sorted. y ≠ 0. cosisin((double)Imag(z), &wd); // set w = cos(y) + i sin(y) // Handle x = +∞ cases. if ((Real(z) == INFf) && ((Imag(z) == INFf) || (Imag(z) != Imag(z)))) { Real(w) = INFf; // cexp(∞ + yi) = ∞ + NaNi for y = NaN or ∞. Imag(w) = (float)Imag(wd); return w; // cexp(∞ + yi) for finite y falls through. } // At this point, x ≠ NaN, +inf, y ≠ 0, and all remaining cases fall through
double scale = exp((double)Real(z)); Real(w) = (float)(scale*Real(wd)); Imag(w) = (float)(scale*Imag(wd)); return w;} /**************************************************************************** double complex cpow(double complex x, double complex y) returns the complex result of x^y. The algorithm is based upon the identity: x^y = cexp(y*clog(x)). Calls: clog, cexp.****************************************************************************/
double complex cpow ( double complex x, double complex y ) /* (complex x)^(complex y) */{ double complex logval,z; logval = clog(x); /* complex logarithm of x */ Real(z) = Real(y)*Real(logval) - Imag(y)*Imag(logval); /* multiply by y */ Imag(z) = Real(y)*Imag(logval) + Imag(y)*Real(logval); return (cexp(z)); /* return complex exponential */}
float complex cpowf ( float complex x, float complex y ) /* (complex x)^(complex y) */{ float complex logval,z; logval = clogf(x); /* complex logarithm of x */ Real(z) = Real(y)*Real(logval) - Imag(y)*Imag(logval); /* multiply by y */ Imag(z) = Real(y)*Imag(logval) + Imag(y)*Real(logval); return (cexpf(z)); /* return complex exponential */}
/**************************************************************************** double complex ctanh(double complex x) returns the complex hyperbolic tangent of its argument. The algorithm is from Kahan's paper and is based on the identity: tanh(x+i*y) = (sinh(2*x) + i*sin(2*y))/(cosh(2*x) + cos(2*y)) = (cosh(x)*sinh(x)*cscsq + i*tan(y))/(1+cscsq*sinh(x)*sinh(x)), where cscsq = 1/(cos(y)*cos(y). For large values of ze.re, spurious overflow and invalid signaling is avoided. CONSTANTS: FPKASINHOM4 = asinh(nextafterd(+infinity,0.0))/4.0 to double precision FPKINF = +infinity Calls: tan, sinh, sqrt, fabs, feholdexcept, feraiseexcept, feclearexcept, and feupdateenv.****************************************************************************/
double complex ctanh( double complex z ){ static const double INF = __builtin_inf(); double x = __builtin_fabs(Real(z)); double y = __builtin_fabs(Imag(z)); double sinhval, coshval, tanval, exm1, cscsq; double complex w; if (x == INF) { w = 1.0 + I*copysign(0.0, sin(2.0*y)); // ctanh(∞ + iy) = 1.0 ± i0 } else if (Imag(z) != Imag(z) || Real(z) != Real(z)) { if (Imag(z) == 0.0) { w = Real(z) + I*0.0; // ctanh(NaN + i0) = NaN + i0 } else { Real(w) = Real(z) + Imag(z); // ctanh(NaN) = NaN + iNaN Imag(w) = Real(w); } } else if (y == INF) { Real(w) = y - y; // ctanh(x + i∞) = NaN + iNaN (invalid) Imag(w) = Real(w); } else if (x > 19.0) { w = 1.0 + I*copysign(0.0, sin(2.0*y)); // if x is big, tanh(z) = 1 ± i0 } else { // edge case free! tanval = tan(y); cscsq = 1.0 + tanval*tanval; // cscsq = 1/cos^2(y) if (x < 0x1p-27) { coshval = 1.0; sinhval = x; } else { exm1 = expm1(x); coshval = 1.0 + 0.5*(exm1*exm1)/(exm1 + 1.0); sinhval = 0.5*(exm1 + exm1/(exm1 + 1.0)); } Real(w) = cscsq * coshval * sinhval / (1.0 + cscsq * sinhval * sinhval); Imag(w) = tanval / (1.0 + cscsq * sinhval * sinhval); } // Patch up signs of return value Real(w) = copysign(Real(w),Real(z)); Imag(w) *= copysign(1.0,Imag(z)); return w;}
float complex ctanhf( float complex z ){ static const float INFf = __builtin_inff(); float x = __builtin_fabsf(Real(z)); float y = __builtin_fabsf(Imag(z)); double sinhval, coshval, tanval, exm1, cscsq; float complex w; if (x == INFf) { w = 1.0f + I*copysignf(0.0f, sinf(2.0f*y)); // ctanh(∞ + iy) = 1.0 ± i0 } else if (Imag(z) != Imag(z) || Real(z) != Real(z)) { if (Imag(z) == 0.0f) { w = Real(z) + I*0.0f; // ctanh(NaN + i0) = NaN + i0 } else { Real(w) = Real(z) + Imag(z); // ctanh(NaN) = NaN + iNaN Imag(w) = Real(w); } } else if (y == INFf) { Real(w) = y - y; // ctanh(x + i∞) = NaN + iNaN (invalid) Imag(w) = Real(w); } else if (x > 19.0f) { w = 1.0f + I*copysignf(0.0f, sinf(2.0f*y)); // if x is big, tanh(z) = 1 ± i0 } else { // edge case free! tanval = (double)tanf(y); cscsq = 1.0 + tanval*tanval; // cscsq = 1/cos^2(y) if (x < 0x1p-13f) { coshval = 1.0; sinhval = x; } else { exm1 = (double)expm1f(x); coshval = 1.0 + 0.5*(exm1*exm1)/(exm1 + 1.0); sinhval = 0.5*(exm1 + exm1/(exm1 + 1.0)); } Real(w) = (float)(cscsq * coshval * sinhval / (1.0 + cscsq * sinhval * sinhval)); Imag(w) = (float)(tanval / (1.0 + cscsq * sinhval * sinhval)); } // Patch up signs of return value Real(w) = copysignf(Real(w),Real(z)); Imag(w) *= copysignf(1.0f,Imag(z)); return w;}
/**************************************************************************** double complex ctan(double complex x) returns the complex hyperbolic tangent of its argument. Per C99, i ctan(z) = ctanh(iz) Calls: ctanh****************************************************************************/
double complex ctan( double complex z ){ double complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = ctanh(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
float complex ctanf( float complex z ){ float complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = ctanhf(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;} /**************************************************************************** double complex casin(double complex z) returns the complex inverse sine of its argument. The algorithm is from Kahan's paper and is based on the formulae: real(casin(z)) = atan (real(z)/real(csqrt(1.0-z)*csqrt(1.0+z))) imag(casin(z)) = arcsinh(imag(csqrt(1.0-cconj(z))*csqrt(1.0+z))) Calls: arcsinh, csqrt, atan, feholdexcept, feclearexcept, feupdateenv.****************************************************************************/
double complex casin ( double complex z ){ double complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = casinh(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
float complex casinf ( float complex z ){ float complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = casinhf(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
/**************************************************************************** double complex casinh(double complex z) returns the complex inverse hyperbolic sine of its argument. We compute the function only in the upper-right quadrant of the complex plane, and use the facts that casinh(conj(z)) = conj(casinh(z)) and casinh is odd to get the values on the rest of the plane. within the upper-right quadrant, we use:
casinh(z) = z if |z| is small, ln(2z) if |z| is big, and a rather complicated expression for other values of z. Calls: asinh, csqrt, atan2.****************************************************************************/
double complex casinh ( double complex z ) { static const double INF = __builtin_inf(); static const double ln2 = 0x1.62e42fefa39ef358p-1; static const double sqrt1_2 = 0x1.6a09e667f3bcc908p-1; double complex w; double x = __builtin_fabs(Real(z)); double y = __builtin_fabs(Imag(z)); double u, xSquared, tmp; double complex sqrt1Plusiz, sqrt1PlusizBar; // If |z| == inf, then casinh(z) = inf + carg(z) if ((x == INF) || (y == INF)) { Real(w) = INF; Imag(w) = atan2(y,x); } // If z = NaN, casinh(z) = NaN, with the special case that casinh(NaN + i0) = NaN + i0. else if ((x != x) || (y != y)) { if (y == 0.0) w = z; else { Real(w) = x + y; Imag(w) = x + y; } }
// at this point x,y are finite, non-nan. else { // If z is small, then casinh(z) = z - z^3/6 + ... = z within half an ulp if ((x < 0x1p-27) && (y < 0x1p-27)) { Real(w) = x; Imag(w) = y; } // If z is big, then casinh(z) = log2 + log(z) + terms smaller than half an ulp else if ((x > 0x1p27) || (y > 0x1p27)) { w = clog(x + I*y); Real(w) += ln2; } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. */ else { // Compute sqrt1Plusiz = sqrt(1-y + ix) u = 1.0 - y; xSquared = (x < 0x1p-106 ? 0.0 : x*x); // Avoid underflows. Faster via mask? if (u == 0.0) { Real(sqrt1Plusiz) = sqrt1_2 * __builtin_sqrt(x); // Avoid spurious underflows in this case Imag(sqrt1Plusiz) = Real(sqrt1Plusiz); // by using the simpler formula. } else { // No underflow or overflow is possible. Real(sqrt1Plusiz) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + xSquared) + __builtin_fabs(u))); tmp = 0.5 * (x / Real(sqrt1Plusiz)); if (u < 0.0) { Imag(sqrt1Plusiz) = Real(sqrt1Plusiz); Real(sqrt1Plusiz) = tmp; } else { Imag(sqrt1Plusiz) = tmp; } } // Compute sqrt1PlusizBar = sqrt(1+y + ix). No underflow or overflow is possible. u = 1.0 + y; Real(sqrt1PlusizBar) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + xSquared) + u)); Imag(sqrt1PlusizBar) = x / (2.0*Real(sqrt1PlusizBar)); // Magic formulas from Kahan. Real(w) = asinh(Real(sqrt1Plusiz)*Imag(sqrt1PlusizBar) + Imag(sqrt1Plusiz)*Real(sqrt1PlusizBar)); Imag(w) = atan2(y, Real(sqrt1Plusiz)*Real(sqrt1PlusizBar) + Imag(sqrt1Plusiz)*Imag(sqrt1PlusizBar)); } } // Patch up signs to handle z in quadrants II - IV, using symmetry. Real(w) = copysign(Real(w), Real(z)); Imag(w) = copysign(Imag(w), Imag(z)); return w;}
float complex casinhf ( float complex z ) { static const float INFf = __builtin_inff(); static const float ln2f = 0x1.62e42fefa39ef358p-1f; static const float sqrt1_2f = 0x1.6a09e667f3bcc908p-1f; float complex w; float x = __builtin_fabsf(Real(z)); float y = __builtin_fabsf(Imag(z)); float u, xSquared, tmp; float complex sqrt1Plusiz, sqrt1PlusizBar; // If |z| == inf, then casinh(z) = inf + carg(z) if ((x == INFf) || (y == INFf)) { Real(w) = INFf; Imag(w) = atan2f(y,x); } // If z = NaN, casinh(z) = NaN, with the special case that casinh(NaN + i0) = NaN + i0. else if ((x != x) || (y != y)) { if (y == 0.0f) w = z; else { Real(w) = x + y; Imag(w) = x + y; } } // at this point x,y are finite, non-nan. else { // If z is small, then casinhf(z) = z - z^3/6 + ... = z within half an ulp if ((x < 0x1p-13f) && (y < 0x1p-13f)) { Real(w) = x; Imag(w) = y; } // If z is big, then casinh(z) = log2 + log(z) + terms smaller than half an ulp else if ((x > 0x1p13f) || (y > 0x1p13f)) { w = clogf(x + I*y); Real(w) += ln2f; } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. */ else { // Compute sqrt1Plusiz = sqrt(1-y + ix) u = 1.0f - y; xSquared = (x < 0x1p-52f ? 0.0f : x*x); // Avoid underflows. Faster via mask? if (u == 0.0f) { Real(sqrt1Plusiz) = sqrt1_2f * __builtin_sqrtf(x); // Avoid spurious underflows in this case Imag(sqrt1Plusiz) = Real(sqrt1Plusiz); // by using the simpler formula. } else { // No underflow or overflow is possible. Real(sqrt1Plusiz) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + xSquared) + __builtin_fabsf(u))); tmp = 0.5f * (x / Real(sqrt1Plusiz)); if (u < 0.0f) { Imag(sqrt1Plusiz) = Real(sqrt1Plusiz); Real(sqrt1Plusiz) = tmp; } else { Imag(sqrt1Plusiz) = tmp; } } // Compute sqrt1PlusizBar = sqrt(1+y + ix). No underflow or overflow is possible. u = 1.0f + y; Real(sqrt1PlusizBar) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + xSquared) + u)); Imag(sqrt1PlusizBar) = x / (2.0f*Real(sqrt1PlusizBar)); // Magic formulas from Kahan. Real(w) = asinhf(Real(sqrt1Plusiz)*Imag(sqrt1PlusizBar) + Imag(sqrt1Plusiz)*Real(sqrt1PlusizBar)); Imag(w) = atan2f(y, Real(sqrt1Plusiz)*Real(sqrt1PlusizBar) + Imag(sqrt1Plusiz)*Imag(sqrt1PlusizBar)); } } // Patch up signs to handle z in quadrants II - IV, using symmetry. Real(w) = copysignf(Real(w), Real(z)); Imag(w) = copysignf(Imag(w), Imag(z)); return w;}
/**************************************************************************** double complex cacos(double complex z) returns the complex inverse cosine of its argument. The algorithm is from Kahan's paper and is based on the formulae: real(cacos(z)) = 2.0*atan(real(csqrt(1.0-z)/real(csqrt(1.0+z)))) imag(cacos(z)) = arcsinh(imag(csqrt(1.0-z)*csqrt(cconj(1.0+z)))) Calls: arcsinh, csqrt, atan, feholdexcept, feclearexcept, feupdateenv.****************************************************************************/
double complex cacos ( double complex z ){ static const double INF = __builtin_inf(); static const double ln2 = 0x1.62e42fefa39ef358p-1; static const double sqrt1_2 = 0x1.6a09e667f3bcc908p-1; static const double pi2 = 0x1.921fb54442d1846ap0;
double complex w; double x = __builtin_fabs(Real(z)); double y = __builtin_fabs(Imag(z)); double u, ySquared, tmp; double complex sqrt1Plusz, sqrt1Minusz; // If |z| == inf, then cacos(z) = carg(z) - inf * I if ((x == INF) || (y == INF)) { Imag(w) = -INF; Real(w) = atan2(y,x); } // If z = NaN, cacos(z) = NaN, with the special case that cacos(0 + iNaN) = π/2 + iNaN. else if ((x != x) || (y != y)) { if (x == 0.0) Real(w) = pi2; else Real(w) = x + y; Imag(w) = x + y; } // at this point x,y are finite, non-nan. else { // If z is small, then cacos(z) = π/2 - z + z^3/6 + ... = π/2 - z within half an ulp if ((x < 0x1p-27) && (y < 0x1p-27)) { Real(w) = pi2 - x; Imag(w) = -y; } // If z is big, then cacos(z) = -i * (log2 + log(z)) + terms smaller than half an ulp else if ((x > 0x1p27) || (y > 0x1p27)) { w = -I*(clog(x + I*y) + ln2); } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Real(w) = 2*atan2( Re(sqrt(1-z)), Re(sqrt(1+z)) ) * Imag(w) = asinh( Im( sqrt(1-z)*sqrt(1+conj(z)) ) ) * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. Analysis of roundoff is a bit harder, but goes though just fine. */ else { ySquared = (y < 0x1p-106 ? 0.0 : y*y); // Avoid underflows. Faster via mask? // Compute sqrt1Plusz = sqrt(1+x + iy) u = 1.0 + x; Real(sqrt1Plusz) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + ySquared) + u)); Imag(sqrt1Plusz) = 0.5 * (y / Real(sqrt1Plusz)); // Compute sqrt1Minusz = sqrt(1-x - iy) u = 1.0 - x; if (u == 0.0) { Real(sqrt1Minusz) = sqrt1_2 * __builtin_sqrt(y); // Avoid spurious underflows in this case Imag(sqrt1Minusz) = -Real(sqrt1Minusz); // by using the simpler formula. } else { // No underflow or overflow is possible. Real(sqrt1Minusz) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + ySquared) + __builtin_fabs(u))); tmp = 0.5 * (y / Real(sqrt1Minusz)); if (u < 0.0) { Imag(sqrt1Minusz) = -Real(sqrt1Minusz); Real(sqrt1Minusz) = tmp; } else { Imag(sqrt1Minusz) = -tmp; } } // Magic formulas from Kahan. Real(w) = 2.0 * atan2(Real(sqrt1Minusz), Real(sqrt1Plusz)); Imag(w) = asinh( Real(sqrt1Plusz)*Imag(sqrt1Minusz) - Imag(sqrt1Plusz)*Real(sqrt1Minusz) ); } } // Patch up signs to handle z in quadrants II, III & IV, using symmetry. Imag(w) = copysign(Imag(w), -Imag(z)); if (Real(z) < 0.0) Real(w) = 2.0 * pi2 - Real(w); // No undue cancellation is possible here - Real(w) < π/2. return w;} float complex cacosf ( float complex z ){ static const float INFf = __builtin_inff(); static const float ln2f = 0x1.62e42fefa39ef358p-1f; static const float pi2f = 0x1.921fb54442d1846ap0f; static const float sqrt1_2f = 0x1.6a09e667f3bcc908p-1f; float complex w; float x = __builtin_fabsf(Real(z)); float y = __builtin_fabsf(Imag(z)); float u, ySquared, tmp; float complex sqrt1Plusz, sqrt1Minusz; // If |z| == inf, then cacos(z) = carg(z) - inf i if ((x == INFf) || (y == INFf)) { Imag(w) = -INFf; Real(w) = atan2f(y,x); } // If z = NaN, cacos(z) = NaN, with the special case that cacos(0 + iNaN) = π/2 + iNaN. else if ((x != x) || (y != y)) { if (x == 0.0f) Real(w) = pi2f; else Real(w) = x + y; Imag(w) = x + y; } // at this point x,y are finite, non-nan. else { // If z is small, then cacos(z) = π/2 - z + z^3/6 + ... = π/2 - z within half an ulp if ((x < 0x1p-13f) && (y < 0x1p-13f)) { Real(w) = pi2f - x; Imag(w) = -y; } // If z is big, then cacos(z) = -i * (log2 + log(z)) + terms smaller than half an ulp else if ((x > 0x1p13f) || (y > 0x1p13f)) { w = -I*(clogf(x + I*y) + ln2f); } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Real(w) = 2*atan2( Re(sqrt(1-z)), Re(sqrt(1+z)) ) * Imag(w) = asinh( Im( sqrt(1-z)*sqrt(1+conj(z)) ) ) * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. Analysis of roundoff is a bit harder, but goes though just fine. */ else { ySquared = (y < 0x1p-52f ? 0.0f : y*y); // Avoid underflows. Faster via mask? // Compute sqrt1Plusz = sqrt(1+x + iy) u = 1.0f + x; Real(sqrt1Plusz) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + ySquared) + u)); Imag(sqrt1Plusz) = 0.5f * (y / Real(sqrt1Plusz)); // Compute sqrt1Minusz = sqrt(1-x - iy) u = 1.0f - x; if (u == 0.0f) { Real(sqrt1Minusz) = sqrt1_2f * __builtin_sqrtf(y); Imag(sqrt1Minusz) = -Real(sqrt1Minusz); } else { Real(sqrt1Minusz) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + ySquared) + __builtin_fabsf(u))); tmp = 0.5f * (y / Real(sqrt1Minusz)); if (u < 0.0f) { Imag(sqrt1Minusz) = -Real(sqrt1Minusz); Real(sqrt1Minusz) = tmp; } else { Imag(sqrt1Minusz) = -tmp; } } // Magic formulas from Kahan. Real(w) = 2.0f * atan2f(Real(sqrt1Minusz),Real(sqrt1Plusz)); Imag(w) = asinhf( Real(sqrt1Plusz)*Imag(sqrt1Minusz) - Imag(sqrt1Plusz)*Real(sqrt1Minusz) ); } } // Patch up signs to handle z in quadrants II, III & IV, using symmetry. Imag(w) = copysignf(Imag(w), -Imag(z)); if (Real(z) < 0.0f) Real(w) = 2.0f * pi2f - Real(w); // No undue cancellation is possible here - Real(w) < π/2. return w;}
/**************************************************************************** double complex cacosh(double complex z) returns the complex inverse hyperbolic`cosine of its argument. The algorithm is from Kahan's paper and is based on the formulae: real(cacosh(z)) = arcsinh(real(csqrt(cconj(z)-1.0)*csqrt(z+1.0))) imag(cacosh(z)) = 2.0*atan(imag(csqrt(z-1.0)/real(csqrt(z+1.0)))) Calls: arcsinh, csqrt, atan, feholdexcept, feclearexcept, feupdateenv.****************************************************************************/
double complex cacosh ( double complex z ){ static const double INF = __builtin_inf(); static const double ln2 = 0x1.62e42fefa39ef358p-1; static const double sqrt1_2 = 0x1.6a09e667f3bcc908p-1; static const double pi2 = 0x1.921fb54442d1846ap0; double complex w; double x = __builtin_fabs(Real(z)); double y = __builtin_fabs(Imag(z)); double u, ySquared, tmp; double complex sqrtzPlus1, sqrtzMinus1; // If |z| == inf, then cacosh(z) = inf + carg(z) * I if ((x == INF) || (y == INF)) { Imag(w) = atan2(y,x); Real(w) = INF; } // If z = NaN, cacosh(z) = NaN. else if ((x != x) || (y != y)) { Real(w) = x + y; Imag(w) = x + y; } // at this point x,y are finite, non-nan. else { // If z is small, then cacosh(z) = I*(π/2 - z + z^3/6 + ...) = I*(π/2 - z) within half an ulp if ((x < 0x1p-27) && (y < 0x1p-27)) { Real(w) = y; Imag(w) = pi2 - x; } // If z is big, then cacosh(z) = (log2 + log(z)) + terms smaller than half an ulp else if ((x > 0x1p27) || (y > 0x1p27)) { w = clog(x + I*y) + ln2; } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Real(w) = asinh(real(csqrt(cconj(z)-1.0)*csqrt(z+1.0))) * Imag(w) = 2.0*atan2(imag(csqrt(z-1.0))/real(csqrt(z+1.0))) * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. Analysis of roundoff is a bit harder, but goes though just fine. */ else { ySquared = (y < 0x1p-106 ? 0.0 : y*y); // Avoid underflows. Faster via mask? // Compute sqrt1Plusz = sqrt(x+1 + iy) u = x + 1.0; Real(sqrtzPlus1) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + ySquared) + u)); Imag(sqrtzPlus1) = 0.5 * (y / Real(sqrtzPlus1)); // Compute sqrt1Minusz = sqrt(x-1 + iy) u = x - 1.0; if (u == 0.0) { Real(sqrtzMinus1) = sqrt1_2 * __builtin_sqrt(y); // Avoid spurious underflows in this case Imag(sqrtzMinus1) = Real(sqrtzMinus1); // by using the simpler formula. } else { // No underflow or overflow is possible. Real(sqrtzMinus1) = __builtin_sqrt(0.5*(__builtin_sqrt(u*u + ySquared) + __builtin_fabs(u))); tmp = 0.5 * (y / Real(sqrtzMinus1)); if (u < 0.0) { Imag(sqrtzMinus1) = Real(sqrtzMinus1); Real(sqrtzMinus1) = tmp; } else { Imag(sqrtzMinus1) = tmp; } } // Magic formulas from Kahan. Real(w) = asinh( Real(sqrtzPlus1)*Real(sqrtzMinus1) + Imag(sqrtzPlus1)*Imag(sqrtzMinus1) ); Imag(w) = 2.0*atan2( Imag(sqrtzMinus1) , Real(sqrtzPlus1) ); } } // Patch up signs to handle z in quadrants II, III & IV, using symmetry. if (Real(z) < 0.0) Imag(w) = 2.0 * pi2 - Imag(w); // No undue cancellation is possible here - Real(w) < π/2. Imag(w) = copysign(Imag(w), Imag(z)); return w;}
float complex cacoshf ( float complex z ){ static const float INFf = __builtin_inff(); static const float ln2f = 0x1.62e42fefa39ef358p-1f; static const float sqrt1_2f = 0x1.6a09e667f3bcc908p-1f; static const float pi2f = 0x1.921fb54442d1846ap0f; float complex w; float x = __builtin_fabsf(Real(z)); float y = __builtin_fabsf(Imag(z)); float u, ySquared, tmp; float complex sqrtzPlus1, sqrtzMinus1; // If |z| == inf, then cacosh(z) = inf + carg(z) * I if ((x == INFf) || (y == INFf)) { Imag(w) = atan2f(y,x); Real(w) = INFf; } // If z = NaN, cacosh(z) = NaN. else if ((x != x) || (y != y)) { Real(w) = x + y; Imag(w) = x + y; } // at this point x,y are finite, non-nan. else { // If z is small, then cacosh(z) = I*(π/2 - z + z^3/6 + ...) = I*(π/2 - z) within half an ulp if ((x < 0x1p-13f) && (y < 0x1p-13f)) { Real(w) = y; Imag(w) = pi2f - x; } // If z is big, then cacosh(z) = (log2 + log(z)) + terms smaller than half an ulp else if ((x > 0x1p13f) || (y > 0x1p13f)) { w = clogf(x + I*y) + ln2f; } /* Otherwise, use the expressions from Kahan's "Much ado about nothing's sign bit..." * * Real(w) = asinh(real(csqrt(cconj(z)-1.0)*csqrt(z+1.0))) * Imag(w) = 2.0*atan2(imag(csqrt(z-1.0))/real(csqrt(z+1.0))) * * Derivation of these formulats boggles the mind, but they are easily verified with the * Monodromy theorem. Analysis of roundoff is a bit harder, but goes though just fine. */ else { ySquared = (y < 0x1p-52f ? 0.0f : y*y); // Avoid underflows. Faster via mask? // Compute sqrt1Plusz = sqrt(x+1 + iy) u = x + 1.0f; Real(sqrtzPlus1) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + ySquared) + u)); Imag(sqrtzPlus1) = 0.5f * (y / Real(sqrtzPlus1)); // Compute sqrt1Minusz = sqrt(x-1 + iy) u = x - 1.0f; if (u == 0.0f) { Real(sqrtzMinus1) = sqrt1_2f * __builtin_sqrtf(y); // Avoid spurious underflows in this case Imag(sqrtzMinus1) = Real(sqrtzMinus1); // by using the simpler formula. } else { // No underflow or overflow is possible. Real(sqrtzMinus1) = __builtin_sqrtf(0.5f*(__builtin_sqrtf(u*u + ySquared) + __builtin_fabsf(u))); tmp = 0.5f * (y / Real(sqrtzMinus1)); if (u < 0.0f) { Imag(sqrtzMinus1) = Real(sqrtzMinus1); Real(sqrtzMinus1) = tmp; } else { Imag(sqrtzMinus1) = tmp; } } // Magic formulas from Kahan. Real(w) = asinhf( Real(sqrtzPlus1)*Real(sqrtzMinus1) + Imag(sqrtzPlus1)*Imag(sqrtzMinus1) ); Imag(w) = 2.0f*atan2f( Imag(sqrtzMinus1) , Real(sqrtzPlus1) ); } } // Patch up signs to handle z in quadrants II, III & IV, using symmetry. if (Real(z) < 0.0f) Imag(w) = 2.0f * pi2f - Imag(w); // No undue cancellation is possible here - Real(w) < π/2. Imag(w) = copysignf(Imag(w), Imag(z)); return w;}
/**************************************************************************** double complex catan(double complex z) returns the complex inverse tangent of its argument. The algorithm is from Kahan's paper and is based on the formula: catan(z) = i*(clog(1.0-i*z) - clog(1+i*z))/2.0. CONSTANTS: FPKTHETA = sqrt(nextafterd(+INF,0.0))/4.0 FPKRHO = 1.0/FPKTHETA FPKPI2 = pi/2.0 FPKPI = pi Calls: copysign, fabs, xdivc, sqrt, log, atan, log1p, and carg.****************************************************************************/
double complex catan ( double complex z ){ double complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = catanh(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
float complex catanf ( float complex z ){ float complex iz, iw, w; Real(iz) = -Imag(z); Imag(iz) = Real(z); iw = catanhf(iz); Real(w) = Imag(iw); Imag(w) = -Real(iw); return w;}
/**************************************************************************** double complex catanh(double complex z) returns the complex inverse hyperbolic tangent of its argument. The algorithm is from Kahan's paper and is based on the formula: catanh(z) = (clog(1.0 + z) - clog(1 - z))/2.0. CONSTANTS: FPKTHETA = sqrt(nextafterd(+INF,0.0))/4.0 FPKRHO = 1.0/FPKTHETA FPKPI2 = pi/2.0 FPKPI = pi Calls: copysign, fabs, xdivc, sqrt, log, atan, log1p, and carg.****************************************************************************/
double complex catanh( double complex z ) { double complex ctemp, w; double t1, t2, xi, eta, beta; beta = copysign(1.0,Real(z)); /* copes with unsigned zero */ Imag(z) = -beta*Imag(z); /* transform real & imag components */ Real(z) = beta*Real(z); if ((Real(z) > FPKTHETA) || (fabs(Imag(z)) > FPKTHETA)) { eta = copysign(M_PI_2,Imag(z)); /* avoid overflow */ ctemp = xdivc(1.0,z); xi = Real(ctemp); } else if (Real(z) == 1.0) { t1 = fabs(Imag(z)) + FPKRHO; xi = log(sqrt(sqrt(4.0 + t1*t1))/sqrt(fabs(Imag(z)))); eta = 0.5*copysign(M_PI-atan(2.0/(fabs(Imag(z))+FPKRHO)),Imag(z)); } else { /* usual case */ t2 = fabs(Imag(z)) + FPKRHO; t1 = 1.0 - Real(z); t2 = t2*t2; xi = 0.25*log1p(4.0*Real(z)/(t1*t1 + t2)); Real(ctemp) = (1.0 - Real(z))*(1.0 + Real(z)) - t2; Imag(ctemp) = Imag(z) + Imag(z); eta = 0.5*carg(ctemp); } Real(w) = beta*xi; /* fix up signs of result */ Imag(w) = -beta*eta; return w;}
float complex catanhf( float complex z ) { float complex ctemp, w; float t1, t2, xi, eta, beta; beta = copysignf(1.0f,Real(z)); /* copes with unsigned zero */ Imag(z) = -beta*Imag(z); /* transform real & imag components */ Real(z) = beta*Real(z); if ((Real(z) > FPKTHETAf) || (fabsf(Imag(z)) > FPKTHETAf)) { eta = copysignf((float) M_PI_2,Imag(z)); /* avoid overflow */ ctemp = xdivcf(1.0f,z); xi = Real(ctemp); } else if (Real(z) == 1.0f) { t1 = fabsf(Imag(z)) + FPKRHOf; xi = logf(sqrtf(sqrtf(4.0f + t1*t1))/sqrtf(fabsf(Imag(z)))); eta = 0.5f*copysignf((float)( M_PI-atan(2.0f/(fabsf(Imag(z))+FPKRHOf))),Imag(z)); } else { /* usual case */ t2 = fabsf(Imag(z)) + FPKRHOf; t1 = 1.0f - Real(z); t2 = t2*t2; xi = 0.25f*log1pf(4.0f*Real(z)/(t1*t1 + t2)); Real(ctemp) = (1.0f - Real(z))*(1.0f + Real(z)) - t2; Imag(ctemp) = Imag(z) + Imag(z); eta = 0.5f*cargf(ctemp); } Real(w) = beta*xi; /* fix up signs of result */ Imag(w) = -beta*eta; return w;}
/* conj(), creal(), and cimag() are gcc built ins. */double creal( double complex z ){ return __builtin_creal(z);}
float crealf( float complex z ){ return __builtin_crealf(z);}
double cimag( double complex z ){ return __builtin_cimag(z);}
float cimagf( float complex z ){ return __builtin_cimagf(z);}
double complex conj( double complex z ){ return __builtin_conj(z);}
float complex conjf( float complex z ){ return __builtin_conjf(z);}
double complex cproj( double complex z ){ static const double inf = __builtin_inf(); double u = __builtin_fabs(Real(z)); double v = __builtin_fabs(Imag(z)); if ((u == inf) || (v == inf)) return inf + I*copysign(0.0, Imag(z)); else return z;}
float complex cprojf( float complex z ){ static const double inf = __builtin_inff(); float u = __builtin_fabsf(Real(z)); float v = __builtin_fabsf(Imag(z)); if ((u == inf) || (v == inf)) return inf + I*copysignf(0.0f, Imag(z)); else return z; }