1/* $NetBSD: t_round.c,v 1.9 2017/09/03 13:41:19 wiz Exp $ */
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30
31#include <atf-c.h>
32#include <float.h>
33#include <math.h>
34#include <stdio.h>
35#include <stdint.h>
36#include <inttypes.h>
37
38/*
39 * This tests for a bug in the initial implementation where
40 * precision was lost in an internal substraction, leading to
41 * rounding into the wrong direction.
42 */
43
44/* 0.5 - EPSILON */
45#define VAL	0x0.7ffffffffffffcp0
46#define VALF	0x0.7fffff8p0
47#define VALL	(0.5 - LDBL_EPSILON)
48
49#ifdef __vax__
50#define SMALL_NUM	1.0e-38
51#else
52#define SMALL_NUM	1.0e-40
53#endif
54
55ATF_TC(round_dir);
56ATF_TC_HEAD(round_dir, tc)
57{
58	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
59}
60
61ATF_TC_BODY(round_dir, tc)
62{
63	double a = VAL, b, c;
64	float af = VALF, bf, cf;
65	long double al = VALL, bl, cl;
66
67	b = round(a);
68	bf = roundf(af);
69	bl = roundl(al);
70
71	ATF_CHECK(fabs(b) < SMALL_NUM);
72	ATF_CHECK(fabsf(bf) < SMALL_NUM);
73	ATF_CHECK(fabsl(bl) < SMALL_NUM);
74
75	c = round(-a);
76	cf = roundf(-af);
77	cl = roundl(-al);
78
79	ATF_CHECK(fabs(c) < SMALL_NUM);
80	ATF_CHECK(fabsf(cf) < SMALL_NUM);
81	ATF_CHECK(fabsl(cl) < SMALL_NUM);
82}
83
84ATF_TC(rounding_alpha);
85ATF_TC_HEAD(rounding_alpha, tc)
86{
87	atf_tc_set_md_var(tc, "descr","Checking MPFR's config failure with -mieee on Alpha");
88}
89
90typedef uint64_t gimpy_limb_t;
91#define GIMPY_NUMB_BITS 64
92
93ATF_TC_BODY(rounding_alpha, tc)
94{
95        double d;
96        gimpy_limb_t u;
97        int i;
98
99        d = 1.0;
100        for (i = 0; i < GIMPY_NUMB_BITS - 1; i++)
101                d = d + d;
102
103        printf("d = %g\n", d);
104        u = (gimpy_limb_t) d;
105
106        for (; i > 0; i--) {
107                ATF_CHECK_MSG((u % 2 == 0),
108		    "%"PRIu64" is not an even number! (iteration %d)", u , i);
109                u = u >> 1;
110        }
111}
112
113ATF_TC(rounding_alpha_simple);
114ATF_TC_HEAD(rounding_alpha_simple, tc)
115{
116	atf_tc_set_md_var(tc, "descr","Checking double to uint64_t edge case");
117}
118
119
120static double rounding_alpha_simple_even = 9223372036854775808.000000; /* 2^63 */
121
122ATF_TC_BODY(rounding_alpha_simple, tc)
123{
124	uint64_t unsigned_even = rounding_alpha_simple_even;
125
126	ATF_CHECK_MSG(unsigned_even % 2 == 0,
127	    "2^63 cast to uint64_t is odd (got %"PRIu64")", unsigned_even);
128
129}
130ATF_TP_ADD_TCS(tp)
131{
132
133	ATF_TP_ADD_TC(tp, round_dir);
134	ATF_TP_ADD_TC(tp, rounding_alpha);
135	ATF_TP_ADD_TC(tp, rounding_alpha_simple);
136
137	return atf_no_error();
138}
139