1 /* go-matherr.c -- a Go version of the matherr function.
3 Copyright 2012 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 /* The gccgo version of the math library calls libc functions. On
8 some systems, such as Solaris, those functions will call matherr on
9 exceptional conditions. This is a version of matherr appropriate
10 for Go, one which returns the values that the Go math library
11 expects. This is fine for pure Go programs. For mixed Go and C
12 programs this will be problematic if the C programs themselves use
13 matherr. Normally the C version of matherr will override this, and
14 the Go code will just have to cope. If this turns out to be too
15 problematic we can change to run pure Go code in the math library
16 on systems that use matherr. */
23 #if defined(HAVE_MATHERR) && defined(HAVE_STRUCT_EXCEPTION)
25 #define PI 3.14159265358979323846264338327950288419716939937510582097494459
28 matherr (struct exception
* e
)
32 if (e
->type
!= DOMAIN
)
36 if (__builtin_strcmp (n
, "acos") == 0
37 || __builtin_strcmp (n
, "asin") == 0)
38 e
->retval
= __builtin_nan ("");
39 else if (__builtin_strcmp (n
, "atan2") == 0)
41 if (e
->arg1
== 0 && e
->arg2
== 0)
46 if (__builtin_memcmp (&e
->arg2
, &nz
, sizeof (double)) != 0)
49 e
->retval
= copysign (PI
, e
->arg1
);
54 else if (__builtin_strcmp (n
, "log") == 0
55 || __builtin_strcmp (n
, "log10") == 0)
56 e
->retval
= __builtin_nan ("");
57 else if (__builtin_strcmp (n
, "pow") == 0)
60 e
->retval
= __builtin_nan ("");
61 else if (e
->arg1
== 0 && e
->arg2
== 0)
63 else if (e
->arg1
== 0 && e
->arg2
< 0)
67 if (modf (e
->arg2
, &i
) == 0 && ((int64_t) i
& 1) == 1)
68 e
->retval
= copysign (__builtin_inf (), e
->arg1
);
70 e
->retval
= __builtin_inf ();
75 else if (__builtin_strcmp (n
, "sqrt") == 0)
78 e
->retval
= __builtin_nan ("");