Home | History | Annotate | Line # | Download | only in libm
t_fe_round.c revision 1.19
      1  1.19  riastrad /*	$NetBSD: t_fe_round.c,v 1.19 2024/05/09 12:18:28 riastradh Exp $	*/
      2  1.19  riastrad 
      3   1.1      maya /*
      4   1.1      maya  * Written by Maya Rashish <maya (at) NetBSD.org>
      5   1.1      maya  * Public domain.
      6   1.1      maya  *
      7   1.1      maya  * Testing IEEE-754 rounding modes (and lrint)
      8   1.1      maya  */
      9   1.1      maya 
     10  1.19  riastrad #include <sys/cdefs.h>
     11  1.19  riastrad __RCSID("$NetBSD: t_fe_round.c,v 1.19 2024/05/09 12:18:28 riastradh Exp $");
     12  1.19  riastrad 
     13   1.1      maya #include <atf-c.h>
     14   1.1      maya #include <fenv.h>
     15   1.1      maya #include <math.h>
     16  1.12       kre #include <stdint.h>
     17   1.1      maya #include <stdio.h>
     18   1.1      maya #include <stdlib.h>
     19   1.1      maya 
     20  1.19  riastrad #ifdef __HAVE_FENV
     21  1.19  riastrad 
     22   1.1      maya /*#pragma STDC FENV_ACCESS ON gcc?? */
     23   1.1      maya 
     24   1.1      maya #define INT 9223L
     25   1.1      maya 
     26  1.10  riastrad static const char *
     27  1.10  riastrad rmname(int rm)
     28  1.10  riastrad {
     29  1.10  riastrad 	switch (rm) {
     30  1.10  riastrad 	case FE_TOWARDZERO:
     31  1.10  riastrad 		return "FE_TOWARDZERO";
     32  1.10  riastrad 	case FE_DOWNWARD:
     33  1.10  riastrad 		return "FE_DOWNWARD";
     34  1.10  riastrad 	case FE_UPWARD:
     35  1.10  riastrad 		return "FE_UPWARD";
     36  1.10  riastrad 	case FE_TONEAREST:
     37  1.10  riastrad 		return "FE_TONEAREST";
     38  1.10  riastrad 	default:
     39  1.10  riastrad 		return "unknown";
     40  1.10  riastrad 	}
     41  1.10  riastrad }
     42  1.10  riastrad 
     43  1.19  riastrad /*
     44  1.19  riastrad  * Examples are chosen to fit within the smallest single-precision
     45  1.19  riastrad  * format any NetBSD port uses, so that we can write the examples once
     46  1.19  riastrad  * in type double, and convert to single without raising inexact-result
     47  1.19  riastrad  * exceptions when we're trying to test whether the integer-rounding
     48  1.19  riastrad  * functions raise them.
     49  1.19  riastrad  */
     50   1.1      maya static const struct {
     51   1.1      maya 	int round_mode;
     52   1.1      maya 	double input;
     53   1.1      maya 	long int expected;
     54   1.1      maya } values[] = {
     55  1.16  riastrad 	{ FE_DOWNWARD,		3.75,		3},
     56  1.16  riastrad 	{ FE_DOWNWARD,		-3.75,		-4},
     57  1.16  riastrad 	{ FE_DOWNWARD,		+0.,		0},
     58  1.19  riastrad 	{ FE_DOWNWARD,		-0.,		0},
     59  1.16  riastrad 	{ FE_DOWNWARD,		-INT-0.0625,	-INT-1},
     60  1.16  riastrad 	{ FE_DOWNWARD,		+INT-0.0625,	INT-1},
     61  1.16  riastrad 	{ FE_DOWNWARD,		-INT+0.0625,	-INT},
     62  1.16  riastrad 	{ FE_DOWNWARD,		+INT+0.0625,	INT},
     63   1.1      maya 
     64  1.19  riastrad 	{ FE_UPWARD,		+0.,		0},
     65  1.16  riastrad 	{ FE_UPWARD,		-0.,		0},
     66  1.16  riastrad 	{ FE_UPWARD,		-123.75,	-123},
     67  1.16  riastrad 	{ FE_UPWARD,		123.75,		124},
     68  1.16  riastrad 	{ FE_UPWARD,		-INT-0.0625,	-INT},
     69  1.16  riastrad 	{ FE_UPWARD,		+INT-0.0625,	INT},
     70  1.16  riastrad 	{ FE_UPWARD,		-INT+0.0625,	-INT+1},
     71  1.16  riastrad 	{ FE_UPWARD,		+INT+0.0625,	INT+1},
     72  1.16  riastrad 
     73  1.16  riastrad 	{ FE_TOWARDZERO,	1.9375,		1},
     74  1.16  riastrad 	{ FE_TOWARDZERO,	-1.9375,	-1},
     75  1.16  riastrad 	{ FE_TOWARDZERO,	0.25,		0},
     76  1.16  riastrad 	{ FE_TOWARDZERO,	INT+0.0625,	INT},
     77  1.16  riastrad 	{ FE_TOWARDZERO,	INT-0.0625,	INT - 1},
     78  1.16  riastrad 	{ FE_TOWARDZERO,	-INT+0.0625,	-INT + 1},
     79  1.16  riastrad 	{ FE_TOWARDZERO,	+0.,		0},
     80  1.16  riastrad 	{ FE_TOWARDZERO,	-0.,		0},
     81  1.16  riastrad 
     82  1.16  riastrad 	{ FE_TONEAREST,		-INT-0.0625,	-INT},
     83  1.16  riastrad 	{ FE_TONEAREST,		+INT-0.0625,	INT},
     84  1.16  riastrad 	{ FE_TONEAREST,		-INT+0.0625,	-INT},
     85  1.16  riastrad 	{ FE_TONEAREST,		+INT+0.0625,	INT},
     86  1.16  riastrad 	{ FE_TONEAREST,		-INT-0.53125,	-INT-1},
     87  1.16  riastrad 	{ FE_TONEAREST,		+INT-0.53125,	INT-1},
     88  1.16  riastrad 	{ FE_TONEAREST,		-INT+0.53125,	-INT+1},
     89  1.16  riastrad 	{ FE_TONEAREST,		+INT+0.53125,	INT+1},
     90  1.16  riastrad 	{ FE_TONEAREST,		+0.,		0},
     91  1.16  riastrad 	{ FE_TONEAREST,		-0.,		0},
     92   1.1      maya };
     93   1.1      maya 
     94  1.16  riastrad ATF_TC(fe_lrint);
     95  1.16  riastrad ATF_TC_HEAD(fe_lrint, tc)
     96   1.1      maya {
     97  1.16  riastrad 	atf_tc_set_md_var(tc, "descr",
     98  1.16  riastrad 	    "Checking IEEE 754 rounding modes using lrint(3)");
     99   1.1      maya }
    100   1.1      maya 
    101  1.16  riastrad ATF_TC_BODY(fe_lrint, tc)
    102   1.1      maya {
    103  1.16  riastrad 	enum {
    104  1.16  riastrad 		LLRINT,
    105  1.16  riastrad 		LLRINTF,
    106  1.16  riastrad 		LRINT,
    107  1.16  riastrad 		LRINTF,
    108  1.16  riastrad 		N_FN,
    109  1.16  riastrad 	} fn;
    110  1.16  riastrad 	static const char *const fnname[] = {
    111  1.16  riastrad 		[LLRINT] = "llrint",
    112  1.16  riastrad 		[LLRINTF] = "llrintf",
    113  1.16  riastrad 		[LRINT] = "lrint",
    114  1.16  riastrad 		[LRINTF] = "lrintf",
    115  1.16  riastrad 	};
    116   1.1      maya 	long int received;
    117  1.16  riastrad 	unsigned i;
    118   1.1      maya 
    119  1.16  riastrad 	for (i = 0; i < __arraycount(values); i++) {
    120  1.16  riastrad 		for (fn = 0; fn < N_FN; fn++) {
    121  1.16  riastrad 			/*
    122  1.16  riastrad 			 * Set the requested rounding mode.
    123  1.16  riastrad 			 */
    124  1.16  riastrad 			fesetround(values[i].round_mode);
    125  1.16  riastrad 
    126  1.16  riastrad 			/*
    127  1.16  riastrad 			 * Call the lrint(3)-family function.
    128  1.16  riastrad 			 */
    129  1.16  riastrad 			switch (fn) {
    130  1.16  riastrad 			case LLRINT:
    131  1.16  riastrad 				received = llrint(values[i].input);
    132  1.16  riastrad 				break;
    133  1.16  riastrad 			case LLRINTF:
    134  1.16  riastrad 				received = llrintf(values[i].input);
    135  1.16  riastrad 				break;
    136  1.16  riastrad 			case LRINT:
    137  1.16  riastrad 				received = lrint(values[i].input);
    138  1.16  riastrad 				break;
    139  1.16  riastrad 			case LRINTF:
    140  1.16  riastrad 				received = lrintf(values[i].input);
    141  1.16  riastrad 				break;
    142  1.16  riastrad 			default:
    143  1.16  riastrad 				atf_tc_fail("impossible");
    144  1.16  riastrad 			}
    145  1.16  riastrad 
    146  1.16  riastrad 			/*
    147  1.16  riastrad 			 * Assuming the result we got has zero
    148  1.16  riastrad 			 * fractional part, casting to long int should
    149  1.16  riastrad 			 * have no rounding.  Verify it matches the
    150  1.16  riastrad 			 * integer we expect.
    151  1.16  riastrad 			 */
    152  1.16  riastrad 			ATF_CHECK_MSG((long int)received == values[i].expected,
    153  1.16  riastrad 			    "[%u] %s %s(%f): got %ld, expected %ld",
    154  1.16  riastrad 			    i, rmname(values[i].round_mode), fnname[fn],
    155  1.16  riastrad 			    values[i].input,
    156  1.16  riastrad 			    (long int)received, values[i].expected);
    157  1.16  riastrad 
    158  1.16  riastrad 			/* Do we get the same rounding mode out? */
    159  1.16  riastrad 			ATF_CHECK_MSG(fegetround() == values[i].round_mode,
    160  1.16  riastrad 			    "[%u] %s: set %d (%s), got %d (%s)",
    161  1.16  riastrad 			    i, fnname[fn],
    162  1.16  riastrad 			    values[i].round_mode, rmname(values[i].round_mode),
    163  1.16  riastrad 			    fegetround(), rmname(fegetround()));
    164  1.16  riastrad 		}
    165   1.1      maya 	}
    166   1.1      maya }
    167   1.1      maya 
    168  1.16  riastrad ATF_TC(fe_nearbyint_rint);
    169  1.16  riastrad ATF_TC_HEAD(fe_nearbyint_rint, tc)
    170   1.6        he {
    171  1.10  riastrad 	atf_tc_set_md_var(tc, "descr",
    172  1.16  riastrad 	    "Checking IEEE 754 rounding modes using nearbyint/rint");
    173   1.6        he }
    174   1.6        he 
    175  1.16  riastrad ATF_TC_BODY(fe_nearbyint_rint, tc)
    176   1.6        he {
    177  1.16  riastrad 	enum {
    178  1.16  riastrad 		NEARBYINT,
    179  1.16  riastrad 		NEARBYINTF,
    180  1.16  riastrad 		NEARBYINTL,
    181  1.16  riastrad 		RINT,
    182  1.16  riastrad 		RINTF,
    183  1.16  riastrad 		RINTL,
    184  1.16  riastrad 		N_FN,
    185  1.16  riastrad 	} fn;
    186  1.16  riastrad 	static const char *const fnname[] = {
    187  1.16  riastrad 		[NEARBYINT] = "nearbyint",
    188  1.16  riastrad 		[NEARBYINTF] = "nearbyintf",
    189  1.16  riastrad 		[NEARBYINTL] = "nearbyintl",
    190  1.16  riastrad 		[RINT] = "rint",
    191  1.16  riastrad 		[RINTF] = "rintf",
    192  1.16  riastrad 		[RINTL] = "rintl",
    193  1.16  riastrad 	};
    194  1.10  riastrad 	double received, ipart, fpart;
    195  1.16  riastrad 	unsigned i;
    196   1.6        he 
    197  1.16  riastrad 	for (i = 0; i < __arraycount(values); i++) {
    198  1.16  riastrad 		for (fn = 0; fn < N_FN; fn++) {
    199  1.16  riastrad 			bool expect_except =
    200  1.16  riastrad 			    values[i].input != (double)values[i].expected;
    201  1.16  riastrad 
    202  1.16  riastrad 			/*
    203  1.16  riastrad 			 * Set the requested rounding mode.
    204  1.16  riastrad 			 */
    205  1.16  riastrad 			fesetround(values[i].round_mode);
    206  1.16  riastrad 
    207  1.17  riastrad #ifdef __ia64__
    208  1.17  riastrad /*
    209  1.17  riastrad  * Without this barrier, we get:
    210  1.17  riastrad  *
    211  1.17  riastrad  * /tmp//ccJayu9g.s:2793: Warning: Use of 'mov.m' violates RAW dependency 'AR[FPSR].sf0.flags' (impliedf)
    212  1.17  riastrad  * /tmp//ccJayu9g.s:2793: Warning: Only the first path encountering the conflict is reported
    213  1.17  riastrad  * /tmp//ccJayu9g.s:2757: Warning: This is the location of the conflicting usage
    214  1.17  riastrad  *
    215  1.17  riastrad  * (If you fix this, remove the entry from doc/HACKS.)
    216  1.17  riastrad  */
    217  1.17  riastrad 			__insn_barrier();
    218  1.17  riastrad #endif
    219  1.17  riastrad 
    220  1.16  riastrad 			/*
    221  1.16  riastrad 			 * Clear sticky floating-point exception bits
    222  1.16  riastrad 			 * so we can verify whether the FE_INEXACT
    223  1.16  riastrad 			 * exception is raised.
    224  1.16  riastrad 			 */
    225  1.16  riastrad 			feclearexcept(FE_ALL_EXCEPT);
    226  1.16  riastrad 
    227  1.16  riastrad 			/*
    228  1.16  riastrad 			 * Call the rint(3)-family function.
    229  1.16  riastrad 			 */
    230  1.16  riastrad 			switch (fn) {
    231  1.16  riastrad 			case NEARBYINT:
    232  1.16  riastrad 				received = nearbyint(values[i].input);
    233  1.16  riastrad 				expect_except = false;
    234  1.16  riastrad 				break;
    235  1.16  riastrad 			case NEARBYINTF:
    236  1.16  riastrad 				received = nearbyintf(values[i].input);
    237  1.16  riastrad 				expect_except = false;
    238  1.16  riastrad 				break;
    239  1.16  riastrad 			case NEARBYINTL:
    240  1.16  riastrad 				received = nearbyintl(values[i].input);
    241  1.16  riastrad 				expect_except = false;
    242  1.16  riastrad 				break;
    243  1.16  riastrad 			case RINT:
    244  1.16  riastrad 				received = rint(values[i].input);
    245  1.16  riastrad 				break;
    246  1.16  riastrad 			case RINTF:
    247  1.16  riastrad 				received = rintf(values[i].input);
    248  1.16  riastrad 				break;
    249  1.16  riastrad 			case RINTL:
    250  1.16  riastrad 				received = rintl(values[i].input);
    251  1.16  riastrad 				break;
    252  1.16  riastrad 			default:
    253  1.16  riastrad 				atf_tc_fail("impossible");
    254  1.16  riastrad 			}
    255  1.16  riastrad 
    256  1.16  riastrad 			/*
    257  1.16  riastrad 			 * Verify FE_INEXACT was raised or not,
    258  1.16  riastrad 			 * depending on whether there was rounding and
    259  1.16  riastrad 			 * whether the function is supposed to raise
    260  1.16  riastrad 			 * exceptions.
    261  1.16  riastrad 			 */
    262  1.16  riastrad 			if (expect_except) {
    263  1.16  riastrad 				ATF_CHECK_MSG(fetestexcept(FE_INEXACT) != 0,
    264  1.16  riastrad 				    "[%u] %s %s(%f)"
    265  1.16  riastrad 				    " failed to raise FE_INEXACT",
    266  1.16  riastrad 				    i, rmname(values[i].round_mode),
    267  1.16  riastrad 				    fnname[fn], values[i].input);
    268  1.16  riastrad 			} else {
    269  1.16  riastrad 				ATF_CHECK_MSG(fetestexcept(FE_INEXACT) == 0,
    270  1.16  riastrad 				    "[%u] %s %s(%f)"
    271  1.16  riastrad 				    " spuriously raised FE_INEXACT",
    272  1.16  riastrad 				    i, rmname(values[i].round_mode),
    273  1.16  riastrad 				    fnname[fn], values[i].input);
    274  1.16  riastrad 			}
    275  1.16  riastrad 
    276  1.16  riastrad 			/*
    277  1.16  riastrad 			 * Verify the fractional part of the result is
    278  1.16  riastrad 			 * zero -- the result of rounding to an integer
    279  1.16  riastrad 			 * is supposed to be an integer.
    280  1.16  riastrad 			 */
    281  1.16  riastrad 			fpart = modf(received, &ipart);
    282  1.16  riastrad 			ATF_CHECK_MSG(fpart == 0,
    283  1.16  riastrad 			    "[%u] %s %s(%f)=%f has fractional part %f"
    284  1.16  riastrad 			    " (integer part %f)",
    285  1.16  riastrad 			    i, rmname(values[i].round_mode), fnname[fn],
    286  1.16  riastrad 			    values[i].input, received, fpart, ipart);
    287  1.16  riastrad 
    288  1.16  riastrad 			/*
    289  1.16  riastrad 			 * Assuming the result we got has zero
    290  1.16  riastrad 			 * fractional part, casting to long int should
    291  1.16  riastrad 			 * have no rounding.  Verify it matches the
    292  1.16  riastrad 			 * integer we expect.
    293  1.16  riastrad 			 */
    294  1.16  riastrad 			ATF_CHECK_MSG((long int)received == values[i].expected,
    295  1.16  riastrad 			    "[%u] %s %s(%f): got %f, expected %ld",
    296  1.16  riastrad 			    i, rmname(values[i].round_mode), fnname[fn],
    297  1.16  riastrad 			    values[i].input, received, values[i].expected);
    298  1.16  riastrad 
    299  1.16  riastrad 			/* Do we get the same rounding mode out? */
    300  1.16  riastrad 			ATF_CHECK_MSG(fegetround() == values[i].round_mode,
    301  1.16  riastrad 			    "[%u] %s: set %d (%s), got %d (%s)",
    302  1.16  riastrad 			    i, fnname[fn],
    303  1.16  riastrad 			    values[i].round_mode, rmname(values[i].round_mode),
    304  1.16  riastrad 			    fegetround(), rmname(fegetround()));
    305  1.16  riastrad 		}
    306   1.6        he 	}
    307   1.6        he }
    308   1.6        he 
    309  1.11  riastrad #ifdef __HAVE_LONG_DOUBLE
    310  1.11  riastrad 
    311  1.11  riastrad /*
    312  1.11  riastrad  * Use one bit more than fits in IEEE 754 binary64.
    313  1.11  riastrad  */
    314  1.11  riastrad static const struct {
    315  1.11  riastrad 	int round_mode;
    316  1.11  riastrad 	long double input;
    317  1.16  riastrad 	int64_t expected;
    318  1.11  riastrad } valuesl[] = {
    319  1.13  riastrad 	{ FE_TOWARDZERO,	0x2.00000000000008p+52L, 0x20000000000000 },
    320  1.13  riastrad 	{ FE_DOWNWARD,		0x2.00000000000008p+52L, 0x20000000000000 },
    321  1.13  riastrad 	{ FE_UPWARD,		0x2.00000000000008p+52L, 0x20000000000001 },
    322  1.13  riastrad 	{ FE_TONEAREST,		0x2.00000000000008p+52L, 0x20000000000000 },
    323  1.13  riastrad 	{ FE_TOWARDZERO,	0x2.00000000000018p+52L, 0x20000000000001 },
    324  1.13  riastrad 	{ FE_DOWNWARD,		0x2.00000000000018p+52L, 0x20000000000001 },
    325  1.13  riastrad 	{ FE_UPWARD,		0x2.00000000000018p+52L, 0x20000000000002 },
    326  1.13  riastrad 	{ FE_TONEAREST,		0x2.00000000000018p+52L, 0x20000000000002 },
    327  1.11  riastrad };
    328  1.11  riastrad 
    329  1.16  riastrad ATF_TC(fe_nearbyintl_rintl);
    330  1.16  riastrad ATF_TC_HEAD(fe_nearbyintl_rintl, tc)
    331  1.11  riastrad {
    332  1.11  riastrad 	atf_tc_set_md_var(tc, "descr",
    333  1.16  riastrad 	    "Checking IEEE 754 rounding modes using nearbyintl/rintl");
    334  1.11  riastrad }
    335  1.11  riastrad 
    336  1.16  riastrad ATF_TC_BODY(fe_nearbyintl_rintl, tc)
    337  1.11  riastrad {
    338  1.16  riastrad 	enum {
    339  1.16  riastrad 		RINTL,
    340  1.16  riastrad 		NEARBYINTL,
    341  1.16  riastrad 		N_FN,
    342  1.16  riastrad 	} fn;
    343  1.16  riastrad 	static const char *const fnname[] = {
    344  1.16  riastrad 		[RINTL] = "rintl",
    345  1.16  riastrad 		[NEARBYINTL] = "nearbyintl",
    346  1.16  riastrad 	};
    347  1.11  riastrad 	long double received, ipart, fpart;
    348  1.16  riastrad 	unsigned i;
    349  1.11  riastrad 
    350  1.16  riastrad 	for (i = 0; i < __arraycount(valuesl); i++) {
    351  1.16  riastrad 		for (fn = 0; fn < N_FN; fn++) {
    352  1.16  riastrad 			bool expect_except =
    353  1.16  riastrad 			    (valuesl[i].input !=
    354  1.16  riastrad 				(long double)valuesl[i].expected);
    355  1.16  riastrad 
    356  1.16  riastrad 			/*
    357  1.16  riastrad 			 * Set the requested rounding mode.
    358  1.16  riastrad 			 */
    359  1.16  riastrad 			fesetround(valuesl[i].round_mode);
    360  1.16  riastrad 
    361  1.16  riastrad 			/*
    362  1.16  riastrad 			 * Clear sticky floating-point exception bits
    363  1.16  riastrad 			 * so we can verify whether the FE_INEXACT
    364  1.16  riastrad 			 * exception is raised.
    365  1.16  riastrad 			 */
    366  1.16  riastrad 			feclearexcept(FE_ALL_EXCEPT);
    367  1.16  riastrad 
    368  1.16  riastrad 			/*
    369  1.16  riastrad 			 * Call the rint(3)-family function.
    370  1.16  riastrad 			 */
    371  1.16  riastrad 			switch (fn) {
    372  1.16  riastrad 			case NEARBYINTL:
    373  1.16  riastrad 				received = nearbyintl(valuesl[i].input);
    374  1.16  riastrad 				expect_except = false;
    375  1.16  riastrad 				break;
    376  1.16  riastrad 			case RINTL:
    377  1.16  riastrad 				received = rintl(valuesl[i].input);
    378  1.16  riastrad 				break;
    379  1.16  riastrad 			default:
    380  1.16  riastrad 				atf_tc_fail("impossible");
    381  1.16  riastrad 			}
    382  1.16  riastrad 
    383  1.16  riastrad 			/*
    384  1.16  riastrad 			 * Verify FE_INEXACT was raised or not,
    385  1.16  riastrad 			 * depending on whether there was rounding and
    386  1.16  riastrad 			 * whether the function is supposed to raise
    387  1.16  riastrad 			 * exceptions.
    388  1.16  riastrad 			 */
    389  1.16  riastrad 			if (expect_except) {
    390  1.16  riastrad 				ATF_CHECK_MSG(fetestexcept(FE_INEXACT) != 0,
    391  1.16  riastrad 				    "[%u] %s %s(%Lf)"
    392  1.16  riastrad 				    " failed to raise FE_INEXACT",
    393  1.16  riastrad 				    i, rmname(valuesl[i].round_mode),
    394  1.16  riastrad 				    fnname[fn], valuesl[i].input);
    395  1.16  riastrad 			} else {
    396  1.16  riastrad 				ATF_CHECK_MSG(fetestexcept(FE_INEXACT) == 0,
    397  1.16  riastrad 				    "[%u] %s %s(%Lf)"
    398  1.16  riastrad 				    " spuriously raised FE_INEXACT",
    399  1.16  riastrad 				    i, rmname(valuesl[i].round_mode),
    400  1.16  riastrad 				    fnname[fn], valuesl[i].input);
    401  1.16  riastrad 			}
    402  1.16  riastrad 
    403  1.18  riastrad #if __HAVE_LONG_DOUBLE + 0 == 128
    404  1.18  riastrad 			atf_tc_expect_fail("PR lib/58237:"
    405  1.18  riastrad 			    " modfl returns wrong answers"
    406  1.18  riastrad 			    " on ld128 architectures");
    407  1.18  riastrad #endif
    408  1.18  riastrad 
    409  1.16  riastrad 			/*
    410  1.16  riastrad 			 * Verify the fractional part of the result is
    411  1.16  riastrad 			 * zero -- the result of rounding to an integer
    412  1.16  riastrad 			 * is supposed to be an integer.
    413  1.16  riastrad 			 */
    414  1.16  riastrad 			fpart = modfl(received, &ipart);
    415  1.16  riastrad 			ATF_CHECK_MSG(fpart == 0,
    416  1.16  riastrad 			    "[%u] %s %s(%Lf)=%Lf has fractional part %Lf"
    417  1.16  riastrad 			    " (integer part %Lf)",
    418  1.16  riastrad 			    i, rmname(valuesl[i].round_mode), fnname[fn],
    419  1.16  riastrad 			    valuesl[i].input, received, fpart, ipart);
    420  1.16  riastrad 
    421  1.16  riastrad 			/*
    422  1.16  riastrad 			 * Assuming the result we got has zero
    423  1.16  riastrad 			 * fractional part, casting to int64_t should
    424  1.16  riastrad 			 * have no rounding.  Verify it matches the
    425  1.16  riastrad 			 * integer we expect.
    426  1.16  riastrad 			 */
    427  1.16  riastrad 			ATF_CHECK_MSG(((int64_t)received ==
    428  1.16  riastrad 				valuesl[i].expected),
    429  1.16  riastrad 			    "[%u] %s %s(%Lf): got %Lf, expected %jd",
    430  1.16  riastrad 			    i, rmname(valuesl[i].round_mode), fnname[fn],
    431  1.16  riastrad 			    valuesl[i].input, received, valuesl[i].expected);
    432  1.16  riastrad 
    433  1.16  riastrad 			/* Do we get the same rounding mode out? */
    434  1.16  riastrad 			ATF_CHECK_MSG(fegetround() == valuesl[i].round_mode,
    435  1.16  riastrad 			    "[%u] %s: set %d (%s), got %d (%s)",
    436  1.16  riastrad 			    i, fnname[fn],
    437  1.16  riastrad 			    valuesl[i].round_mode,
    438  1.16  riastrad 			    rmname(valuesl[i].round_mode),
    439  1.16  riastrad 			    fegetround(), rmname(fegetround()));
    440  1.16  riastrad 		}
    441  1.11  riastrad 	}
    442  1.11  riastrad }
    443  1.11  riastrad 
    444  1.19  riastrad #endif	/* __HAVE_LONG_DOUBLE */
    445   1.7        he 
    446   1.1      maya ATF_TP_ADD_TCS(tp)
    447   1.1      maya {
    448   1.1      maya 
    449  1.16  riastrad 	ATF_TP_ADD_TC(tp, fe_lrint);
    450  1.16  riastrad 	ATF_TP_ADD_TC(tp, fe_nearbyint_rint);
    451  1.11  riastrad #ifdef __HAVE_LONG_DOUBLE
    452  1.16  riastrad 	ATF_TP_ADD_TC(tp, fe_nearbyintl_rintl);
    453  1.11  riastrad #endif
    454   1.1      maya 
    455   1.1      maya 	return atf_no_error();
    456   1.1      maya }
    457   1.1      maya 
    458  1.19  riastrad #else  /* !__HAVE_FENV */
    459   1.1      maya 
    460  1.19  riastrad ATF_TP_ADD_TCS(tp)
    461   1.7        he {
    462   1.6        he 
    463  1.19  riastrad 	/*
    464  1.19  riastrad 	 * No fenv, no fesetround to test.
    465  1.19  riastrad 	 */
    466   1.1      maya 	return atf_no_error();
    467   1.1      maya }
    468   1.1      maya 
    469  1.19  riastrad #endif	/* __HAVE_FENV */
    470