* c-c++-common/ubsan/sanitize-recover-7.c (dg-options): Add -w.
[official-gcc.git] / libgo / runtime / go-matherr.c
blobd620ba08bad8af0f48747a8b1d44ebefe49b4d3c
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. */
18 #include <math.h>
19 #include <stdint.h>
21 #include "config.h"
23 #if defined(HAVE_MATHERR) && defined(HAVE_STRUCT_EXCEPTION)
25 #define PI 3.14159265358979323846264338327950288419716939937510582097494459
27 int
28 matherr (struct exception* e)
30 const char *n;
32 if (e->type != DOMAIN)
33 return 0;
35 n = e->name;
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)
43 double nz;
45 nz = -0.0;
46 if (__builtin_memcmp (&e->arg2, &nz, sizeof (double)) != 0)
47 e->retval = e->arg1;
48 else
49 e->retval = copysign (PI, e->arg1);
51 else
52 return 0;
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)
59 if (e->arg1 < 0)
60 e->retval = __builtin_nan ("");
61 else if (e->arg1 == 0 && e->arg2 == 0)
62 e->retval = 1.0;
63 else if (e->arg1 == 0 && e->arg2 < 0)
65 double i;
67 if (modf (e->arg2, &i) == 0 && ((int64_t) i & 1) == 1)
68 e->retval = copysign (__builtin_inf (), e->arg1);
69 else
70 e->retval = __builtin_inf ();
72 else
73 return 0;
75 else if (__builtin_strcmp (n, "sqrt") == 0)
77 if (e->arg1 < 0)
78 e->retval = __builtin_nan ("");
79 else
80 return 0;
82 else
83 return 0;
85 return 1;
88 #endif