Home | History | Annotate | Line # | Download | only in libm
t_round.c revision 1.6
      1  1.6    maya /* $NetBSD: t_round.c,v 1.6 2017/08/30 14:24:20 maya 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.5    maya #include <sys/param.h>
     30  1.5    maya 
     31  1.1  jruoho #include <atf-c.h>
     32  1.4   joerg #include <float.h>
     33  1.1  jruoho #include <math.h>
     34  1.5    maya #include <stdio.h>
     35  1.1  jruoho 
     36  1.1  jruoho /*
     37  1.1  jruoho  * This tests for a bug in the initial implementation where
     38  1.1  jruoho  * precision was lost in an internal substraction, leading to
     39  1.1  jruoho  * rounding into the wrong direction.
     40  1.1  jruoho  */
     41  1.1  jruoho 
     42  1.1  jruoho /* 0.5 - EPSILON */
     43  1.1  jruoho #define VAL	0x0.7ffffffffffffcp0
     44  1.1  jruoho #define VALF	0x0.7fffff8p0
     45  1.4   joerg #define VALL	(0.5 - LDBL_EPSILON)
     46  1.1  jruoho 
     47  1.2     mrg #ifdef __vax__
     48  1.2     mrg #define SMALL_NUM	1.0e-38
     49  1.2     mrg #else
     50  1.2     mrg #define SMALL_NUM	1.0e-40
     51  1.2     mrg #endif
     52  1.2     mrg 
     53  1.1  jruoho ATF_TC(round_dir);
     54  1.1  jruoho ATF_TC_HEAD(round_dir, tc)
     55  1.1  jruoho {
     56  1.1  jruoho 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
     57  1.1  jruoho }
     58  1.1  jruoho 
     59  1.1  jruoho ATF_TC_BODY(round_dir, tc)
     60  1.1  jruoho {
     61  1.1  jruoho 	double a = VAL, b, c;
     62  1.1  jruoho 	float af = VALF, bf, cf;
     63  1.4   joerg 	long double al = VALL, bl, cl;
     64  1.1  jruoho 
     65  1.1  jruoho 	b = round(a);
     66  1.1  jruoho 	bf = roundf(af);
     67  1.4   joerg 	bl = roundl(al);
     68  1.1  jruoho 
     69  1.3  jruoho 	ATF_CHECK(fabs(b) < SMALL_NUM);
     70  1.3  jruoho 	ATF_CHECK(fabsf(bf) < SMALL_NUM);
     71  1.4   joerg 	ATF_CHECK(fabsl(bl) < SMALL_NUM);
     72  1.1  jruoho 
     73  1.1  jruoho 	c = round(-a);
     74  1.1  jruoho 	cf = roundf(-af);
     75  1.4   joerg 	cl = roundl(-al);
     76  1.1  jruoho 
     77  1.3  jruoho 	ATF_CHECK(fabs(c) < SMALL_NUM);
     78  1.3  jruoho 	ATF_CHECK(fabsf(cf) < SMALL_NUM);
     79  1.4   joerg 	ATF_CHECK(fabsl(cl) < SMALL_NUM);
     80  1.1  jruoho }
     81  1.1  jruoho 
     82  1.5    maya ATF_TC(rounding_alpha);
     83  1.5    maya ATF_TC_HEAD(rounding_alpha, tc)
     84  1.5    maya {
     85  1.5    maya 	atf_tc_set_md_var(tc, "descr","Checking MPFR's config failure with -mieee on Alpha");
     86  1.5    maya }
     87  1.5    maya 
     88  1.5    maya typedef uint64_t gimpy_limb_t;
     89  1.5    maya #define GIMPY_NUMB_BITS 64
     90  1.5    maya 
     91  1.5    maya ATF_TC_BODY(rounding_alpha, tc)
     92  1.5    maya {
     93  1.5    maya         double d;
     94  1.5    maya         gimpy_limb_t u;
     95  1.5    maya         int i;
     96  1.5    maya 
     97  1.5    maya         d = 1.0;
     98  1.5    maya         for (i = 0; i < GIMPY_NUMB_BITS - 1; i++)
     99  1.5    maya                 d = d + d;
    100  1.5    maya 
    101  1.5    maya         printf("d = %g\n", d);
    102  1.5    maya         u = (gimpy_limb_t) d;
    103  1.5    maya 
    104  1.5    maya         for (; i > 0; i--) {
    105  1.6    maya                 printf("i=%d, u: %"PRIu64"\n", i, u);
    106  1.5    maya                 ATF_CHECK(!(u & 1));
    107  1.5    maya                 u = u >> 1;
    108  1.5    maya         }
    109  1.5    maya }
    110  1.5    maya 
    111  1.1  jruoho ATF_TP_ADD_TCS(tp)
    112  1.1  jruoho {
    113  1.1  jruoho 
    114  1.1  jruoho 	ATF_TP_ADD_TC(tp, round_dir);
    115  1.5    maya 	ATF_TP_ADD_TC(tp, rounding_alpha);
    116  1.1  jruoho 
    117  1.1  jruoho 	return atf_no_error();
    118  1.1  jruoho }
    119