2017-11-20 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-cdiv.c
blob0355e26fc8ed2a955bddc7dcd41e180d33691f66
1 /* go-cdiv.c -- complex division routines
3 Copyright 2013 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include <complex.h>
8 #include <math.h>
10 /* Calls to these functions are generated by the Go frontend for
11 division of complex64 or complex128. We use these because Go's
12 complex division expects slightly different results from the GCC
13 default. When dividing NaN+1.0i / 0+0i, Go expects NaN+NaNi but
14 GCC generates NaN+Infi. NaN+Infi seems wrong seems the rules of
15 C99 Annex G specify that if either side of a complex number is Inf,
16 the the whole number is Inf, but an operation involving NaN ought
17 to result in NaN, not Inf. */
19 complex float
20 __go_complex64_div (complex float a, complex float b)
22 if (__builtin_expect (b == 0, 0))
24 if (!isinf (crealf (a))
25 && !isinf (cimagf (a))
26 && (isnan (crealf (a)) || isnan (cimagf (a))))
28 /* Pass "1" to nanf to match math/bits.go. */
29 return nanf("1") + nanf("1")*I;
32 return a / b;
35 complex double
36 __go_complex128_div (complex double a, complex double b)
38 if (__builtin_expect (b == 0, 0))
40 if (!isinf (creal (a))
41 && !isinf (cimag (a))
42 && (isnan (creal (a)) || isnan (cimag (a))))
44 /* Pass "1" to nan to match math/bits.go. */
45 return nan("1") + nan("1")*I;
48 return a / b;