Home | History | Annotate | Line # | Download | only in libm
t_round.c revision 1.4
      1  1.4   joerg /* $NetBSD: t_round.c,v 1.4 2013/11/11 23:57:34 joerg Exp $ */
      2  1.3  jruoho 
      3  1.3  jruoho /*-
      4  1.3  jruoho  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.3  jruoho  * All rights reserved.
      6  1.3  jruoho  *
      7  1.3  jruoho  * Redistribution and use in source and binary forms, with or without
      8  1.3  jruoho  * modification, are permitted provided that the following conditions
      9  1.3  jruoho  * are met:
     10  1.3  jruoho  * 1. Redistributions of source code must retain the above copyright
     11  1.3  jruoho  *    notice, this list of conditions and the following disclaimer.
     12  1.3  jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3  jruoho  *    notice, this list of conditions and the following disclaimer in the
     14  1.3  jruoho  *    documentation and/or other materials provided with the distribution.
     15  1.3  jruoho  *
     16  1.3  jruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.3  jruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.3  jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.3  jruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.3  jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.3  jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.3  jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.3  jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.3  jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.3  jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.3  jruoho  * POSSIBILITY OF SUCH DAMAGE.
     27  1.3  jruoho  */
     28  1.1  jruoho 
     29  1.1  jruoho #include <atf-c.h>
     30  1.4   joerg #include <float.h>
     31  1.1  jruoho #include <math.h>
     32  1.1  jruoho 
     33  1.1  jruoho /*
     34  1.1  jruoho  * This tests for a bug in the initial implementation where
     35  1.1  jruoho  * precision was lost in an internal substraction, leading to
     36  1.1  jruoho  * rounding into the wrong direction.
     37  1.1  jruoho  */
     38  1.1  jruoho 
     39  1.1  jruoho /* 0.5 - EPSILON */
     40  1.1  jruoho #define VAL	0x0.7ffffffffffffcp0
     41  1.1  jruoho #define VALF	0x0.7fffff8p0
     42  1.4   joerg #define VALL	(0.5 - LDBL_EPSILON)
     43  1.1  jruoho 
     44  1.2     mrg #ifdef __vax__
     45  1.2     mrg #define SMALL_NUM	1.0e-38
     46  1.2     mrg #else
     47  1.2     mrg #define SMALL_NUM	1.0e-40
     48  1.2     mrg #endif
     49  1.2     mrg 
     50  1.1  jruoho ATF_TC(round_dir);
     51  1.1  jruoho ATF_TC_HEAD(round_dir, tc)
     52  1.1  jruoho {
     53  1.1  jruoho 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
     54  1.1  jruoho }
     55  1.1  jruoho 
     56  1.1  jruoho ATF_TC_BODY(round_dir, tc)
     57  1.1  jruoho {
     58  1.1  jruoho 	double a = VAL, b, c;
     59  1.1  jruoho 	float af = VALF, bf, cf;
     60  1.4   joerg 	long double al = VALL, bl, cl;
     61  1.1  jruoho 
     62  1.1  jruoho 	b = round(a);
     63  1.1  jruoho 	bf = roundf(af);
     64  1.4   joerg 	bl = roundl(al);
     65  1.1  jruoho 
     66  1.3  jruoho 	ATF_CHECK(fabs(b) < SMALL_NUM);
     67  1.3  jruoho 	ATF_CHECK(fabsf(bf) < SMALL_NUM);
     68  1.4   joerg 	ATF_CHECK(fabsl(bl) < SMALL_NUM);
     69  1.1  jruoho 
     70  1.1  jruoho 	c = round(-a);
     71  1.1  jruoho 	cf = roundf(-af);
     72  1.4   joerg 	cl = roundl(-al);
     73  1.1  jruoho 
     74  1.3  jruoho 	ATF_CHECK(fabs(c) < SMALL_NUM);
     75  1.3  jruoho 	ATF_CHECK(fabsf(cf) < SMALL_NUM);
     76  1.4   joerg 	ATF_CHECK(fabsl(cl) < SMALL_NUM);
     77  1.1  jruoho }
     78  1.1  jruoho 
     79  1.1  jruoho ATF_TP_ADD_TCS(tp)
     80  1.1  jruoho {
     81  1.1  jruoho 
     82  1.1  jruoho 	ATF_TP_ADD_TC(tp, round_dir);
     83  1.1  jruoho 
     84  1.1  jruoho 	return atf_no_error();
     85  1.1  jruoho }
     86