Path: blob/main/lib/libc/tests/gen/fpclassify_test.c
289379 views
/* $NetBSD: t_fpclassify.c,v 1.3 2011/10/01 21:47:08 christos Exp $ */12/*-3* Copyright (c) 2011 The NetBSD Foundation, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS16* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED17* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR18* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS19* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR20* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF21* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS22* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN23* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)24* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE25* POSSIBILITY OF SUCH DAMAGE.26*/2728#include <atf-c.h>2930#include <float.h>31#include <math.h>32#include <stdio.h>33#include <string.h>3435#ifndef __i386__3637ATF_TC(no_test);38ATF_TC_HEAD(no_test, tc)39{40atf_tc_set_md_var(tc, "descr", "Dummy test");41}4243ATF_TC_BODY(no_test,tc)44{45atf_tc_skip("Test not available on this architecture");46}4748#else /* defined(__i386__) */4950#undef LDBL_MANT_DIG51#define LDBL_MANT_DIG DBL_MANT_DIG5253ATF_TC(fpclassify_float);54ATF_TC_HEAD(fpclassify_float, tc)55{5657atf_tc_set_md_var(tc, "descr", "Test float operations");58}5960ATF_TC_BODY(fpclassify_float, tc)61{62float d0, d1, d2, f, ip;63int e, i;6465d0 = FLT_MIN;66ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);67f = frexpf(d0, &e);68ATF_REQUIRE_EQ(e, FLT_MIN_EXP);69ATF_REQUIRE_EQ(f, 0.5);70d1 = d0;7172/* shift a "1" bit through the mantissa (skip the implicit bit) */73for (i = 1; i < FLT_MANT_DIG; i++) {74d1 /= 2;75ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);76ATF_REQUIRE(d1 > 0 && d1 < d0);7778d2 = ldexpf(d0, -i);79ATF_REQUIRE_EQ(d2, d1);8081d2 = modff(d1, &ip);82ATF_REQUIRE_EQ(d2, d1);83ATF_REQUIRE_EQ(ip, 0);8485f = frexpf(d1, &e);86ATF_REQUIRE_EQ(e, FLT_MIN_EXP - i);87ATF_REQUIRE_EQ(f, 0.5);88}8990d1 /= 2;91ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);92f = frexpf(d1, &e);93ATF_REQUIRE_EQ(e, 0);94ATF_REQUIRE_EQ(f, 0);95}9697ATF_TC(fpclassify_double);98ATF_TC_HEAD(fpclassify_double, tc)99{100101atf_tc_set_md_var(tc, "descr", "Test double operations");102}103104ATF_TC_BODY(fpclassify_double, tc)105{106double d0, d1, d2, f, ip;107int e, i;108109d0 = DBL_MIN;110ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);111f = frexp(d0, &e);112ATF_REQUIRE_EQ(e, DBL_MIN_EXP);113ATF_REQUIRE_EQ(f, 0.5);114d1 = d0;115116/* shift a "1" bit through the mantissa (skip the implicit bit) */117for (i = 1; i < DBL_MANT_DIG; i++) {118d1 /= 2;119ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);120ATF_REQUIRE(d1 > 0 && d1 < d0);121122d2 = ldexp(d0, -i);123ATF_REQUIRE_EQ(d2, d1);124125d2 = modf(d1, &ip);126ATF_REQUIRE_EQ(d2, d1);127ATF_REQUIRE_EQ(ip, 0);128129f = frexp(d1, &e);130ATF_REQUIRE_EQ(e, DBL_MIN_EXP - i);131ATF_REQUIRE_EQ(f, 0.5);132}133134d1 /= 2;135ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);136f = frexp(d1, &e);137ATF_REQUIRE_EQ(e, 0);138ATF_REQUIRE_EQ(f, 0);139}140141/*142* XXX NetBSD doesn't have long-double flavors of frexp, ldexp, and modf,143* XXX so this test is disabled.144*/145146#ifdef TEST_LONG_DOUBLE147148ATF_TC(fpclassify_long_double);149ATF_TC_HEAD(fpclassify_long_double, tc)150{151152atf_tc_set_md_var(tc, "descr", "Test long double operations");153}154155ATF_TC_BODY(fpclassify_long_double, tc)156{157long double d0, d1, d2, f, ip;158int e, i;159160d0 = LDBL_MIN;161ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL);162f = frexpl(d0, &e);163ATF_REQUIRE_EQ(e, LDBL_MIN_EXP);164ATF_REQUIRE_EQ(f, 0.5);165d1 = d0;166167/* shift a "1" bit through the mantissa (skip the implicit bit) */168for (i = 1; i < LDBL_MANT_DIG; i++) {169d1 /= 2;170ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL);171ATF_REQUIRE(d1 > 0 && d1 < d0);172173d2 = ldexpl(d0, -i);174ATF_REQUIRE_EQ(d2, d1);175176d2 = modfl(d1, &ip);177ATF_REQUIRE_EQ(d2, d1);178ATF_REQUIRE_EQ(ip, 0);179180f = frexpl(d1, &e);181ATF_REQUIRE_EQ(e, LDBL_MIN_EXP - i);182ATF_REQUIRE_EQ(f, 0.5);183}184185d1 /= 2;186ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO);187f = frexpl(d1, &e);188ATF_REQUIRE_EQ(e, 0);189ATF_REQUIRE_EQ(f, 0);190}191#endif /* TEST_LONG_DOUBLE */192#endif /* __i386__ */193194ATF_TP_ADD_TCS(tp)195{196197#ifndef __i386__198ATF_TP_ADD_TC(tp, no_test);199#else200ATF_TP_ADD_TC(tp, fpclassify_float);201ATF_TP_ADD_TC(tp, fpclassify_double);202#ifdef TEST_LONG_DOUBLE203ATF_TP_ADD_TC(tp, fpclassify_long_double);204#endif /* TEST_LONG_DOUBLE */205#endif /* __i386__ */206207return atf_no_error();208}209210211