Home | History | Annotate | Line # | Download | only in libm
t_ldexp.c revision 1.15
      1 /* $NetBSD: t_ldexp.c,v 1.15 2016/08/25 00:26:01 maya Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_ldexp.c,v 1.15 2016/08/25 00:26:01 maya Exp $");
     33 
     34 #include <sys/param.h>
     35 
     36 #include <atf-c.h>
     37 
     38 #include <math.h>
     39 #include <limits.h>
     40 #include <stdio.h>
     41 #include <string.h>
     42 
     43 #define SKIP	9999
     44 #define FORMAT  "%23.23lg"
     45 
     46 static const int exps[] = { 0, 1, -1, 100, -100 };
     47 
     48 struct ldexp_test {
     49 	double	    x;
     50 	int	    exp1;
     51 	int	    exp2;
     52 	const char *result;
     53 };
     54 
     55 struct ldexp_test ldexp_basic[] = {
     56 	{ 1.0,	5,	SKIP,	"                     32" },
     57 	{ 1.0,	1022,	SKIP,	"4.4942328371557897693233e+307" },
     58 	{ 1.0,	1023,	-1,	"4.4942328371557897693233e+307" },
     59 	{ 1.0,	1023,	SKIP,	"8.9884656743115795386465e+307" },
     60 	{ 1.0,	1022,	1,	"8.9884656743115795386465e+307" },
     61 	{ 1.0,	-1022,	2045,	"8.9884656743115795386465e+307" },
     62 	{ 1.0,	-5,	SKIP,	"                0.03125" },
     63 	{ 1.0,	-1021,	SKIP,	"4.4501477170144027661805e-308" },
     64 	{ 1.0,	-1022,	1,	"4.4501477170144027661805e-308" },
     65 	{ 1.0,	-1022,	SKIP,	"2.2250738585072013830902e-308" },
     66 	{ 1.0,	-1021,	-1,	"2.2250738585072013830902e-308" },
     67 	{ 1.0,	1023,	-2045,	"2.2250738585072013830902e-308" },
     68 	{ 1.0,	1023,	-1023,	"                      1" },
     69 	{ 1.0,	-1022,	1022,	"                      1" },
     70 	{ 0,	0,	0,	NULL }
     71 };
     72 
     73 struct ldexp_test ldexp_zero[] = {
     74 	{ 0.0,	-1,	SKIP,	"                      0" },
     75 	{ 0.0,	0,	SKIP,	"                      0" },
     76 	{ 0.0,	1,	SKIP,	"                      0" },
     77 	{ 0.0,	1024,	SKIP,	"                      0" },
     78 	{ 0.0,	1025,	SKIP,	"                      0" },
     79 	{ 0.0,	-1023,	SKIP,	"                      0" },
     80 	{ 0.0,	-1024,	SKIP,	"                      0" },
     81 	{ 0,	0,	0,	NULL }
     82 };
     83 
     84 struct ldexp_test ldexp_infinity[] = {
     85 	{ 1.0,	1024,	-1,	"                    inf" },
     86 	{ 1.0,	1024,	0,	"                    inf" },
     87 	{ 1.0,	1024,	1,	"                    inf" },
     88 	{ -1.0,	1024,	-1,	"                   -inf" },
     89 	{ -1.0,	1024,	0,	"                   -inf" },
     90 	{ -1.0,	1024,	1,	"                   -inf" },
     91 	{ 0,	0,	0,	NULL }
     92 };
     93 
     94 struct ldexp_test ldexp_overflow[] = {
     95 	{ 1.0,	1024,	SKIP,	"                    inf" },
     96 	{ 1.0,	1023,	1,	"                    inf" },
     97 	{ 1.0,	-1022,	2046,	"                    inf" },
     98 	{ 1.0,	1025,	SKIP,	"                    inf" },
     99 	{ -1.0,	1024,	SKIP,	"                   -inf" },
    100 	{ -1.0,	1023,	1,	"                   -inf" },
    101 	{ -1.0,	-1022,	2046,	"                   -inf" },
    102 	{ -1.0,	1025,	SKIP,	"                   -inf" },
    103 	{ 0,	0,	0,	NULL }
    104 };
    105 
    106 struct ldexp_test ldexp_denormal[] = {
    107 	{ 1.0,	-1023,	SKIP,	"1.1125369292536006915451e-308" },
    108 	{ 1.0,	-1022,	-1,	"1.1125369292536006915451e-308" },
    109 	{ 1.0,	1023,	-2046,	"1.1125369292536006915451e-308" },
    110 	{ 1.0,	-1024,	SKIP,	"5.5626846462680034577256e-309" },
    111 	{ 1.0,	-1074,	SKIP,	"4.9406564584124654417657e-324" },
    112 	{ -1.0,	-1023,	SKIP,	"-1.1125369292536006915451e-308" },
    113 	{ -1.0,	-1022,	-1,	"-1.1125369292536006915451e-308" },
    114 	{ -1.0,	1023,	-2046,	"-1.1125369292536006915451e-308" },
    115 	{ -1.0,	-1024,	SKIP,	"-5.5626846462680034577256e-309" },
    116 	{ -1.0,	-1074,	SKIP,	"-4.9406564584124654417657e-324" },
    117 	{ 0,	0,	0,	NULL }
    118 };
    119 
    120 struct ldexp_test ldexp_underflow[] = {
    121 	{ 1.0,	-1075,	SKIP,	"                      0" },
    122 	{ 1.0,	-1074,	-1,	"                      0" },
    123 	{ 1.0,	1023,	-2098,	"                      0" },
    124 	{ 1.0,	-1076,	SKIP,	"                      0" },
    125 	{ -1.0,	-1075,	SKIP,	"                     -0" },
    126 	{ -1.0,	-1074,	-1,	"                     -0" },
    127 	{ -1.0,	1023,	-2098,	"                     -0" },
    128 	{ -1.0,	-1076,	SKIP,	"                     -0" },
    129 	{ 0,	0,	0,	NULL }
    130 };
    131 
    132 struct ldexp_test ldexp_denormal_large[] = {
    133 	{ 1.0,	-1028,	1024,	"                 0.0625" },
    134 	{ 1.0,	-1028,	1025,	"                  0.125" },
    135 	{ 1.0,	-1028,	1026,	"                   0.25" },
    136 	{ 1.0,	-1028,	1027,	"                    0.5" },
    137 	{ 1.0,	-1028,	1028,	"                      1" },
    138 	{ 1.0,	-1028,	1029,	"                      2" },
    139 	{ 1.0,	-1028,	1030,	"                      4" },
    140 	{ 1.0,	-1028,	1040,	"                   4096" },
    141 	{ 1.0,	-1028,	1050,	"                4194304" },
    142 	{ 1.0,	-1028,	1060,	"             4294967296" },
    143 	{ 1.0,	-1028,	1100,	" 4722366482869645213696" },
    144 	{ 1.0,	-1028,	1200,	"5.9863107065073783529623e+51" },
    145 	{ 1.0,	-1028,	1300,	"7.5885503602567541832791e+81" },
    146 	{ 1.0,	-1028,	1400,	"9.6196304190416209014353e+111" },
    147 	{ 1.0,	-1028,	1500,	"1.2194330274671844653834e+142" },
    148 	{ 1.0,	-1028,	1600,	"1.5458150092069033378781e+172" },
    149 	{ 1.0,	-1028,	1700,	"1.9595533242629369747791e+202" },
    150 	{ 1.0,	-1028,	1800,	"2.4840289476811342962384e+232" },
    151 	{ 1.0,	-1028,	1900,	"3.1488807865122869393369e+262" },
    152 	{ 1.0,	-1028,	2000,	"3.9916806190694396233127e+292" },
    153 	{ 1.0,	-1028,	2046,	"2.808895523222368605827e+306" },
    154 	{ 1.0,	-1028,	2047,	"5.6177910464447372116541e+306" },
    155 	{ 1.0,	-1028,	2048,	"1.1235582092889474423308e+307" },
    156 	{ 1.0,	-1028,	2049,	"2.2471164185778948846616e+307" },
    157 	{ 1.0,	-1028,	2050,	"4.4942328371557897693233e+307" },
    158 	{ 1.0,	-1028,	2051,	"8.9884656743115795386465e+307" },
    159 	{ 0,	0,	0,	NULL }
    160 };
    161 
    162 static void
    163 run_test(struct ldexp_test *table)
    164 {
    165 	char outbuf[64];
    166 	size_t i;
    167 	double v;
    168 
    169 	for (i = 0; table->result != NULL; table++, i++) {
    170 
    171 		v = ldexp(table->x, table->exp1);
    172 
    173 		if (table->exp2 != SKIP)
    174 			v = ldexp(v, table->exp2);
    175 
    176 		(void)snprintf(outbuf, sizeof(outbuf), FORMAT, v);
    177 
    178 		ATF_CHECK_STREQ_MSG(table->result, outbuf,
    179 			    "Entry %zu:\n\tExp: \"%s\"\n\tAct: \"%s\"",
    180 			    i, table->result, outbuf);
    181 	}
    182 }
    183 
    184 /*
    185  * ldexp(3)
    186  */
    187 ATF_TC(ldexp_exp2);
    188 ATF_TC_HEAD(ldexp_exp2, tc)
    189 {
    190 	atf_tc_set_md_var(tc, "descr", "Test ldexp(x, n) == x * exp2(n)");
    191 }
    192 
    193 ATF_TC_BODY(ldexp_exp2, tc)
    194 {
    195 	const double n[] = { 1, 2, 3, 10, 50, 100 };
    196 #if __DBL_MIN_10_EXP__ <= -40
    197 	const double eps = 1.0e-40;
    198 #else
    199 	const double eps = __DBL_MIN__*4.0;
    200 #endif
    201 	const double x = 12.0;
    202 	double y;
    203 	size_t i;
    204 
    205 	for (i = 0; i < __arraycount(n); i++) {
    206 
    207 		y = ldexp(x, n[i]);
    208 
    209 		if (fabs(y - (x * exp2(n[i]))) > eps) {
    210 			atf_tc_fail_nonfatal("ldexp(%0.01f, %0.01f) "
    211 			    "!= %0.01f * exp2(%0.01f)", x, n[i], x, n[i]);
    212 		}
    213 	}
    214 }
    215 
    216 ATF_TC(ldexp_nan);
    217 ATF_TC_HEAD(ldexp_nan, tc)
    218 {
    219 	atf_tc_set_md_var(tc, "descr", "Test ldexp(NaN) == NaN");
    220 }
    221 
    222 ATF_TC_BODY(ldexp_nan, tc)
    223 {
    224 	const double x = 0.0L / 0.0L;
    225 	double y;
    226 	size_t i;
    227 
    228 	ATF_REQUIRE(isnan(x) != 0);
    229 
    230 	for (i = 0; i < __arraycount(exps); i++) {
    231 		y = ldexp(x, exps[i]);
    232 		ATF_CHECK(isnan(y) != 0);
    233 	}
    234 }
    235 
    236 ATF_TC(ldexp_inf_neg);
    237 ATF_TC_HEAD(ldexp_inf_neg, tc)
    238 {
    239 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-Inf) == -Inf");
    240 }
    241 
    242 ATF_TC_BODY(ldexp_inf_neg, tc)
    243 {
    244 	const double x = -1.0L / 0.0L;
    245 	size_t i;
    246 
    247 	for (i = 0; i < __arraycount(exps); i++)
    248 		ATF_CHECK(ldexp(x, exps[i]) == x);
    249 }
    250 
    251 ATF_TC(ldexp_inf_pos);
    252 ATF_TC_HEAD(ldexp_inf_pos, tc)
    253 {
    254 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+Inf) == +Inf");
    255 }
    256 
    257 ATF_TC_BODY(ldexp_inf_pos, tc)
    258 {
    259 	const double x = 1.0L / 0.0L;
    260 	size_t i;
    261 
    262 	for (i = 0; i < __arraycount(exps); i++)
    263 		ATF_CHECK(ldexp(x, exps[i]) == x);
    264 }
    265 
    266 ATF_TC(ldexp_zero_neg);
    267 ATF_TC_HEAD(ldexp_zero_neg, tc)
    268 {
    269 	atf_tc_set_md_var(tc, "descr", "Test ldexp(-0.0) == -0.0");
    270 }
    271 
    272 ATF_TC_BODY(ldexp_zero_neg, tc)
    273 {
    274 	const double x = -0.0L;
    275 	double y;
    276 	size_t i;
    277 
    278 	ATF_REQUIRE(signbit(x) != 0);
    279 
    280 	for (i = 0; i < __arraycount(exps); i++) {
    281 		y = ldexp(x, exps[i]);
    282 		ATF_CHECK(x == y);
    283 		ATF_CHECK(signbit(y) != 0);
    284 	}
    285 }
    286 
    287 ATF_TC(ldexp_zero_pos);
    288 ATF_TC_HEAD(ldexp_zero_pos, tc)
    289 {
    290 	atf_tc_set_md_var(tc, "descr", "Test ldexp(+0.0) == +0.0");
    291 }
    292 
    293 ATF_TC_BODY(ldexp_zero_pos, tc)
    294 {
    295 	const double x = 0.0L;
    296 	double y;
    297 	size_t i;
    298 
    299 	ATF_REQUIRE(signbit(x) == 0);
    300 
    301 	for (i = 0; i < __arraycount(exps); i++) {
    302 		y = ldexp(x, exps[i]);
    303 		ATF_CHECK(x == y);
    304 		ATF_CHECK(signbit(y) == 0);
    305 	}
    306 }
    307 
    308 /*
    309  * ldexpf(3)
    310  */
    311 
    312 ATF_TC(ldexpf_exp2f);
    313 ATF_TC_HEAD(ldexpf_exp2f, tc)
    314 {
    315 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(x, n) == x * exp2f(n)");
    316 }
    317 
    318 ATF_TC_BODY(ldexpf_exp2f, tc)
    319 {
    320 	const float n[] = { 1, 2, 3, 10, 50, 100 };
    321 	const float eps = 1.0e-9;
    322 	const float x = 12.0;
    323 	float y;
    324 	size_t i;
    325 
    326 	for (i = 0; i < __arraycount(n); i++) {
    327 
    328 		y = ldexpf(x, n[i]);
    329 
    330 		if (fabsf(y - (x * exp2f(n[i]))) > eps) {
    331 			atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) "
    332 			    "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]);
    333 		}
    334 	}
    335 }
    336 
    337 ATF_TC(ldexpf_nan);
    338 ATF_TC_HEAD(ldexpf_nan, tc)
    339 {
    340 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(NaN) == NaN");
    341 }
    342 
    343 ATF_TC_BODY(ldexpf_nan, tc)
    344 {
    345 	const float x = 0.0L / 0.0L;
    346 	float y;
    347 	size_t i;
    348 
    349 	ATF_REQUIRE(isnan(x) != 0);
    350 
    351 	for (i = 0; i < __arraycount(exps); i++) {
    352 		y = ldexpf(x, exps[i]);
    353 		ATF_CHECK(isnan(y) != 0);
    354 	}
    355 }
    356 
    357 ATF_TC(ldexpf_inf_neg);
    358 ATF_TC_HEAD(ldexpf_inf_neg, tc)
    359 {
    360 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-Inf) == -Inf");
    361 }
    362 
    363 ATF_TC_BODY(ldexpf_inf_neg, tc)
    364 {
    365 	const float x = -1.0L / 0.0L;
    366 	size_t i;
    367 
    368 	for (i = 0; i < __arraycount(exps); i++)
    369 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    370 }
    371 
    372 ATF_TC(ldexpf_inf_pos);
    373 ATF_TC_HEAD(ldexpf_inf_pos, tc)
    374 {
    375 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+Inf) == +Inf");
    376 }
    377 
    378 ATF_TC_BODY(ldexpf_inf_pos, tc)
    379 {
    380 	const float x = 1.0L / 0.0L;
    381 	size_t i;
    382 
    383 	for (i = 0; i < __arraycount(exps); i++)
    384 		ATF_CHECK(ldexpf(x, exps[i]) == x);
    385 }
    386 
    387 ATF_TC(ldexpf_zero_neg);
    388 ATF_TC_HEAD(ldexpf_zero_neg, tc)
    389 {
    390 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(-0.0) == -0.0");
    391 }
    392 
    393 ATF_TC_BODY(ldexpf_zero_neg, tc)
    394 {
    395 	const float x = -0.0L;
    396 	float y;
    397 	size_t i;
    398 
    399 	ATF_REQUIRE(signbit(x) != 0);
    400 
    401 	for (i = 0; i < __arraycount(exps); i++) {
    402 		y = ldexpf(x, exps[i]);
    403 		ATF_CHECK(x == y);
    404 		ATF_CHECK(signbit(y) != 0);
    405 	}
    406 }
    407 
    408 ATF_TC(ldexpf_zero_pos);
    409 ATF_TC_HEAD(ldexpf_zero_pos, tc)
    410 {
    411 	atf_tc_set_md_var(tc, "descr", "Test ldexpf(+0.0) == +0.0");
    412 }
    413 
    414 ATF_TC_BODY(ldexpf_zero_pos, tc)
    415 {
    416 	const float x = 0.0L;
    417 	float y;
    418 	size_t i;
    419 
    420 	ATF_REQUIRE(signbit(x) == 0);
    421 
    422 	for (i = 0; i < __arraycount(exps); i++) {
    423 		y = ldexpf(x, exps[i]);
    424 		ATF_CHECK(x == y);
    425 		ATF_CHECK(signbit(y) == 0);
    426 	}
    427 }
    428 
    429 #define TEST(name, desc)						\
    430 	ATF_TC(name);							\
    431 	ATF_TC_HEAD(name, tc)						\
    432 	{								\
    433 									\
    434 		atf_tc_set_md_var(tc, "descr",				\
    435 		    "Test ldexp(3) for " ___STRING(desc));		\
    436 	}								\
    437 	ATF_TC_BODY(name, tc)						\
    438 	{								\
    439 		if (strcmp("vax", MACHINE_ARCH) == 0)			\
    440 			atf_tc_skip("Test not valid for " MACHINE_ARCH); \
    441 		run_test(name);						\
    442 	}
    443 
    444 TEST(ldexp_basic, basics)
    445 TEST(ldexp_zero, zero)
    446 TEST(ldexp_infinity, infinity)
    447 TEST(ldexp_overflow, overflow)
    448 TEST(ldexp_denormal, denormal)
    449 TEST(ldexp_denormal_large, large)
    450 TEST(ldexp_underflow, underflow)
    451 
    452 ATF_TP_ADD_TCS(tp)
    453 {
    454 
    455 	ATF_TP_ADD_TC(tp, ldexp_basic);
    456 	ATF_TP_ADD_TC(tp, ldexp_zero);
    457 	ATF_TP_ADD_TC(tp, ldexp_infinity);
    458 	ATF_TP_ADD_TC(tp, ldexp_overflow);
    459 	ATF_TP_ADD_TC(tp, ldexp_denormal);
    460 	ATF_TP_ADD_TC(tp, ldexp_underflow);
    461 	ATF_TP_ADD_TC(tp, ldexp_denormal_large);
    462 
    463 	ATF_TP_ADD_TC(tp, ldexp_exp2);
    464 	ATF_TP_ADD_TC(tp, ldexp_nan);
    465 	ATF_TP_ADD_TC(tp, ldexp_inf_neg);
    466 	ATF_TP_ADD_TC(tp, ldexp_inf_pos);
    467 	ATF_TP_ADD_TC(tp, ldexp_zero_neg);
    468 	ATF_TP_ADD_TC(tp, ldexp_zero_pos);
    469 
    470 	ATF_TP_ADD_TC(tp, ldexpf_exp2f);
    471 	ATF_TP_ADD_TC(tp, ldexpf_nan);
    472 	ATF_TP_ADD_TC(tp, ldexpf_inf_neg);
    473 	ATF_TP_ADD_TC(tp, ldexpf_inf_pos);
    474 	ATF_TP_ADD_TC(tp, ldexpf_zero_neg);
    475 	ATF_TP_ADD_TC(tp, ldexpf_zero_pos);
    476 
    477 	return atf_no_error();
    478 }
    479