1*498c7b5eSderaadt /* $OpenBSD: lround_test.c,v 1.2 2021/12/13 18:04:28 deraadt Exp $ */
2c36e572eSmbuhl /*-
3c36e572eSmbuhl * Copyright (c) 2005 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl * All rights reserved.
5c36e572eSmbuhl *
6c36e572eSmbuhl * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl * modification, are permitted provided that the following conditions
8c36e572eSmbuhl * are met:
9c36e572eSmbuhl * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl * notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl * notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl * documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl *
15c36e572eSmbuhl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl * SUCH DAMAGE.
26c36e572eSmbuhl */
27c36e572eSmbuhl
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl
30c36e572eSmbuhl /*
31c36e572eSmbuhl * Test for lround(), lroundf(), llround(), and llroundf().
32c36e572eSmbuhl */
33c36e572eSmbuhl
34c36e572eSmbuhl #include <fenv.h>
35c36e572eSmbuhl #include <limits.h>
36c36e572eSmbuhl #include <math.h>
37c36e572eSmbuhl #include <stdio.h>
38c36e572eSmbuhl
39c36e572eSmbuhl #include "test-utils.h"
40c36e572eSmbuhl
41c36e572eSmbuhl #define IGNORE 0x12345
42c36e572eSmbuhl
43c36e572eSmbuhl #define test(func, x, result, excepts) do { \
44c36e572eSmbuhl ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT)); \
45c36e572eSmbuhl long long _r = (func)(x); \
46c36e572eSmbuhl CHECK_FP_EXCEPTIONS_MSG(excepts, FE_ALL_EXCEPT, "for %s(%s)", \
47c36e572eSmbuhl #func, #x); \
48c36e572eSmbuhl if ((excepts & FE_INVALID) != 0) { \
49c36e572eSmbuhl ATF_REQUIRE_EQ(result, IGNORE); \
50c36e572eSmbuhl ATF_CHECK_EQ_MSG(FE_INVALID, fetestexcept(FE_INVALID), \
51c36e572eSmbuhl "FE_INVALID not set correctly for %s(%s)", #func, #x); \
52c36e572eSmbuhl } else { \
53c36e572eSmbuhl ATF_REQUIRE_MSG(result != IGNORE, "Expected can't be IGNORE!"); \
54c36e572eSmbuhl ATF_REQUIRE_EQ(result, (__STRING(func(_d)), _r)); \
55c36e572eSmbuhl } \
56c36e572eSmbuhl } while (0)
57c36e572eSmbuhl
58c36e572eSmbuhl #define testall(x, result, excepts) do { \
59c36e572eSmbuhl test(lround, x, result, excepts); \
60c36e572eSmbuhl test(lroundf, x, result, excepts); \
61c36e572eSmbuhl test(llround, x, result, excepts); \
62c36e572eSmbuhl test(llroundf, x, result, excepts); \
63c36e572eSmbuhl } while (0)
64c36e572eSmbuhl
65c36e572eSmbuhl #pragma STDC FENV_ACCESS ON
66c36e572eSmbuhl
67c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(main);
ATF_TC_BODY(main,tc)68c36e572eSmbuhl ATF_TC_BODY(main, tc)
69c36e572eSmbuhl {
70c36e572eSmbuhl testall(0.0, 0, 0);
71c36e572eSmbuhl testall(0.25, 0, FE_INEXACT);
72c36e572eSmbuhl testall(0.5, 1, FE_INEXACT);
73c36e572eSmbuhl testall(-0.5, -1, FE_INEXACT);
74c36e572eSmbuhl testall(1.0, 1, 0);
75c36e572eSmbuhl testall(0x12345000p0, 0x12345000, 0);
76c36e572eSmbuhl testall(0x1234.fp0, 0x1235, FE_INEXACT);
77c36e572eSmbuhl testall(INFINITY, IGNORE, FE_INVALID);
78c36e572eSmbuhl testall(NAN, IGNORE, FE_INVALID);
79c36e572eSmbuhl
80c36e572eSmbuhl #if (LONG_MAX == 0x7fffffffl)
81c36e572eSmbuhl test(lround, 0x7fffffff.8p0, IGNORE, FE_INVALID);
82c36e572eSmbuhl test(lround, -0x80000000.8p0, IGNORE, FE_INVALID);
83c36e572eSmbuhl test(lround, 0x80000000.0p0, IGNORE, FE_INVALID);
84c36e572eSmbuhl test(lround, 0x7fffffff.4p0, 0x7fffffffl, FE_INEXACT);
85c36e572eSmbuhl test(lround, -0x80000000.4p0, -0x80000000l, FE_INEXACT);
86c36e572eSmbuhl test(lroundf, 0x80000000.0p0f, IGNORE, FE_INVALID);
87c36e572eSmbuhl test(lroundf, 0x7fffff80.0p0f, 0x7fffff80l, 0);
88c36e572eSmbuhl #elif (LONG_MAX == 0x7fffffffffffffffll)
89c36e572eSmbuhl test(lround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
90c36e572eSmbuhl test(lroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
91c36e572eSmbuhl test(lround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00l, 0);
92c36e572eSmbuhl test(lroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000l, 0);
93c36e572eSmbuhl test(lround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
94c36e572eSmbuhl test(lroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
95c36e572eSmbuhl test(lround, -0x8000000000000000.0p0, (long)-0x8000000000000000l, 0);
96c36e572eSmbuhl test(lroundf, -0x8000000000000000.0p0f, (long)-0x8000000000000000l, 0);
97c36e572eSmbuhl #else
98c36e572eSmbuhl #error "Unsupported long size"
99c36e572eSmbuhl #endif
100c36e572eSmbuhl
101c36e572eSmbuhl #if (LLONG_MAX == 0x7fffffffffffffffLL)
102c36e572eSmbuhl test(llround, 0x8000000000000000.0p0, IGNORE, FE_INVALID);
103c36e572eSmbuhl test(llroundf, 0x8000000000000000.0p0f, IGNORE, FE_INVALID);
104c36e572eSmbuhl test(llround, 0x7ffffffffffffc00.0p0, 0x7ffffffffffffc00ll, 0);
105c36e572eSmbuhl test(llroundf, 0x7fffff8000000000.0p0f, 0x7fffff8000000000ll, 0);
106c36e572eSmbuhl test(llround, -0x8000000000000800.0p0, IGNORE, FE_INVALID);
107c36e572eSmbuhl test(llroundf, -0x8000010000000000.0p0f, IGNORE, FE_INVALID);
108c36e572eSmbuhl test(llround, -0x8000000000000000.0p0, (long long)-0x8000000000000000ll, 0);
109c36e572eSmbuhl test(llroundf, -0x8000000000000000.0p0f, (long long)-0x8000000000000000ll, 0);
110c36e572eSmbuhl #else
111c36e572eSmbuhl #error "Unsupported long long size"
112c36e572eSmbuhl #endif
113c36e572eSmbuhl }
114c36e572eSmbuhl
ATF_TP_ADD_TCS(tp)115c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
116c36e572eSmbuhl {
117c36e572eSmbuhl ATF_TP_ADD_TC(tp, main);
118c36e572eSmbuhl
119c36e572eSmbuhl return (atf_no_error());
120c36e572eSmbuhl }
121