18a7d0e8cSEnji Cooper /*-
28a7d0e8cSEnji Cooper * Copyright (c) 2005 David Schultz <das@FreeBSD.org>
38a7d0e8cSEnji Cooper * All rights reserved.
48a7d0e8cSEnji Cooper *
58a7d0e8cSEnji Cooper * Redistribution and use in source and binary forms, with or without
68a7d0e8cSEnji Cooper * modification, are permitted provided that the following conditions
78a7d0e8cSEnji Cooper * are met:
88a7d0e8cSEnji Cooper * 1. Redistributions of source code must retain the above copyright
98a7d0e8cSEnji Cooper * notice, this list of conditions and the following disclaimer.
108a7d0e8cSEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
118a7d0e8cSEnji Cooper * notice, this list of conditions and the following disclaimer in the
128a7d0e8cSEnji Cooper * documentation and/or other materials provided with the distribution.
138a7d0e8cSEnji Cooper *
148a7d0e8cSEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158a7d0e8cSEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168a7d0e8cSEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178a7d0e8cSEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188a7d0e8cSEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198a7d0e8cSEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208a7d0e8cSEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218a7d0e8cSEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228a7d0e8cSEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238a7d0e8cSEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248a7d0e8cSEnji Cooper * SUCH DAMAGE.
258a7d0e8cSEnji Cooper */
268a7d0e8cSEnji Cooper
278a7d0e8cSEnji Cooper /*
288a7d0e8cSEnji Cooper * Test for lround(), lroundf(), llround(), and llroundf().
298a7d0e8cSEnji Cooper */
308a7d0e8cSEnji Cooper
318a7d0e8cSEnji Cooper #include <sys/cdefs.h>
328a7d0e8cSEnji Cooper #include <fenv.h>
338a7d0e8cSEnji Cooper #include <limits.h>
348a7d0e8cSEnji Cooper #include <math.h>
358a7d0e8cSEnji Cooper #include <stdio.h>
368a7d0e8cSEnji Cooper
37133bc645SAlex Richardson #include "test-utils.h"
38133bc645SAlex Richardson
39133bc645SAlex Richardson #define IGNORE 0x12345
40133bc645SAlex Richardson
418a7d0e8cSEnji Cooper #define test(func, x, result, excepts) do { \
42133bc645SAlex Richardson ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
43*df3b437cSDimitry Andric long long _r = (func)(x); \
44133bc645SAlex Richardson CHECK_FP_EXCEPTIONS_MSG(excepts, FE_ALL_EXCEPT, "for %s(%s)", \
45133bc645SAlex Richardson #func, #x); \
46133bc645SAlex Richardson if ((excepts & FE_INVALID) != 0) { \
47133bc645SAlex Richardson ATF_REQUIRE_EQ(result, IGNORE); \
48133bc645SAlex Richardson ATF_CHECK_EQ_MSG(FE_INVALID, fetestexcept(FE_INVALID), \
49133bc645SAlex Richardson "FE_INVALID not set correctly for %s(%s)", #func, #x); \
50133bc645SAlex Richardson } else { \
51133bc645SAlex Richardson ATF_REQUIRE_MSG(result != IGNORE, "Expected can't be IGNORE!"); \
52133bc645SAlex Richardson ATF_REQUIRE_EQ(result, (__STRING(func(_d)), _r)); \
53133bc645SAlex Richardson } \
548a7d0e8cSEnji Cooper } while (0)
558a7d0e8cSEnji Cooper
568a7d0e8cSEnji Cooper #define testall(x, result, excepts) do { \
578a7d0e8cSEnji Cooper test(lround, x, result, excepts); \
588a7d0e8cSEnji Cooper test(lroundf, x, result, excepts); \
598a7d0e8cSEnji Cooper test(llround, x, result, excepts); \
608a7d0e8cSEnji Cooper test(llroundf, x, result, excepts); \
618a7d0e8cSEnji Cooper } while (0)
628a7d0e8cSEnji Cooper
638a7d0e8cSEnji Cooper #pragma STDC FENV_ACCESS ON
648a7d0e8cSEnji Cooper
65133bc645SAlex Richardson ATF_TC_WITHOUT_HEAD(main);
ATF_TC_BODY(main,tc)66133bc645SAlex Richardson ATF_TC_BODY(main, tc)
678a7d0e8cSEnji Cooper {
688a7d0e8cSEnji Cooper testall(0.0, 0, 0);
698a7d0e8cSEnji Cooper testall(0.25, 0, FE_INEXACT);
708a7d0e8cSEnji Cooper testall(0.5, 1, FE_INEXACT);
718a7d0e8cSEnji Cooper testall(-0.5, -1, FE_INEXACT);
728a7d0e8cSEnji Cooper testall(1.0, 1, 0);
738a7d0e8cSEnji Cooper testall(0x12345000p0, 0x12345000, 0);
748a7d0e8cSEnji Cooper testall(0x1234.fp0, 0x1235, FE_INEXACT);
758a7d0e8cSEnji Cooper testall(INFINITY, IGNORE, FE_INVALID);
768a7d0e8cSEnji Cooper testall(NAN, IGNORE, FE_INVALID);
778a7d0e8cSEnji Cooper
788a7d0e8cSEnji Cooper #if (LONG_MAX == 0x7fffffffl)
798a7d0e8cSEnji Cooper test(lround, 0x7fffffff.8p0, IGNORE, FE_INVALID);
808a7d0e8cSEnji Cooper test(lround, -0x80000000.8p0, IGNORE, FE_INVALID);
818a7d0e8cSEnji Cooper test(lround, 0x80000000.0p0, IGNORE, FE_INVALID);
828a7d0e8cSEnji Cooper test(lround, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
838a7d0e8cSEnji Cooper test(lround, -0x80000000.4p0, -0x80000000l, FE_INEXACT);
848a7d0e8cSEnji Cooper test(lroundf, 0x80000000.0p0f, IGNORE, FE_INVALID);
858a7d0e8cSEnji Cooper test(lroundf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
868a7d0e8cSEnji Cooper #elif (LONG_MAX == 0x7fffffffffffffffll)
878a7d0e8cSEnji Cooper test(lround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
888a7d0e8cSEnji Cooper test(lroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
898a7d0e8cSEnji Cooper test(lround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
908a7d0e8cSEnji Cooper test(lroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
918a7d0e8cSEnji Cooper test(lround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
928a7d0e8cSEnji Cooper test(lroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
93133bc645SAlex Richardson test(lround, -0x8000000000000000.0p0, (long)-0x8000000000000000l, 0);
94133bc645SAlex Richardson test(lroundf, -0x8000000000000000.0p0f, (long)-0x8000000000000000l, 0);
958a7d0e8cSEnji Cooper #else
968a7d0e8cSEnji Cooper #error "Unsupported long size"
978a7d0e8cSEnji Cooper #endif
988a7d0e8cSEnji Cooper
998a7d0e8cSEnji Cooper #if (LLONG_MAX == 0x7fffffffffffffffLL)
1008a7d0e8cSEnji Cooper test(llround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
1018a7d0e8cSEnji Cooper test(llroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
1028a7d0e8cSEnji Cooper test(llround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
1038a7d0e8cSEnji Cooper test(llroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
1048a7d0e8cSEnji Cooper test(llround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
1058a7d0e8cSEnji Cooper test(llroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
106133bc645SAlex Richardson test(llround, -0x8000000000000000.0p0, (long long)-0x8000000000000000ll, 0);
107133bc645SAlex Richardson test(llroundf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ll, 0);
1088a7d0e8cSEnji Cooper #else
1098a7d0e8cSEnji Cooper #error "Unsupported long long size"
1108a7d0e8cSEnji Cooper #endif
111133bc645SAlex Richardson }
1128a7d0e8cSEnji Cooper
ATF_TP_ADD_TCS(tp)113133bc645SAlex Richardson ATF_TP_ADD_TCS(tp)
114133bc645SAlex Richardson {
115133bc645SAlex Richardson ATF_TP_ADD_TC(tp, main);
1168a7d0e8cSEnji Cooper
117133bc645SAlex Richardson return (atf_no_error());
1188a7d0e8cSEnji Cooper }
119