diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-02-08 22:35:30 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-02-08 22:35:30 +0000 |
commit | d2b480bc96f6ca12d7de236206e5e82e3cbabd57 (patch) | |
tree | 4a8dab4b58205b5450ab14c370dd544cc25f9a09 /libgo/runtime | |
parent | 09839cde4cf413ed731035886ecbc8ece14a7451 (diff) | |
download | gcc-d2b480bc96f6ca12d7de236206e5e82e3cbabd57.zip gcc-d2b480bc96f6ca12d7de236206e5e82e3cbabd57.tar.gz gcc-d2b480bc96f6ca12d7de236206e5e82e3cbabd57.tar.bz2 |
runtime: Add matherr function when appropriate.
From-SVN: r184024
Diffstat (limited to 'libgo/runtime')
-rw-r--r-- | libgo/runtime/go-matherr.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libgo/runtime/go-matherr.c b/libgo/runtime/go-matherr.c new file mode 100644 index 0000000..786f316 --- /dev/null +++ b/libgo/runtime/go-matherr.c @@ -0,0 +1,88 @@ +/* go-matherr.c -- a Go version of the matherr function. + + Copyright 2012 The Go Authors. All rights reserved. + Use of this source code is governed by a BSD-style + license that can be found in the LICENSE file. */ + +/* The gccgo version of the math library calls libc functions. On + some systems, such as Solaris, those functions will call matherr on + exceptional conditions. This is a version of matherr appropriate + for Go, one which returns the values that the Go math library + expects. This is fine for pure Go programs. For mixed Go and C + programs this will be problematic if the C programs themselves use + matherr. Normally the C version of matherr will override this, and + the Go code will just have to cope. If this turns out to be too + problematic we can change to run pure Go code in the math library + on systems that use matherr. */ + +#include <math.h> +#include <stdint.h> + +#include "config.h" + +#if defined(HAVE_MATHERR) && defined(HAVE_STRUCT_EXCEPTION) + +#define PI 3.14159265358979323846264338327950288419716939937510582097494459 + +int +matherr (struct exception* e) +{ + const char *n; + + if (e->type != DOMAIN) + return 0; + + n = e->name; + if (__builtin_strcmp (n, "acos") == 0 + || __builtin_strcmp (n, "asin") == 0) + e->retval = NAN; + else if (__builtin_strcmp (n, "atan2") == 0) + { + if (e->arg1 == 0 && e->arg2 == 0) + { + double nz; + + nz = -0.0; + if (__builtin_memcmp (&e->arg2, &nz, sizeof (double)) != 0) + e->retval = e->arg1; + else + e->retval = copysign (PI, e->arg1); + } + else + return 0; + } + else if (__builtin_strcmp (n, "log") == 0 + || __builtin_strcmp (n, "log10") == 0) + e->retval = NAN; + else if (__builtin_strcmp (n, "pow") == 0) + { + if (e->arg1 < 0) + e->retval = NAN; + else if (e->arg1 == 0 && e->arg2 == 0) + e->retval = 1.0; + else if (e->arg1 == 0 && e->arg2 < 0) + { + double i; + + if (modf (e->arg2, &i) == 0 && ((int64_t) i & 1) == 1) + e->retval = copysign (INFINITY, e->arg1); + else + e->retval = INFINITY; + } + else + return 0; + } + else if (__builtin_strcmp (n, "sqrt") == 0) + { + if (e->arg1 < 0) + e->retval = NAN; + else + return 0; + } + else + return 0; + + return 1; +} + +#endif |