blob: bea15cdc4e0a575563c5f1708365edbc1540b533 [file] [log] [blame]
Daniel Dunbarb3a69012009-06-26 16:47:03 +00001//===-- divsc3.c - Implement __divsc3 -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements __divsc3 for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "int_lib.h"
15#include <math.h>
16#include <complex.h>
17
18// Returns: the quotient of (a + ib) / (c + id)
19
20float _Complex
21__divsc3(float __a, float __b, float __c, float __d)
22{
23 int __ilogbw = 0;
24 float __logbw = logbf(fmaxf(fabsf(__c), fabsf(__d)));
25 if (isfinite(__logbw))
26 {
27 __ilogbw = (int)__logbw;
28 __c = scalbnf(__c, -__ilogbw);
29 __d = scalbnf(__d, -__ilogbw);
30 }
31 float __denom = __c * __c + __d * __d;
32 float _Complex z;
33 __real__ z = scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
34 __imag__ z = scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
35 if (isnan(__real__ z) && isnan(__imag__ z))
36 {
37 if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
38 {
39 __real__ z = copysignf(INFINITY, __c) * __a;
40 __imag__ z = copysignf(INFINITY, __c) * __b;
41 }
42 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
43 {
44 __a = copysignf(isinf(__a) ? 1 : 0, __a);
45 __b = copysignf(isinf(__b) ? 1 : 0, __b);
46 __real__ z = INFINITY * (__a * __c + __b * __d);
47 __imag__ z = INFINITY * (__b * __c - __a * __d);
48 }
49 else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
50 {
51 __c = copysignf(isinf(__c) ? 1 : 0, __c);
52 __d = copysignf(isinf(__d) ? 1 : 0, __d);
53 __real__ z = 0 * (__a * __c + __b * __d);
54 __imag__ z = 0 * (__b * __c - __a * __d);
55 }
56 }
57 return z;
58}