1#include <stdint.h>
2#include <stdio.h>
3#include "mtest.h"
4
5// TODO: fix ldbl
6static struct ll_l t[] = {
7#if LDBL_MANT_DIG == 53
8#include "sanity/nexttoward.h"
9#include "special/nexttoward.h"
10
11#elif LDBL_MANT_DIG == 64
12#include "sanity/nexttoward.h"
13#include "special/nexttoward.h"
14
15#endif
16};
17
18int main(void)
19{
20	#pragma STDC FENV_ACCESS ON
21	double y;
22	float d;
23	int e, i, err = 0;
24	struct ll_l *p;
25
26	for (i = 0; i < sizeof t/sizeof *t; i++) {
27		p = t + i;
28
29		if (p->r < 0)
30			continue;
31		fesetround(p->r);
32		feclearexcept(FE_ALL_EXCEPT);
33		y = nexttoward(p->x, p->x2);
34		e = fetestexcept(INEXACT|INVALID|DIVBYZERO|UNDERFLOW|OVERFLOW);
35
36		if (!checkexceptall(e, p->e, p->r)) {
37			printf("%s:%d: bad fp exception: %s nexttoward(%La,%La)=%La, want %s",
38				p->file, p->line, rstr(p->r), p->x, p->x2, p->y, estr(p->e));
39			printf(" got %s\n", estr(e));
40			err++;
41		}
42		d = ulperr(y, p->y, p->dy);
43		if (!checkcr(y, p->y, p->r)) {
44			printf("%s:%d: %s nexttoward(%La,%La) want %La got %a ulperr %.3f = %a + %a\n",
45				p->file, p->line, rstr(p->r), p->x, p->x2, p->y, y, d, d-p->dy, p->dy);
46			err++;
47		}
48	}
49	return !!err;
50}
51