summary refs log tree commit diff
path: root/math/test-fenv-tls.c
blob: e059103cf7665833ae631540f2f4b910aff1780e (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
/* Test floating-point environment is thread-local.
   Copyright (C) 2013-2014 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/>.  */

#include <fenv.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>

#define TEST_ONE_RM(RM)						\
  do								\
    {								\
      if (fesetround (RM) == 0)					\
	{							\
	  rm = fegetround ();					\
	  if (rm != RM)						\
	    {							\
	      printf ("expected " #RM ", got %d\n", rm);	\
	      ret = 1;						\
	    }							\
	}							\
    }								\
  while (0)

static void *
test_round (void *arg)
{
  intptr_t ret = 0;
  for (int i = 0; i < 10000; i++)
    {
      int rm;
#ifdef FE_DOWNWARD
      TEST_ONE_RM (FE_DOWNWARD);
#endif
#ifdef FE_TONEAREST
      TEST_ONE_RM (FE_TONEAREST);
#endif
#ifdef FE_TOWARDZERO
      TEST_ONE_RM (FE_TOWARDZERO);
#endif
#ifdef FE_UPWARD
      TEST_ONE_RM (FE_UPWARD);
#endif
    }
  return (void *) ret;
}

#define TEST_ONE_RAISE(EX)				\
  do							\
    {							\
      if (feraiseexcept (EX) == 0)			\
	if (fetestexcept (EX) != EX)			\
	  {						\
	    printf (#EX " not raised\n");		\
	    ret = 1;					\
	  }						\
      if (feclearexcept (FE_ALL_EXCEPT) == 0)		\
	if (fetestexcept (FE_ALL_EXCEPT) != 0)		\
	  {						\
	    printf ("exceptions not all cleared\n");	\
	    ret = 1;					\
	  }						\
    }							\
  while (0)

static void *
test_raise (void *arg)
{
  intptr_t ret = 0;
  for (int i = 0; i < 10000; i++)
    {
#ifdef FE_DIVBYZERO
      TEST_ONE_RAISE (FE_DIVBYZERO);
#endif
#ifdef FE_INEXACT
      TEST_ONE_RAISE (FE_INEXACT);
#endif
#ifdef FE_INVALID
      TEST_ONE_RAISE (FE_INVALID);
#endif
#ifdef FE_OVERFLOW
      TEST_ONE_RAISE (FE_OVERFLOW);
#endif
#ifdef UNDERFLOW
      TEST_ONE_RAISE (FE_UNDERFLOW);
#endif
    }
  return (void *) ret;
}

#define TEST_ONE_ENABLE(EX)				\
  do							\
    {							\
      if (feenableexcept (EX) != -1)			\
	if (fegetexcept () != EX)			\
	  {						\
	    printf (#EX " not enabled\n");		\
	    ret = 1;					\
	  }						\
      if (fedisableexcept (EX) != -1)			\
	if (fegetexcept () != 0)			\
	  {						\
	    printf ("exceptions not all disabled\n");	\
	    ret = 1;					\
	  }						\
    }							\
  while (0)

static void *
test_enable (void *arg)
{
  intptr_t ret = 0;
  for (int i = 0; i < 10000; i++)
    {
#ifdef FE_DIVBYZERO
      TEST_ONE_ENABLE (FE_DIVBYZERO);
#endif
#ifdef FE_INEXACT
      TEST_ONE_ENABLE (FE_INEXACT);
#endif
#ifdef FE_INVALID
      TEST_ONE_ENABLE (FE_INVALID);
#endif
#ifdef FE_OVERFLOW
      TEST_ONE_ENABLE (FE_OVERFLOW);
#endif
#ifdef UNDERFLOW
      TEST_ONE_ENABLE (FE_UNDERFLOW);
#endif
    }
  return (void *) ret;
}

static int
do_test (void)
{
  int ret = 0;
  void *vret;
  pthread_t thread_id;
  int pret;

  pret = pthread_create (&thread_id, NULL, test_round, NULL);
  if (pret != 0)
    {
      printf ("pthread_create failed: %d\n", pret);
      return 1;
    }
  vret = test_round (NULL);
  ret |= (intptr_t) vret;
  pret = pthread_join (thread_id, &vret);
  if (pret != 0)
    {
      printf ("pthread_join failed: %d\n", pret);
      return 1;
    }
  ret |= (intptr_t) vret;

  pret = pthread_create (&thread_id, NULL, test_raise, NULL);
  if (pret != 0)
    {
      printf ("pthread_create failed: %d\n", pret);
      return 1;
    }
  vret = test_raise (NULL);
  ret |= (intptr_t) vret;
  pret = pthread_join (thread_id, &vret);
  if (pret != 0)
    {
      printf ("pthread_join failed: %d\n", pret);
      return 1;
    }
  ret |= (intptr_t) vret;

  pret = pthread_create (&thread_id, NULL, test_enable, NULL);
  if (pret != 0)
    {
      printf ("pthread_create failed: %d\n", pret);
      return 1;
    }
  vret = test_enable (NULL);
  ret |= (intptr_t) vret;
  pret = pthread_join (thread_id, &vret);
  if (pret != 0)
    {
      printf ("pthread_join failed: %d\n", pret);
      return 1;
    }
  ret |= (intptr_t) vret;

  return ret;
}

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