From ce441c1b09323e5bce2545230414c069a0c706ea Mon Sep 17 00:00:00 2001 From: nsz Date: Sat, 5 May 2012 17:21:00 +0200 Subject: [PATCH 1/1] math: separate fenvutil.c for printing except/round flags --- src/math/fenvutil.c | 59 +++++++++++++++++++++ src/math/fenvutil.h | 2 + src/math/lrint.c | 123 +++----------------------------------------- 3 files changed, 67 insertions(+), 117 deletions(-) create mode 100644 src/math/fenvutil.c create mode 100644 src/math/fenvutil.h diff --git a/src/math/fenvutil.c b/src/math/fenvutil.c new file mode 100644 index 0000000..ee3e7a3 --- /dev/null +++ b/src/math/fenvutil.c @@ -0,0 +1,59 @@ +#include +#include +#include "fenvutil.h" + +static struct { + int flag; + char *s; +} eflags[] = { + {FE_INVALID, "FE_INVALID"}, + {FE_DIVBYZERO, "FE_DIVBYZERO"}, + {FE_OVERFLOW, "FE_OVERFLOW"}, + {FE_UNDERFLOW, "FE_UNDERFLOW"}, + {FE_INEXACT, "FE_INEXACT"}, +}; +static int ne = sizeof eflags / sizeof *eflags; + +static struct { + int flag; + char *s; +} rflags[] = { + {FE_TONEAREST,"FE_TONEAREST"}, + {FE_DOWNWARD,"FE_DOWNWARD"}, + {FE_UPWARD,"FE_UPWARD"}, + {FE_TOWARDZERO,"FE_TOWARDZERO"}, +}; +static int nr = sizeof rflags / sizeof *rflags; + +char *strexcept(int f) { + static char buf[256]; + char *p; + int i, all=0; + + p = buf; + for (i = 0; i < ne; i++) + if (f & eflags[i].flag) { + p += sprintf(p, "%s%s", all ? "|" : "", eflags[i].s); + all |= eflags[i].flag; + } + if (all != f) { + p += sprintf(p, "%s%d", all ? "|" : "", f & ~all); + all = f; + } + p += sprintf(p, "%s", all ? "" : "0"); + return buf; +} + +char *strround(int f) { + static char buf[256]; + int i; + + for (i = 0; i < nr; i++) + if (f == rflags[i].flag) { + sprintf(buf, "%s", rflags[i].s); + return buf; + } + sprintf(buf, "%d", f); + return buf; +} + diff --git a/src/math/fenvutil.h b/src/math/fenvutil.h new file mode 100644 index 0000000..c77d341 --- /dev/null +++ b/src/math/fenvutil.h @@ -0,0 +1,2 @@ +char *strexcept(int f); +char *strround(int f); diff --git a/src/math/lrint.c b/src/math/lrint.c index 6f03c6b..0e019a6 100644 --- a/src/math/lrint.c +++ b/src/math/lrint.c @@ -4,109 +4,7 @@ #include #include #include - -static struct { - int flag; - char *s; -} eflags[] = { - {FE_INVALID, "FE_INVALID"}, - {FE_DIVBYZERO, "FE_DIVBYZERO"}, - {FE_OVERFLOW, "FE_OVERFLOW"}, - {FE_UNDERFLOW, "FE_UNDERFLOW"}, - {FE_INEXACT, "FE_INEXACT"}, -}; -static int ne = sizeof eflags / sizeof *eflags; - -static struct { - int flag; - char *s; -} rflags[] = { - {FE_TONEAREST,"FE_TONEAREST,"}, - {FE_DOWNWARD,"FE_DOWNWARD,"}, - {FE_UPWARD,"FE_UPWARD,"}, - {FE_TOWARDZERO,"FE_TOWARDZERO,"}, -}; -static int nr = sizeof rflags / sizeof *rflags; - -void printexcept(int f) { - int i, all=0; - - for (i = 0; i < ne; i++) - if (f & eflags[i].flag) { - printf("%s%s", all ? "|" : "", eflags[i].s); - all |= eflags[i].flag; - } - if (all != f) { - printf("%s%d", all ? "|" : "", f & ~all); - all = f; - } - printf("%s,", all ? "" : "0"); -} - -void printround(int f) { - int i; - - for (i = 0; i < nr; i++) - if (f == rflags[i].flag) { - printf("%s ", rflags[i].s); - return; - } - printf("%d, ", f); -} - -/* -struct {double x;} t[] = { -0.0, -0.25, --0.25, -0.5, --0.5, -0.75, --0.75, -1.0, --1.0, -1.25, --1.25, -0x1p30, --0x1p30, -0x1p31-1, --0x1p31+1, -0x1p31, --0x1p31, -0x1p31+1, --0x1p31-1, -0x1p31-0.5, --0x1p31+0.5, -0x1p31+0.5, --0x1p31-0.5, -0x1p32, --0x1p32, -0x1p32 - 0.5, --0x1p32 + 0.5, -}; - -void test_gendata() -{ - int f, i, j; - long n; - - for (i = 0; i < sizeof t/sizeof *t; i++) { - for (j = 0; j < nr; j++) { - fesetround(rflags[j].flag); - feclearexcept(FE_ALL_EXCEPT); - n = lrint(t[i].x); - f = fetestexcept(FE_ALL_EXCEPT); - if (f == FE_INVALID) - n = 0; - - printround(rflags[j].flag); - printf("%18a, %11ld, ", t[i].x, n); - printexcept(f); - printf("\n"); - } - } -} -*/ +#include "fenvutil.h" static struct { int round; @@ -235,20 +133,11 @@ void test_lrint() n = lrint(t[i].x); f = fetestexcept(FE_ALL_EXCEPT); - if (t[i].except != FE_INVALID && n != t[i].n) { - error("round="); - printround(t[i].round); - printf("lrint(%a) want %ld got %ld\n", t[i].x, t[i].n, n); - } - if (f != t[i].except) { - error("round="); - printround(t[i].round); - printf("lrint(%a)==%ld want except=", t[i].x, t[i].n); - printexcept(t[i].except); - printf(" got except="); - printexcept(f); - printf("\n"); - } + if (t[i].except != FE_INVALID && n != t[i].n) + error("round=%s, lrint(%a) want %ld got %ld\n", strround(t[i].round), t[i].x, t[i].n, n); + if (f != t[i].except) + error("round=%s, lrint(%a)==%ld want except=%s, got except=%s\n", + strround(t[i].round), t[i].x, t[i].n, strdup(strexcept(t[i].except)), strdup(strexcept(f))); } } -- 2.20.1