#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <err.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
FE_OVERFLOW | FE_UNDERFLOW)
#define NEXCEPTS (sizeof(std_excepts) / sizeof(std_excepts[0]))
static const int std_excepts[] = {
FE_INVALID,
FE_DIVBYZERO,
FE_OVERFLOW,
FE_UNDERFLOW,
FE_INEXACT,
};
static int std_except_sets[1 << NEXCEPTS];
static void init_exceptsets(void);
static void test_dfl_env(void);
static void test_fegsetenv(void);
static void test_fegsetexceptflag(void);
static void test_masking(void);
static void test_fegsetround(void);
static void test_feholdupdate(void);
static void test_feraiseexcept(void);
static void test_fetestclearexcept(void);
static int getround(void);
static void raiseexcept(int excepts);
static void trap_handler(int sig);
#pragma STDC FENV_ACCESS ON
int
main(int argc, char *argv[])
{
printf("1..8\n");
init_exceptsets();
test_dfl_env();
printf("ok 1 - fenv\n");
test_fetestclearexcept();
printf("ok 2 - fenv\n");
test_fegsetexceptflag();
printf("ok 3 - fenv\n");
test_feraiseexcept();
printf("ok 4 - fenv\n");
test_fegsetround();
printf("ok 5 - fenv\n");
test_fegsetenv();
printf("ok 6 - fenv\n");
test_masking();
printf("ok 7 - fenv\n");
test_feholdupdate();
printf("ok 8 - fenv\n");
return (0);
}
void
init_exceptsets(void)
{
int i, j, sr;
for (i = 0; i < 1 << NEXCEPTS; i++) {
for (sr = i, j = 0; sr != 0; sr >>= 1, j++)
std_except_sets[i] |= std_excepts[j] & ((~sr & 1) - 1);
}
}
static void
test_dfl_env(void)
{
#ifndef NO_STRICT_DFL_ENV
fenv_t env;
fegetenv(&env);
#ifdef __amd64
memset(&env.__x87.__others[0], 0, 14);
#endif
assert(memcmp(&env, FE_DFL_ENV, sizeof(env)) == 0);
#endif
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
}
static void
test_fetestclearexcept(void)
{
int excepts, i;
for (i = 0; i < 1 << NEXCEPTS; i++)
assert(fetestexcept(std_except_sets[i]) == 0);
for (i = 0; i < 1 << NEXCEPTS; i++) {
excepts = std_except_sets[i];
raiseexcept(excepts);
assert(fetestexcept(excepts) == excepts);
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
raiseexcept(excepts);
assert(fetestexcept(excepts) == excepts);
if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0) {
excepts |= FE_INEXACT;
assert((fetestexcept(ALL_STD_EXCEPT) | FE_INEXACT) ==
excepts);
} else {
assert(fetestexcept(ALL_STD_EXCEPT) == excepts);
}
assert(feclearexcept(excepts) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) == 0);
}
}
static void
test_fegsetexceptflag(void)
{
fexcept_t flag;
int excepts, i;
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
for (i = 0; i < 1 << NEXCEPTS; i++) {
excepts = std_except_sets[i];
assert(fegetexceptflag(&flag, excepts) == 0);
raiseexcept(ALL_STD_EXCEPT);
assert(fesetexceptflag(&flag, excepts) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) ==
(ALL_STD_EXCEPT ^ excepts));
assert(fegetexceptflag(&flag, FE_ALL_EXCEPT) == 0);
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
assert(fesetexceptflag(&flag, excepts) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) == 0);
assert(fesetexceptflag(&flag, ALL_STD_EXCEPT ^ excepts) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) ==
(ALL_STD_EXCEPT ^ excepts));
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
}
}
static void
test_feraiseexcept(void)
{
int excepts, i;
for (i = 0; i < 1 << NEXCEPTS; i++) {
excepts = std_except_sets[i];
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
assert(feraiseexcept(excepts) == 0);
if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0) {
excepts |= FE_INEXACT;
assert((fetestexcept(ALL_STD_EXCEPT) | FE_INEXACT) ==
excepts);
} else {
assert(fetestexcept(ALL_STD_EXCEPT) == excepts);
}
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
}
assert(feraiseexcept(FE_INVALID | FE_DIVBYZERO) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) == (FE_INVALID | FE_DIVBYZERO));
assert(feraiseexcept(FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) == ALL_STD_EXCEPT);
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
}
static void
test_fegsetround(void)
{
assert(fegetround() == FE_TONEAREST);
assert(getround() == FE_TONEAREST);
assert(FLT_ROUNDS == 1);
assert(fesetround(FE_DOWNWARD) == 0);
assert(fegetround() == FE_DOWNWARD);
assert(getround() == FE_DOWNWARD);
assert(FLT_ROUNDS == 3);
assert(fesetround(FE_UPWARD) == 0);
assert(getround() == FE_UPWARD);
assert(fegetround() == FE_UPWARD);
assert(FLT_ROUNDS == 2);
assert(fesetround(FE_TOWARDZERO) == 0);
assert(getround() == FE_TOWARDZERO);
assert(fegetround() == FE_TOWARDZERO);
assert(FLT_ROUNDS == 0);
assert(fesetround(FE_TONEAREST) == 0);
assert(getround() == FE_TONEAREST);
assert(FLT_ROUNDS == 1);
assert(feclearexcept(FE_ALL_EXCEPT) == 0);
}
static void
test_fegsetenv(void)
{
fenv_t env1, env2;
int excepts, i;
for (i = 0; i < 1 << NEXCEPTS; i++) {
excepts = std_except_sets[i];
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
assert(fegetround() == FE_TONEAREST);
assert(fegetenv(&env1) == 0);
raiseexcept(excepts);
if ((excepts & (FE_UNDERFLOW | FE_OVERFLOW)) != 0 &&
(excepts & FE_INEXACT) == 0)
assert(feclearexcept(FE_INEXACT) == 0);
fesetround(FE_DOWNWARD);
assert(fegetenv(&env2) == 0);
assert(fesetenv(&env1) == 0);
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
assert(fegetround() == FE_TONEAREST);
assert(fesetenv(&env2) == 0);
assert(fetestexcept(FE_ALL_EXCEPT) == excepts);
assert(fegetround() == FE_DOWNWARD);
assert(fesetenv(&env1) == 0);
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
assert(fegetround() == FE_TONEAREST);
}
}
static void
test_masking(void)
{
#if !defined(__arm__) && !defined(__aarch64__) && !defined(__riscv)
struct sigaction act;
int except, i, pass, raise, status;
assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
assert((feenableexcept(FE_INVALID|FE_OVERFLOW) & ALL_STD_EXCEPT) == 0);
assert((feenableexcept(FE_UNDERFLOW) & ALL_STD_EXCEPT) ==
(FE_INVALID | FE_OVERFLOW));
assert((fedisableexcept(FE_OVERFLOW) & ALL_STD_EXCEPT) ==
(FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW));
assert((fegetexcept() & ALL_STD_EXCEPT) == (FE_INVALID | FE_UNDERFLOW));
assert((fedisableexcept(FE_ALL_EXCEPT) & ALL_STD_EXCEPT) ==
(FE_INVALID | FE_UNDERFLOW));
assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = trap_handler;
for (pass = 0; pass < 2; pass++) {
for (i = 0; i < NEXCEPTS; i++) {
except = std_excepts[i];
if (except == FE_INEXACT)
raise = FE_DIVBYZERO | FE_INVALID;
else
raise = ALL_STD_EXCEPT ^ except;
switch(fork()) {
case 0:
assert((fegetexcept() & ALL_STD_EXCEPT) == 0);
assert((feenableexcept(except)
& ALL_STD_EXCEPT) == 0);
assert(fegetexcept() == except);
raiseexcept(raise);
assert(feraiseexcept(raise) == 0);
assert(fetestexcept(ALL_STD_EXCEPT) == raise);
assert(sigaction(SIGFPE, &act, NULL) == 0);
switch (pass) {
case 0:
raiseexcept(except);
case 1:
feraiseexcept(except);
default:
assert(0);
}
assert(0);
default:
assert(wait(&status) > 0);
if (!WIFEXITED(status))
errx(1, "child aborted\n");
assert(WEXITSTATUS(status) == 0);
break;
case -1:
assert(0);
}
}
}
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
#endif
}
static void
test_feholdupdate(void)
{
fenv_t env;
struct sigaction act;
int except, i, pass, status, raise;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = trap_handler;
for (pass = 0; pass < 2; pass++) {
for (i = 0; i < NEXCEPTS; i++) {
except = std_excepts[i];
if (except == FE_INEXACT)
raise = FE_DIVBYZERO | FE_INVALID;
else
raise = ALL_STD_EXCEPT ^ except;
switch(fork()) {
case 0:
if (pass == 1)
assert((feenableexcept(except) &
ALL_STD_EXCEPT) == 0);
raiseexcept(raise);
assert(fesetround(FE_DOWNWARD) == 0);
assert(feholdexcept(&env) == 0);
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
raiseexcept(except);
assert(fesetround(FE_UPWARD) == 0);
if (pass == 1)
assert(sigaction(SIGFPE, &act, NULL) ==
0);
assert(feupdateenv(&env) == 0);
assert(fegetround() == FE_DOWNWARD);
assert(fetestexcept(ALL_STD_EXCEPT) ==
(except | raise));
assert(pass == 0);
_exit(0);
default:
assert(wait(&status) > 0);
if (!WIFEXITED(status))
errx(1, "child aborted\n");
assert(WEXITSTATUS(status) == 0);
break;
case -1:
assert(0);
}
}
#if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
break;
#endif
}
assert(fetestexcept(FE_ALL_EXCEPT) == 0);
}
static void
raiseexcept(int excepts)
{
volatile double d;
if (excepts & FE_INVALID) {
d = 0.0;
d = 0.0 / d;
}
if (excepts & FE_DIVBYZERO) {
d = 0.0;
d = 1.0 / d;
}
if (excepts & FE_OVERFLOW) {
d = DBL_MAX;
d *= 2.0;
}
if (excepts & FE_UNDERFLOW) {
d = DBL_MIN;
d /= DBL_MAX;
}
if (excepts & FE_INEXACT) {
d = DBL_MIN;
d += 1.0;
}
d = 1.0;
d += 1.0;
}
static int
getround(void)
{
volatile double d, e;
d = 1.0;
d -= 1.0;
e = copysign(1.0, d);
if (e < 0.0)
return (FE_DOWNWARD);
d = 1.0;
e = d + (DBL_EPSILON * 3.0 / 4.0);
if (e == 1.0)
return (FE_TOWARDZERO);
e = d + (DBL_EPSILON * 1.0 / 4.0);
if (e > 1.0)
return (FE_UPWARD);
return (FE_TONEAREST);
}
static void
trap_handler(int sig)
{
assert(sig == SIGFPE);
_exit(0);
}