1140089Sdas/*-
2140089Sdas * Copyright (c) 2005 David Schultz <das@FreeBSD.org>
3140089Sdas * All rights reserved.
4140089Sdas *
5140089Sdas * Redistribution and use in source and binary forms, with or without
6140089Sdas * modification, are permitted provided that the following conditions
7140089Sdas * are met:
8140089Sdas * 1. Redistributions of source code must retain the above copyright
9140089Sdas *    notice, this list of conditions and the following disclaimer.
10140089Sdas * 2. Redistributions in binary form must reproduce the above copyright
11140089Sdas *    notice, this list of conditions and the following disclaimer in the
12140089Sdas *    documentation and/or other materials provided with the distribution.
13140089Sdas *
14140089Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15140089Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16140089Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17140089Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18140089Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19140089Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20140089Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21140089Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22140089Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23140089Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24140089Sdas * SUCH DAMAGE.
25140089Sdas */
26140089Sdas
27140089Sdas/*
28140089Sdas * Test for lround(), lroundf(), llround(), and llroundf().
29140089Sdas */
30140089Sdas
31140089Sdas#include <sys/cdefs.h>
32140089Sdas__FBSDID("$FreeBSD$");
33140089Sdas
34140089Sdas#include <assert.h>
35140089Sdas#include <fenv.h>
36140089Sdas#include <limits.h>
37140089Sdas#include <math.h>
38140089Sdas#include <stdio.h>
39140089Sdas
40174491Sdas/*
41174491Sdas * XXX The volatile here is to avoid gcc's bogus constant folding and work
42174491Sdas *     around the lack of support for the FENV_ACCESS pragma.
43174491Sdas */
44140089Sdas#define	test(func, x, result, excepts)	do {				\
45174491Sdas	volatile double _d = x;						\
46140089Sdas	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
47174491Sdas	assert((func)(_d) == (result) || fetestexcept(FE_INVALID));	\
48140089Sdas	assert(fetestexcept(FE_ALL_EXCEPT) == (excepts));		\
49140089Sdas} while (0)
50140089Sdas
51140089Sdas#define	testall(x, result, excepts)	do {				\
52140089Sdas	test(lround, x, result, excepts);				\
53140089Sdas	test(lroundf, x, result, excepts);				\
54140089Sdas	test(llround, x, result, excepts);				\
55140089Sdas	test(llroundf, x, result, excepts);				\
56140089Sdas} while (0)
57140089Sdas
58140089Sdas#define	IGNORE	0
59140089Sdas
60140089Sdas#pragma STDC FENV_ACCESS ON
61140089Sdas
62140089Sdasint
63140089Sdasmain(int argc, char *argv[])
64140089Sdas{
65140089Sdas
66140089Sdas	printf("1..1\n");
67140089Sdas
68140089Sdas	testall(0.0, 0, 0);
69140089Sdas	testall(0.25, 0, FE_INEXACT);
70140089Sdas	testall(0.5, 1, FE_INEXACT);
71140089Sdas	testall(-0.5, -1, FE_INEXACT);
72140089Sdas	testall(1.0, 1, 0);
73140089Sdas	testall(0x12345000p0, 0x12345000, 0);
74140089Sdas	testall(0x1234.fp0, 0x1235, FE_INEXACT);
75140089Sdas	testall(INFINITY, IGNORE, FE_INVALID);
76140089Sdas	testall(NAN, IGNORE, FE_INVALID);
77140089Sdas
78140089Sdas#if (LONG_MAX == 0x7fffffffl)
79140089Sdas	test(lround, 0x7fffffff.8p0, IGNORE, FE_INVALID);
80140089Sdas	test(lround, -0x80000000.8p0, IGNORE, FE_INVALID);
81140089Sdas	test(lround, 0x80000000.0p0, IGNORE, FE_INVALID);
82140089Sdas	test(lround, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
83140089Sdas	test(lround, -0x80000000.4p0, -0x80000000l, FE_INEXACT);
84140089Sdas	test(lroundf, 0x80000000.0p0f, IGNORE, FE_INVALID);
85140089Sdas	test(lroundf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
86140089Sdas#elif (LONG_MAX == 0x7fffffffffffffffll)
87140089Sdas	test(lround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
88140089Sdas	test(lroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
89140089Sdas	test(lround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
90140089Sdas	test(lroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
91140089Sdas	test(lround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
92140089Sdas	test(lroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
93140089Sdas	test(lround, -0x8000000000000000.0p0, -0x8000000000000000l, 0);
94140089Sdas	test(lroundf, -0x8000000000000000.0p0f, -0x8000000000000000l, 0);
95140089Sdas#else
96140089Sdas#error "Unsupported long size"
97140089Sdas#endif
98140089Sdas
99140089Sdas#if (LLONG_MAX == 0x7fffffffffffffffLL)
100140089Sdas	test(llround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
101140089Sdas	test(llroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
102140089Sdas	test(llround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
103140089Sdas	test(llroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
104140089Sdas	test(llround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
105140089Sdas	test(llroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
106140089Sdas	test(llround, -0x8000000000000000.0p0, -0x8000000000000000ll, 0);
107140089Sdas	test(llroundf, -0x8000000000000000.0p0f, -0x8000000000000000ll, 0);
108140089Sdas#else
109140089Sdas#error "Unsupported long long size"
110140089Sdas#endif
111140089Sdas
112140089Sdas	printf("ok 1 - lround\n");
113140089Sdas
114140089Sdas	return (0);
115140089Sdas}
116