source: azure_iot_hub/trunk/musl-1.1.18/src/complex/csqrt.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 3.1 KB
Line 
1/* origin: FreeBSD /usr/src/lib/msun/src/s_csqrt.c */
2/*-
3 * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include "libm.h"
29
30/*
31 * gcc doesn't implement complex multiplication or division correctly,
32 * so we need to handle infinities specially. We turn on this pragma to
33 * notify conforming c99 compilers that the fast-but-incorrect code that
34 * gcc generates is acceptable, since the special cases have already been
35 * handled.
36 */
37#pragma STDC CX_LIMITED_RANGE ON
38
39/* We risk spurious overflow for components >= DBL_MAX / (1 + sqrt(2)). */
40#define THRESH 0x1.a827999fcef32p+1022
41
42double complex csqrt(double complex z)
43{
44 double complex result;
45 double a, b;
46 double t;
47 int scale;
48
49 a = creal(z);
50 b = cimag(z);
51
52 /* Handle special cases. */
53 if (z == 0)
54 return CMPLX(0, b);
55 if (isinf(b))
56 return CMPLX(INFINITY, b);
57 if (isnan(a)) {
58 t = (b - b) / (b - b); /* raise invalid if b is not a NaN */
59 return CMPLX(a, t); /* return NaN + NaN i */
60 }
61 if (isinf(a)) {
62 /*
63 * csqrt(inf + NaN i) = inf + NaN i
64 * csqrt(inf + y i) = inf + 0 i
65 * csqrt(-inf + NaN i) = NaN +- inf i
66 * csqrt(-inf + y i) = 0 + inf i
67 */
68 if (signbit(a))
69 return CMPLX(fabs(b - b), copysign(a, b));
70 else
71 return CMPLX(a, copysign(b - b, b));
72 }
73 /*
74 * The remaining special case (b is NaN) is handled just fine by
75 * the normal code path below.
76 */
77
78 /* Scale to avoid overflow. */
79 if (fabs(a) >= THRESH || fabs(b) >= THRESH) {
80 a *= 0.25;
81 b *= 0.25;
82 scale = 1;
83 } else {
84 scale = 0;
85 }
86
87 /* Algorithm 312, CACM vol 10, Oct 1967. */
88 if (a >= 0) {
89 t = sqrt((a + hypot(a, b)) * 0.5);
90 result = CMPLX(t, b / (2 * t));
91 } else {
92 t = sqrt((-a + hypot(a, b)) * 0.5);
93 result = CMPLX(fabs(b) / (2 * t), copysign(t, b));
94 }
95
96 /* Rescale. */
97 if (scale)
98 result *= 2;
99 return result;
100}
Note: See TracBrowser for help on using the repository browser.