Home | History | Annotate | Line # | Download | only in libm
t_round.c revision 1.9
      1  1.9     wiz /* $NetBSD: t_round.c,v 1.9 2017/09/03 13:41:19 wiz 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.7    maya #include <stdint.h>
     36  1.1  jruoho 
     37  1.1  jruoho /*
     38  1.1  jruoho  * This tests for a bug in the initial implementation where
     39  1.1  jruoho  * precision was lost in an internal substraction, leading to
     40  1.1  jruoho  * rounding into the wrong direction.
     41  1.1  jruoho  */
     42  1.1  jruoho 
     43  1.1  jruoho /* 0.5 - EPSILON */
     44  1.1  jruoho #define VAL	0x0.7ffffffffffffcp0
     45  1.1  jruoho #define VALF	0x0.7fffff8p0
     46  1.4   joerg #define VALL	(0.5 - LDBL_EPSILON)
     47  1.1  jruoho 
     48  1.2     mrg #ifdef __vax__
     49  1.2     mrg #define SMALL_NUM	1.0e-38
     50  1.2     mrg #else
     51  1.2     mrg #define SMALL_NUM	1.0e-40
     52  1.2     mrg #endif
     53  1.2     mrg 
     54  1.1  jruoho ATF_TC(round_dir);
     55  1.1  jruoho ATF_TC_HEAD(round_dir, tc)
     56  1.1  jruoho {
     57  1.1  jruoho 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
     58  1.1  jruoho }
     59  1.1  jruoho 
     60  1.1  jruoho ATF_TC_BODY(round_dir, tc)
     61  1.1  jruoho {
     62  1.1  jruoho 	double a = VAL, b, c;
     63  1.1  jruoho 	float af = VALF, bf, cf;
     64  1.4   joerg 	long double al = VALL, bl, cl;
     65  1.1  jruoho 
     66  1.1  jruoho 	b = round(a);
     67  1.1  jruoho 	bf = roundf(af);
     68  1.4   joerg 	bl = roundl(al);
     69  1.1  jruoho 
     70  1.3  jruoho 	ATF_CHECK(fabs(b) < SMALL_NUM);
     71  1.3  jruoho 	ATF_CHECK(fabsf(bf) < SMALL_NUM);
     72  1.4   joerg 	ATF_CHECK(fabsl(bl) < SMALL_NUM);
     73  1.1  jruoho 
     74  1.1  jruoho 	c = round(-a);
     75  1.1  jruoho 	cf = roundf(-af);
     76  1.4   joerg 	cl = roundl(-al);
     77  1.1  jruoho 
     78  1.3  jruoho 	ATF_CHECK(fabs(c) < SMALL_NUM);
     79  1.3  jruoho 	ATF_CHECK(fabsf(cf) < SMALL_NUM);
     80  1.4   joerg 	ATF_CHECK(fabsl(cl) < SMALL_NUM);
     81  1.1  jruoho }
     82  1.1  jruoho 
     83  1.5    maya ATF_TC(rounding_alpha);
     84  1.5    maya ATF_TC_HEAD(rounding_alpha, tc)
     85  1.5    maya {
     86  1.5    maya 	atf_tc_set_md_var(tc, "descr","Checking MPFR's config failure with -mieee on Alpha");
     87  1.5    maya }
     88  1.5    maya 
     89  1.5    maya typedef uint64_t gimpy_limb_t;
     90  1.5    maya #define GIMPY_NUMB_BITS 64
     91  1.5    maya 
     92  1.5    maya ATF_TC_BODY(rounding_alpha, tc)
     93  1.5    maya {
     94  1.5    maya         double d;
     95  1.5    maya         gimpy_limb_t u;
     96  1.5    maya         int i;
     97  1.5    maya 
     98  1.5    maya         d = 1.0;
     99  1.5    maya         for (i = 0; i < GIMPY_NUMB_BITS - 1; i++)
    100  1.5    maya                 d = d + d;
    101  1.5    maya 
    102  1.5    maya         printf("d = %g\n", d);
    103  1.5    maya         u = (gimpy_limb_t) d;
    104  1.5    maya 
    105  1.5    maya         for (; i > 0; i--) {
    106  1.8    maya                 ATF_CHECK_MSG((u % 2 == 0),
    107  1.8    maya 		    "%"PRIu64" is not an even number! (iteration %d)", u , i);
    108  1.5    maya                 u = u >> 1;
    109  1.5    maya         }
    110  1.5    maya }
    111  1.5    maya 
    112  1.7    maya ATF_TC(rounding_alpha_simple);
    113  1.7    maya ATF_TC_HEAD(rounding_alpha_simple, tc)
    114  1.7    maya {
    115  1.7    maya 	atf_tc_set_md_var(tc, "descr","Checking double to uint64_t edge case");
    116  1.7    maya }
    117  1.7    maya 
    118  1.8    maya 
    119  1.8    maya double rounding_alpha_simple_even = 9223372036854775808.000000; /* 2^63 */
    120  1.8    maya 
    121  1.7    maya ATF_TC_BODY(rounding_alpha_simple, tc)
    122  1.7    maya {
    123  1.8    maya 	uint64_t unsigned_even = rounding_alpha_simple_even;
    124  1.7    maya 
    125  1.7    maya 	ATF_CHECK_MSG(unsigned_even % 2 == 0,
    126  1.9     wiz 	    "2^63 cast to uint64_t is odd (got %"PRIu64")", unsigned_even);
    127  1.7    maya 
    128  1.7    maya }
    129  1.1  jruoho ATF_TP_ADD_TCS(tp)
    130  1.1  jruoho {
    131  1.1  jruoho 
    132  1.1  jruoho 	ATF_TP_ADD_TC(tp, round_dir);
    133  1.5    maya 	ATF_TP_ADD_TC(tp, rounding_alpha);
    134  1.7    maya 	ATF_TP_ADD_TC(tp, rounding_alpha_simple);
    135  1.1  jruoho 
    136  1.1  jruoho 	return atf_no_error();
    137  1.1  jruoho }
    138