1*1ec3feb6SAlex Richardson /* $NetBSD: t_bit.c,v 1.1 2019/04/26 08:52:16 maya Exp $ */
2*1ec3feb6SAlex Richardson
3*1ec3feb6SAlex Richardson /*
4*1ec3feb6SAlex Richardson * Written by Maya Rashish <maya@NetBSD.org>
5*1ec3feb6SAlex Richardson * Public domain.
6*1ec3feb6SAlex Richardson *
7*1ec3feb6SAlex Richardson * Testing signbit{,f,l} function correctly
8*1ec3feb6SAlex Richardson */
9*1ec3feb6SAlex Richardson
10*1ec3feb6SAlex Richardson #include <atf-c.h>
11*1ec3feb6SAlex Richardson #include <float.h>
12*1ec3feb6SAlex Richardson #include <math.h>
13*1ec3feb6SAlex Richardson #include <stdio.h>
14*1ec3feb6SAlex Richardson #include <stdint.h>
15*1ec3feb6SAlex Richardson #include <stdbool.h>
16*1ec3feb6SAlex Richardson
17*1ec3feb6SAlex Richardson static const struct {
18*1ec3feb6SAlex Richardson double input;
19*1ec3feb6SAlex Richardson bool is_negative;
20*1ec3feb6SAlex Richardson } values[] = {
21*1ec3feb6SAlex Richardson { -1, true},
22*1ec3feb6SAlex Richardson { -123, true},
23*1ec3feb6SAlex Richardson { -123E6, true},
24*1ec3feb6SAlex Richardson #ifdef INFINITY
25*1ec3feb6SAlex Richardson { -INFINITY, true},
26*1ec3feb6SAlex Richardson { INFINITY, false},
27*1ec3feb6SAlex Richardson #endif
28*1ec3feb6SAlex Richardson { 123E6, false},
29*1ec3feb6SAlex Richardson { 0, false},
30*1ec3feb6SAlex Richardson { -FLT_MIN, true},
31*1ec3feb6SAlex Richardson { FLT_MIN, false},
32*1ec3feb6SAlex Richardson /*
33*1ec3feb6SAlex Richardson * Cannot be accurately represented as float,
34*1ec3feb6SAlex Richardson * but sign should be preserved
35*1ec3feb6SAlex Richardson */
36*1ec3feb6SAlex Richardson { DBL_MAX, false},
37*1ec3feb6SAlex Richardson { -DBL_MAX, true},
38*1ec3feb6SAlex Richardson };
39*1ec3feb6SAlex Richardson
40*1ec3feb6SAlex Richardson #ifdef __HAVE_LONG_DOUBLE
41*1ec3feb6SAlex Richardson static const struct {
42*1ec3feb6SAlex Richardson long double input;
43*1ec3feb6SAlex Richardson bool is_negative;
44*1ec3feb6SAlex Richardson } ldbl_values[] = {
45*1ec3feb6SAlex Richardson { -LDBL_MIN, true},
46*1ec3feb6SAlex Richardson { LDBL_MIN, false},
47*1ec3feb6SAlex Richardson { LDBL_MAX, false},
48*1ec3feb6SAlex Richardson { -LDBL_MAX, true},
49*1ec3feb6SAlex Richardson };
50*1ec3feb6SAlex Richardson #endif
51*1ec3feb6SAlex Richardson
52*1ec3feb6SAlex Richardson ATF_TC(signbit);
ATF_TC_HEAD(signbit,tc)53*1ec3feb6SAlex Richardson ATF_TC_HEAD(signbit, tc)
54*1ec3feb6SAlex Richardson {
55*1ec3feb6SAlex Richardson atf_tc_set_md_var(tc, "descr","Check that signbit functions correctly");
56*1ec3feb6SAlex Richardson }
57*1ec3feb6SAlex Richardson
ATF_TC_BODY(signbit,tc)58*1ec3feb6SAlex Richardson ATF_TC_BODY(signbit, tc)
59*1ec3feb6SAlex Richardson {
60*1ec3feb6SAlex Richardson double iterator_d;
61*1ec3feb6SAlex Richardson float iterator_f;
62*1ec3feb6SAlex Richardson
63*1ec3feb6SAlex Richardson for (unsigned int i = 0; i < __arraycount(values); i++) {
64*1ec3feb6SAlex Richardson iterator_d = values[i].input;
65*1ec3feb6SAlex Richardson iterator_f = (float) values[i].input;
66*1ec3feb6SAlex Richardson if (signbit(iterator_f) != values[i].is_negative)
67*1ec3feb6SAlex Richardson atf_tc_fail("%s:%d iteration %d signbitf is wrong"
68*1ec3feb6SAlex Richardson " about the sign of %f", __func__,
69*1ec3feb6SAlex Richardson __LINE__, i, iterator_f);
70*1ec3feb6SAlex Richardson if (signbit(iterator_d) != values[i].is_negative)
71*1ec3feb6SAlex Richardson atf_tc_fail("%s:%d iteration %d signbit is wrong"
72*1ec3feb6SAlex Richardson "about the sign of %f", __func__,
73*1ec3feb6SAlex Richardson __LINE__,i, iterator_d);
74*1ec3feb6SAlex Richardson
75*1ec3feb6SAlex Richardson #ifdef __HAVE_LONG_DOUBLE
76*1ec3feb6SAlex Richardson long double iterator_l = values[i].input;
77*1ec3feb6SAlex Richardson if (signbit(iterator_l) != values[i].is_negative)
78*1ec3feb6SAlex Richardson atf_tc_fail("%s:%d iteration %d signbitl is wrong"
79*1ec3feb6SAlex Richardson " about the sign of %Lf", __func__,
80*1ec3feb6SAlex Richardson __LINE__, i, iterator_l);
81*1ec3feb6SAlex Richardson #endif
82*1ec3feb6SAlex Richardson }
83*1ec3feb6SAlex Richardson
84*1ec3feb6SAlex Richardson #ifdef __HAVE_LONG_DOUBLE
85*1ec3feb6SAlex Richardson for (unsigned int i = 0; i < __arraycount(ldbl_values); i++) {
86*1ec3feb6SAlex Richardson if (signbit(ldbl_values[i].input) != ldbl_values[i].is_negative)
87*1ec3feb6SAlex Richardson atf_tc_fail("%s:%d iteration %d signbitl is"
88*1ec3feb6SAlex Richardson "wrong about the sign of %Lf",
89*1ec3feb6SAlex Richardson __func__, __LINE__, i,
90*1ec3feb6SAlex Richardson ldbl_values[i].input);
91*1ec3feb6SAlex Richardson }
92*1ec3feb6SAlex Richardson #endif
93*1ec3feb6SAlex Richardson
94*1ec3feb6SAlex Richardson }
95*1ec3feb6SAlex Richardson
ATF_TP_ADD_TCS(tp)96*1ec3feb6SAlex Richardson ATF_TP_ADD_TCS(tp)
97*1ec3feb6SAlex Richardson {
98*1ec3feb6SAlex Richardson
99*1ec3feb6SAlex Richardson ATF_TP_ADD_TC(tp, signbit);
100*1ec3feb6SAlex Richardson
101*1ec3feb6SAlex Richardson return atf_no_error();
102*1ec3feb6SAlex Richardson }
103