11ec3feb6SAlex Richardson /* $NetBSD: t_round.c,v 1.9 2017/09/03 13:41:19 wiz Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper
291ec3feb6SAlex Richardson #include <sys/param.h>
301ec3feb6SAlex Richardson
3157718be8SEnji Cooper #include <atf-c.h>
3257718be8SEnji Cooper #include <float.h>
3357718be8SEnji Cooper #include <math.h>
341ec3feb6SAlex Richardson #include <stdio.h>
351ec3feb6SAlex Richardson #include <stdint.h>
361ec3feb6SAlex Richardson #include <inttypes.h>
3757718be8SEnji Cooper
3857718be8SEnji Cooper /*
3957718be8SEnji Cooper * This tests for a bug in the initial implementation where
4057718be8SEnji Cooper * precision was lost in an internal substraction, leading to
4157718be8SEnji Cooper * rounding into the wrong direction.
4257718be8SEnji Cooper */
4357718be8SEnji Cooper
4457718be8SEnji Cooper /* 0.5 - EPSILON */
4557718be8SEnji Cooper #define VAL 0x0.7ffffffffffffcp0
4657718be8SEnji Cooper #define VALF 0x0.7fffff8p0
4757718be8SEnji Cooper #define VALL (0.5 - LDBL_EPSILON)
4857718be8SEnji Cooper
4957718be8SEnji Cooper #ifdef __vax__
5057718be8SEnji Cooper #define SMALL_NUM 1.0e-38
5157718be8SEnji Cooper #else
5257718be8SEnji Cooper #define SMALL_NUM 1.0e-40
5357718be8SEnji Cooper #endif
5457718be8SEnji Cooper
5557718be8SEnji Cooper ATF_TC(round_dir);
ATF_TC_HEAD(round_dir,tc)5657718be8SEnji Cooper ATF_TC_HEAD(round_dir, tc)
5757718be8SEnji Cooper {
5857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
5957718be8SEnji Cooper }
6057718be8SEnji Cooper
ATF_TC_BODY(round_dir,tc)6157718be8SEnji Cooper ATF_TC_BODY(round_dir, tc)
6257718be8SEnji Cooper {
6357718be8SEnji Cooper double a = VAL, b, c;
6457718be8SEnji Cooper float af = VALF, bf, cf;
6557718be8SEnji Cooper long double al = VALL, bl, cl;
6657718be8SEnji Cooper
6757718be8SEnji Cooper b = round(a);
6857718be8SEnji Cooper bf = roundf(af);
6957718be8SEnji Cooper bl = roundl(al);
7057718be8SEnji Cooper
7157718be8SEnji Cooper ATF_CHECK(fabs(b) < SMALL_NUM);
7257718be8SEnji Cooper ATF_CHECK(fabsf(bf) < SMALL_NUM);
7357718be8SEnji Cooper ATF_CHECK(fabsl(bl) < SMALL_NUM);
7457718be8SEnji Cooper
7557718be8SEnji Cooper c = round(-a);
7657718be8SEnji Cooper cf = roundf(-af);
7757718be8SEnji Cooper cl = roundl(-al);
7857718be8SEnji Cooper
7957718be8SEnji Cooper ATF_CHECK(fabs(c) < SMALL_NUM);
8057718be8SEnji Cooper ATF_CHECK(fabsf(cf) < SMALL_NUM);
8157718be8SEnji Cooper ATF_CHECK(fabsl(cl) < SMALL_NUM);
8257718be8SEnji Cooper }
8357718be8SEnji Cooper
841ec3feb6SAlex Richardson ATF_TC(rounding_alpha);
ATF_TC_HEAD(rounding_alpha,tc)851ec3feb6SAlex Richardson ATF_TC_HEAD(rounding_alpha, tc)
861ec3feb6SAlex Richardson {
871ec3feb6SAlex Richardson atf_tc_set_md_var(tc, "descr","Checking MPFR's config failure with -mieee on Alpha");
881ec3feb6SAlex Richardson }
891ec3feb6SAlex Richardson
901ec3feb6SAlex Richardson typedef uint64_t gimpy_limb_t;
911ec3feb6SAlex Richardson #define GIMPY_NUMB_BITS 64
921ec3feb6SAlex Richardson
ATF_TC_BODY(rounding_alpha,tc)931ec3feb6SAlex Richardson ATF_TC_BODY(rounding_alpha, tc)
941ec3feb6SAlex Richardson {
951ec3feb6SAlex Richardson double d;
961ec3feb6SAlex Richardson gimpy_limb_t u;
971ec3feb6SAlex Richardson int i;
981ec3feb6SAlex Richardson
991ec3feb6SAlex Richardson d = 1.0;
1001ec3feb6SAlex Richardson for (i = 0; i < GIMPY_NUMB_BITS - 1; i++)
1011ec3feb6SAlex Richardson d = d + d;
1021ec3feb6SAlex Richardson
1031ec3feb6SAlex Richardson printf("d = %g\n", d);
1041ec3feb6SAlex Richardson u = (gimpy_limb_t) d;
1051ec3feb6SAlex Richardson
1061ec3feb6SAlex Richardson for (; i > 0; i--) {
1071ec3feb6SAlex Richardson ATF_CHECK_MSG((u % 2 == 0),
1081ec3feb6SAlex Richardson "%"PRIu64" is not an even number! (iteration %d)", u , i);
1091ec3feb6SAlex Richardson u = u >> 1;
1101ec3feb6SAlex Richardson }
1111ec3feb6SAlex Richardson }
1121ec3feb6SAlex Richardson
1131ec3feb6SAlex Richardson ATF_TC(rounding_alpha_simple);
ATF_TC_HEAD(rounding_alpha_simple,tc)1141ec3feb6SAlex Richardson ATF_TC_HEAD(rounding_alpha_simple, tc)
1151ec3feb6SAlex Richardson {
1161ec3feb6SAlex Richardson atf_tc_set_md_var(tc, "descr","Checking double to uint64_t edge case");
1171ec3feb6SAlex Richardson }
1181ec3feb6SAlex Richardson
1191ec3feb6SAlex Richardson
120*87d65c74SAlex Richardson static double rounding_alpha_simple_even = 9223372036854775808.000000; /* 2^63 */
1211ec3feb6SAlex Richardson
ATF_TC_BODY(rounding_alpha_simple,tc)1221ec3feb6SAlex Richardson ATF_TC_BODY(rounding_alpha_simple, tc)
1231ec3feb6SAlex Richardson {
1241ec3feb6SAlex Richardson uint64_t unsigned_even = rounding_alpha_simple_even;
1251ec3feb6SAlex Richardson
1261ec3feb6SAlex Richardson ATF_CHECK_MSG(unsigned_even % 2 == 0,
1271ec3feb6SAlex Richardson "2^63 cast to uint64_t is odd (got %"PRIu64")", unsigned_even);
1281ec3feb6SAlex Richardson
1291ec3feb6SAlex Richardson }
ATF_TP_ADD_TCS(tp)13057718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
13157718be8SEnji Cooper {
13257718be8SEnji Cooper
13357718be8SEnji Cooper ATF_TP_ADD_TC(tp, round_dir);
1341ec3feb6SAlex Richardson ATF_TP_ADD_TC(tp, rounding_alpha);
1351ec3feb6SAlex Richardson ATF_TP_ADD_TC(tp, rounding_alpha_simple);
13657718be8SEnji Cooper
13757718be8SEnji Cooper return atf_no_error();
13857718be8SEnji Cooper }
139