1*84d9c625SLionel Sambuc /* $NetBSD: t_round.c,v 1.4 2013/11/11 23:57:34 joerg Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #include <atf-c.h>
30*84d9c625SLionel Sambuc #include <float.h>
3111be35a1SLionel Sambuc #include <math.h>
3211be35a1SLionel Sambuc
3311be35a1SLionel Sambuc /*
3411be35a1SLionel Sambuc * This tests for a bug in the initial implementation where
3511be35a1SLionel Sambuc * precision was lost in an internal substraction, leading to
3611be35a1SLionel Sambuc * rounding into the wrong direction.
3711be35a1SLionel Sambuc */
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc /* 0.5 - EPSILON */
4011be35a1SLionel Sambuc #define VAL 0x0.7ffffffffffffcp0
4111be35a1SLionel Sambuc #define VALF 0x0.7fffff8p0
42*84d9c625SLionel Sambuc #define VALL (0.5 - LDBL_EPSILON)
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #ifdef __vax__
4511be35a1SLionel Sambuc #define SMALL_NUM 1.0e-38
4611be35a1SLionel Sambuc #else
4711be35a1SLionel Sambuc #define SMALL_NUM 1.0e-40
4811be35a1SLionel Sambuc #endif
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc ATF_TC(round_dir);
ATF_TC_HEAD(round_dir,tc)5111be35a1SLionel Sambuc ATF_TC_HEAD(round_dir, tc)
5211be35a1SLionel Sambuc {
5311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
5411be35a1SLionel Sambuc }
5511be35a1SLionel Sambuc
ATF_TC_BODY(round_dir,tc)5611be35a1SLionel Sambuc ATF_TC_BODY(round_dir, tc)
5711be35a1SLionel Sambuc {
5811be35a1SLionel Sambuc double a = VAL, b, c;
5911be35a1SLionel Sambuc float af = VALF, bf, cf;
60*84d9c625SLionel Sambuc long double al = VALL, bl, cl;
6111be35a1SLionel Sambuc
6211be35a1SLionel Sambuc b = round(a);
6311be35a1SLionel Sambuc bf = roundf(af);
64*84d9c625SLionel Sambuc bl = roundl(al);
6511be35a1SLionel Sambuc
6611be35a1SLionel Sambuc ATF_CHECK(fabs(b) < SMALL_NUM);
6711be35a1SLionel Sambuc ATF_CHECK(fabsf(bf) < SMALL_NUM);
68*84d9c625SLionel Sambuc ATF_CHECK(fabsl(bl) < SMALL_NUM);
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc c = round(-a);
7111be35a1SLionel Sambuc cf = roundf(-af);
72*84d9c625SLionel Sambuc cl = roundl(-al);
7311be35a1SLionel Sambuc
7411be35a1SLionel Sambuc ATF_CHECK(fabs(c) < SMALL_NUM);
7511be35a1SLionel Sambuc ATF_CHECK(fabsf(cf) < SMALL_NUM);
76*84d9c625SLionel Sambuc ATF_CHECK(fabsl(cl) < SMALL_NUM);
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)7911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
8011be35a1SLionel Sambuc {
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, round_dir);
8311be35a1SLionel Sambuc
8411be35a1SLionel Sambuc return atf_no_error();
8511be35a1SLionel Sambuc }
86