about summary refs log tree commit diff
path: root/stdlib/tst-strtod-round.c
blob: a2cd2ab636575d9a58a3b4a94804b5dd88045c42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Test for correct rounding of results of strtod and related
   functions.
   Copyright (C) 2012-2016 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

/* Defining _LIBC_TEST ensures long double math functions are
   declared in the headers.  */
#define _LIBC_TEST 1
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math-tests.h>

struct exactness
{
  bool f;
  bool d;
  bool ld;
};

struct test_results {
  float f;
  double d;
  long double ld;
};

struct test {
  const char *s;
  struct exactness exact;
  struct test_results rd, rn, rz, ru;
};

#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du,	\
	      ld53exact, ld53d, ld53n, ld53z, ld53u,			\
	      ld64iexact, ld64id, ld64in, ld64iz, ld64iu,		\
	      ld64mexact, ld64md, ld64mn, ld64mz, ld64mu,		\
	      ld106exact, ld106d, ld106n, ld106z, ld106u,		\
	      ld113exact, ld113d, ld113n, ld113z, ld113u)		\
  {									\
    s,									\
    { fexact, dexact, ld53exact },					\
    { fd, dd, ld53d },							\
    { fn, dn, ld53n },							\
    { fz, dz, ld53z },							\
    { fu, du, ld53u }							\
  }
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16381
/* This is for the Intel extended float format.  */
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du,	\
	      ld53exact, ld53d, ld53n, ld53z, ld53u,			\
	      ld64iexact, ld64id, ld64in, ld64iz, ld64iu,		\
	      ld64mexact, ld64md, ld64mn, ld64mz, ld64mu,		\
	      ld106exact, ld106d, ld106n, ld106z, ld106u,		\
	      ld113exact, ld113d, ld113n, ld113z, ld113u)		\
  {									\
    s,									\
    { fexact, dexact, ld64iexact },					\
    { fd, dd, ld64id },							\
    { fn, dn, ld64in },							\
    { fz, dz, ld64iz },							\
    { fu, du, ld64iu }							\
  }
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16382
/* This is for the Motorola extended float format.  */
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du,	\
	      ld53exact, ld53d, ld53n, ld53z, ld53u,			\
	      ld64iexact, ld64id, ld64in, ld64iz, ld64iu,		\
	      ld64mexact, ld64md, ld64mn, ld64mz, ld64mu,		\
	      ld106exact, ld106d, ld106n, ld106z, ld106u,		\
	      ld113exact, ld113d, ld113n, ld113z, ld113u)		\
  {									\
    s,									\
    { fexact, dexact, ld64mexact },					\
    { fd, dd, ld64md },							\
    { fn, dn, ld64mn },							\
    { fz, dz, ld64mz },							\
    { fu, du, ld64mu }							\
  }
#elif LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du,	\
	      ld53exact, ld53d, ld53n, ld53z, ld53u,			\
	      ld64iexact, ld64id, ld64in, ld64iz, ld64iu,		\
	      ld64mexact, ld64md, ld64mn, ld64mz, ld64mu,		\
	      ld106exact, ld106d, ld106n, ld106z, ld106u,		\
	      ld113exact, ld113d, ld113n, ld113z, ld113u)		\
  {									\
    s,									\
    { fexact, dexact, ld106exact },					\
    { fd, dd, ld106d },							\
    { fn, dn, ld106n },							\
    { fz, dz, ld106z },							\
    { fu, du, ld106u }							\
  }
#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du,	\
	      ld53exact, ld53d, ld53n, ld53z, ld53u,			\
	      ld64iexact, ld64id, ld64in, ld64iz, ld64iu,		\
	      ld64mexact, ld64md, ld64mn, ld64mz, ld64mu,		\
	      ld106exact, ld106d, ld106n, ld106z, ld106u,		\
	      ld113exact, ld113d, ld113n, ld113z, ld113u)		\
  {									\
    s,									\
    { fexact, dexact, ld113exact },					\
    { fd, dd, ld113d },							\
    { fn, dn, ld113n },							\
    { fz, dz, ld113z },							\
    { fu, du, ld113u }							\
  }
#else
# error "unknown long double format"
#endif

/* Include the generated test data.  */
#include "tst-strtod-round-data.h"

static int
test_in_one_mode (const char *s, const struct test_results *expected,
		  const struct exactness *exact, const char *mode_name,
		  bool float_round_ok, bool double_round_ok,
		  bool long_double_round_ok)
{
  int result = 0;
  float f = strtof (s, NULL);
  double d = strtod (s, NULL);
  long double ld = strtold (s, NULL);
  if (f != expected->f
      || copysignf (1.0f, f) != copysignf (1.0f, expected->f))
    {
      printf ("strtof (%s) returned %a not %a (%s)\n", s, f,
	      expected->f, mode_name);
      if (float_round_ok || exact->f)
	result = 1;
      else
	printf ("ignoring this inexact result\n");
    }
  if (d != expected->d
      || copysign (1.0, d) != copysign (1.0, expected->d))
    {
      printf ("strtod (%s) returned %a not %a (%s)\n", s, d,
	      expected->d, mode_name);
      if (double_round_ok || exact->d)
	result = 1;
      else
	printf ("ignoring this inexact result\n");
    }
  if (ld != expected->ld
      || copysignl (1.0L, ld) != copysignl (1.0L, expected->ld))
    {
      printf ("strtold (%s) returned %La not %La (%s)\n", s, ld,
	      expected->ld, mode_name);
      if ((long_double_round_ok && LDBL_MANT_DIG != 106) || exact->ld)
	result = 1;
      else
	printf ("ignoring this inexact result\n");
    }
  return result;
}

static int
do_test (void)
{
  int save_round_mode __attribute__ ((unused)) = fegetround ();
  int result = 0;
  for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
    {
      result |= test_in_one_mode (tests[i].s, &tests[i].rn, &tests[i].exact,
				  "default rounding mode",
				  true, true, true);
#ifdef FE_DOWNWARD
      if (!fesetround (FE_DOWNWARD))
	{
	  result |= test_in_one_mode (tests[i].s, &tests[i].rd,
				      &tests[i].exact, "FE_DOWNWARD",
				      ROUNDING_TESTS (float, FE_DOWNWARD),
				      ROUNDING_TESTS (double, FE_DOWNWARD),
				      ROUNDING_TESTS (long double,
						      FE_DOWNWARD));
	  fesetround (save_round_mode);
	}
#endif
#ifdef FE_TOWARDZERO
      if (!fesetround (FE_TOWARDZERO))
	{
	  result |= test_in_one_mode (tests[i].s, &tests[i].rz,
				      &tests[i].exact, "FE_TOWARDZERO",
				      ROUNDING_TESTS (float, FE_TOWARDZERO),
				      ROUNDING_TESTS (double, FE_TOWARDZERO),
				      ROUNDING_TESTS (long double,
						      FE_TOWARDZERO));
	  fesetround (save_round_mode);
	}
#endif
#ifdef FE_UPWARD
      if (!fesetround (FE_UPWARD))
	{
	  result |= test_in_one_mode (tests[i].s, &tests[i].ru,
				      &tests[i].exact, "FE_UPWARD",
				      ROUNDING_TESTS (float, FE_UPWARD),
				      ROUNDING_TESTS (double, FE_UPWARD),
				      ROUNDING_TESTS (long double, FE_UPWARD));
	  fesetround (save_round_mode);
	}
#endif
    }
  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"