1140089Sdas/*-
2175310Sdas * Copyright (c) 2005-2008 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 lrint(), lrintf(), llrint(), and llrintf().
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
40175310Sdas#ifdef	__i386__
41175310Sdas#include <ieeefp.h>
42175310Sdas#endif
43175310Sdas
44216223Sdas/*
45216223Sdas * XXX The volatile here is to avoid gcc's bogus constant folding and work
46216223Sdas *     around the lack of support for the FENV_ACCESS pragma.
47216223Sdas */
48140089Sdas#define	test(func, x, result, excepts)	do {				\
49216223Sdas	volatile double _d = x;						\
50140089Sdas	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
51216223Sdas	assert((func)(_d) == (result) || fetestexcept(FE_INVALID));	\
52140089Sdas	assert(fetestexcept(FE_ALL_EXCEPT) == (excepts));		\
53140089Sdas} while (0)
54140089Sdas
55140089Sdas#define	testall(x, result, excepts)	do {				\
56140089Sdas	test(lrint, x, result, excepts);				\
57140089Sdas	test(lrintf, x, result, excepts);				\
58175310Sdas	test(lrintl, x, result, excepts);				\
59140089Sdas	test(llrint, x, result, excepts);				\
60140089Sdas	test(llrintf, x, result, excepts);				\
61175310Sdas	test(llrintl, x, result, excepts);				\
62140089Sdas} while (0)
63140089Sdas
64140089Sdas#define	IGNORE	0
65140089Sdas
66140089Sdas#pragma STDC FENV_ACCESS ON
67140089Sdas
68175310Sdasvoid
69175310Sdasrun_tests(void)
70140089Sdas{
71140089Sdas
72140089Sdas	assert(fesetround(FE_DOWNWARD) == 0);
73140089Sdas	testall(0.75, 0, FE_INEXACT);
74140089Sdas	testall(-0.5, -1, FE_INEXACT);
75140089Sdas
76140089Sdas	assert(fesetround(FE_TONEAREST) == 0);
77140089Sdas	testall(0.0, 0, 0);
78140089Sdas	testall(0.25, 0, FE_INEXACT);
79140089Sdas	testall(0.5, 0, FE_INEXACT);
80140089Sdas	testall(-2.5, -2, FE_INEXACT);
81140089Sdas	testall(1.0, 1, 0);
82140089Sdas	testall(0x12345000p0, 0x12345000, 0);
83140089Sdas	testall(0x1234.fp0, 0x1235, FE_INEXACT);
84140089Sdas	testall(INFINITY, IGNORE, FE_INVALID);
85140089Sdas	testall(NAN, IGNORE, FE_INVALID);
86140089Sdas
87140089Sdas#if (LONG_MAX == 0x7fffffffl)
88140089Sdas	assert(fesetround(FE_UPWARD) == 0);
89140089Sdas	test(lrint, 0x7fffffff.8p0, IGNORE, FE_INVALID);
90140089Sdas	test(lrint, -0x80000000.4p0, -0x80000000l, FE_INEXACT);
91140089Sdas
92140089Sdas	assert(fesetround(FE_DOWNWARD) == 0);
93140089Sdas	test(lrint, -0x80000000.8p0, IGNORE, FE_INVALID);
94140089Sdas	test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID);
95140089Sdas	test(lrint, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
96140089Sdas	test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID);
97140089Sdas	test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
98140089Sdas
99140089Sdas	assert(fesetround(FE_TOWARDZERO) == 0);
100140089Sdas	test(lrint, 0x7fffffff.8p0,  0x7fffffffl, FE_INEXACT);
101140089Sdas	test(lrint, -0x80000000.8p0, -0x80000000l, FE_INEXACT);
102140089Sdas	test(lrint, 0x80000000.0p0, IGNORE, FE_INVALID);
103140089Sdas	test(lrintf, 0x80000000.0p0f, IGNORE, FE_INVALID);
104140089Sdas	test(lrintf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
105140089Sdas#elif (LONG_MAX == 0x7fffffffffffffffll)
106140089Sdas	assert(fesetround(FE_TONEAREST) == 0);
107140089Sdas	test(lrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
108140089Sdas	test(lrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
109140089Sdas	test(lrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
110140089Sdas	test(lrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
111140089Sdas	test(lrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
112140089Sdas	test(lrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
113140089Sdas	test(lrint, -0x8000000000000000.0p0, -0x8000000000000000l, 0);
114140089Sdas	test(lrintf, -0x8000000000000000.0p0f, -0x8000000000000000l, 0);
115140089Sdas#else
116140089Sdas#error "Unsupported long size"
117140089Sdas#endif
118140089Sdas
119140089Sdas#if (LLONG_MAX == 0x7fffffffffffffffLL)
120140089Sdas	assert(fesetround(FE_TONEAREST) == 0);
121140089Sdas	test(llrint, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
122140089Sdas	test(llrintf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
123140089Sdas	test(llrint, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
124140089Sdas	test(llrintf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
125140089Sdas	test(llrint, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
126140089Sdas	test(llrintf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
127140089Sdas	test(llrint, -0x8000000000000000.0p0, -0x8000000000000000ll, 0);
128140089Sdas	test(llrintf, -0x8000000000000000.0p0f, -0x8000000000000000ll, 0);
129140089Sdas#else
130140089Sdas#error "Unsupported long long size"
131140089Sdas#endif
132175310Sdas}
133140089Sdas
134175310Sdasint
135175310Sdasmain(int argc, char *argv[])
136175310Sdas{
137175310Sdas
138175310Sdas	printf("1..1\n");
139175310Sdas
140175310Sdas	run_tests();
141175310Sdas#ifdef	__i386__
142175310Sdas	fpsetprec(FP_PE);
143175310Sdas	run_tests();
144175310Sdas#endif
145175310Sdas
146140089Sdas	printf("ok 1 - lrint\n");
147140089Sdas
148140089Sdas	return (0);
149140089Sdas}
150