summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/dfp/convert-dfp-round-thread.c
blob: d288ac70c826f64519d6081b8b8f8fce6901bc5b (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* { dg-options "-std=gnu99 -D_GNU_SOURCE -pthread" } */
/* { dg-do run { target i?86-*-linux* x86_64-*-linux* } } */

/* N1150 5.2: Conversions among decimal floating types and between
   decimal floating types and generic floating types.
   C99 6.3.1.5(3) New.

   Perform conversions between DFP types in which the assigned value
   cannot be represented exactly in the result and must be rounded
   correctly according to the current rounding mode.

   Normally this would not be part of compiler testing, but conversions
   are currently handled in libgcc via decNumber.  */

#include <pthread.h>
#include <error.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include "dfp-round.h"

extern void abort (void);
static __thread int failcnt = 0;

/* Support compiling the test to report individual failures; default is
   to abort as soon as a check fails.  */
#ifdef DBG
#include <stdio.h>
#define FAILURE { printf ("failed at line %d\n", __LINE__); failcnt++; }
#else
#define FAILURE abort ();
#endif

pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mut3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mut4 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mut5 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond4 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond5 = PTHREAD_COND_INITIALIZER;
pthread_barrier_t bar1;
pthread_barrier_t bar2;
pthread_barrier_t bar3;
pthread_barrier_t bar4;
pthread_barrier_t bar5;

__thread _Decimal32 d32;
__thread _Decimal64 d64;
__thread _Decimal128 d128;

_Decimal32 d64_to_d32 (_Decimal64 d) { return d; }
_Decimal64 d128_to_d64 (_Decimal128 d) { return d; }
_Decimal32 d128_to_d32 (_Decimal128 d) { return d; }

int
do_d64_to_d32 (_Decimal64 orig, _Decimal32 exp)
{
  d64 = orig;
  d32 = d64_to_d32 (d64);
  return (d32 == exp);
}

int
do_d128_to_d32 (_Decimal128 orig, _Decimal32 exp)
{
  d128 = orig;
  d32 = d128_to_d32 (d128);
  return (d32 == exp);
}

int
do_d128_to_d64 (_Decimal128 orig, _Decimal64 exp)
{
  d128 = orig;
  d64 = d128_to_d64 (d128);
  return (d64 == exp);
}

void *
downward (void *arg)
{
  int err;

  DFP_SETROUND (FE_DEC_DOWNWARD);

  err = pthread_mutex_lock (&mut1);
  if (err != 0)
    error (EXIT_FAILURE, err, "downward: failed to lock");

  err = pthread_barrier_wait (&bar1);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("downward: barrier_wait failed");
      exit (1);
    } 

  err = pthread_cond_wait (&cond1, &mut1);
  if (err != 0)
    error (EXIT_FAILURE, err, "downward: failed to wait");

  err = pthread_mutex_unlock (&mut1);
  if (err != 0)
    error (EXIT_FAILURE, err, "downward: failed to unlock");

  if (!do_d64_to_d32 (1.1111125dd, 1.111112df)) FAILURE
  if (!do_d64_to_d32 (1.1111135dd, 1.111113df)) FAILURE
  if (!do_d64_to_d32 (-1.1111125dd, -1.111113df)) FAILURE
  if (!do_d64_to_d32 (-1.1111135dd, -1.111114df)) FAILURE
  if (!do_d128_to_d32 (1.1111125dl, 1.111112df)) FAILURE
  if (!do_d128_to_d32 (1.1111135dl, 1.111113df)) FAILURE
  if (!do_d128_to_d32 (-1.1111125dl, -1.111113df)) FAILURE
  if (!do_d128_to_d32 (-1.1111135dl, -1.111114df)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111125dl, 1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111135dl, 1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111125dl, -1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111135dl, -1.111111111111114dd)) FAILURE
  
#ifdef DBG
  if (failcnt)
    printf ("downward: %d fails\n", failcnt);
#endif
  return (void *) (ptrdiff_t) failcnt;
}

void *
tonearest (void *arg)
{
  int err;
  DFP_SETROUND (FE_DEC_TONEAREST);

  err = pthread_mutex_lock (&mut2);
  if (err != 0)
    error (EXIT_FAILURE, err, "tonearest: failed to lock");

  err = pthread_barrier_wait (&bar2);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("tonearest: barrier_wait failed");
      exit (1);
    } 

  err = pthread_cond_wait (&cond2, &mut2);
  if (err != 0)
    error (EXIT_FAILURE, err, "tonearest: failed to wait");

  err = pthread_mutex_unlock (&mut2);
  if (err != 0)
    error (EXIT_FAILURE, err, "tonearest: failed to unlock");

  if (!do_d64_to_d32 (1.1111125dd, 1.111112df)) FAILURE
  if (!do_d64_to_d32 (1.1111135dd, 1.111114df)) FAILURE
  if (!do_d64_to_d32 (-1.1111125dd, -1.111112df)) FAILURE
  if (!do_d64_to_d32 (-1.1111135dd, -1.111114df)) FAILURE
  if (!do_d128_to_d32 (1.1111125dl, 1.111112df)) FAILURE
  if (!do_d128_to_d32 (1.1111135dl, 1.111114df)) FAILURE
  if (!do_d128_to_d32 (-1.1111125dl, -1.111112df)) FAILURE
  if (!do_d128_to_d32 (-1.1111135dl, -1.111114df)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111125dl, 1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111135dl, 1.111111111111114dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111125dl, -1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111135dl, -1.111111111111114dd)) FAILURE
  
#ifdef DBG
  if (failcnt)
    printf ("tonearest: %d fails\n", failcnt);
#endif
  return (void *) (ptrdiff_t) failcnt;
} 

void *
toneareastfromzero (void *arg)
{
  int err;
  DFP_SETROUND (FE_DEC_TONEARESTFROMZERO);

  err = pthread_mutex_lock (&mut3);
  if (err != 0)
    error (EXIT_FAILURE, err, "toneareastfromzero: failed to lock");

  err = pthread_barrier_wait (&bar3);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("toneareastfromzero: barrier_wait failed");
      exit (1);
    } 

  err = pthread_cond_wait (&cond3, &mut3);
  if (err != 0)
    error (EXIT_FAILURE, err, "toneareastfromzero: failed to wait");

  err = pthread_mutex_unlock (&mut3);
  if (err != 0)
    error (EXIT_FAILURE, err, "toneareastfromzero: failed to unlock");

  if (!do_d64_to_d32 (1.1111125dd, 1.111113df)) FAILURE
  if (!do_d64_to_d32 (1.1111135dd, 1.111114df)) FAILURE
  if (!do_d64_to_d32 (-1.1111125dd, -1.111113df)) FAILURE
  if (!do_d64_to_d32 (-1.1111135dd, -1.111114df)) FAILURE
  if (!do_d128_to_d32 (1.1111125dl, 1.111113df)) FAILURE
  if (!do_d128_to_d32 (1.1111135dl, 1.111114df)) FAILURE
  if (!do_d128_to_d32 (-1.1111125dl, -1.111113df)) FAILURE
  if (!do_d128_to_d32 (-1.1111135dl, -1.111114df)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111125dl, 1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111135dl, 1.111111111111114dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111125dl, -1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111135dl, -1.111111111111114dd)) FAILURE
  
#ifdef DBG
  if (failcnt)
    printf ("toneareastfromzero: %d fails\n", failcnt);
#endif
  return (void *) (ptrdiff_t) failcnt;
} 
  
void *
towardzero (void *arg)
{
  int err;
  DFP_SETROUND (FE_DEC_TOWARDZERO);

  err = pthread_mutex_lock (&mut4);
  if (err != 0)
    error (EXIT_FAILURE, err, "towardzero: failed to lock");

  err = pthread_barrier_wait (&bar4);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("towardzero: barrier_wait failed");
      exit (1);
    } 

  err = pthread_cond_wait (&cond4, &mut4);
  if (err != 0)
    error (EXIT_FAILURE, err, "towardzero: failed to wait");

  err = pthread_mutex_unlock (&mut4);
  if (err != 0)
    error (EXIT_FAILURE, err, "towardzero: failed to unlock");

  if (!do_d64_to_d32 (1.1111125dd, 1.111112df)) FAILURE
  if (!do_d64_to_d32 (1.1111135dd, 1.111113df)) FAILURE
  if (!do_d64_to_d32 (-1.1111125dd, -1.111112df)) FAILURE
  if (!do_d64_to_d32 (-1.1111135dd, -1.111113df)) FAILURE
  if (!do_d128_to_d32 (1.1111125dl, 1.111112df)) FAILURE
  if (!do_d128_to_d32 (1.1111135dl, 1.111113df)) FAILURE
  if (!do_d128_to_d32 (-1.1111125dl, -1.111112df)) FAILURE
  if (!do_d128_to_d32 (-1.1111135dl, -1.111113df)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111125dl, 1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111135dl, 1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111125dl, -1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111135dl, -1.111111111111113dd)) FAILURE

#ifdef DBG
  if (failcnt)
    printf ("towardzero: %d fails\n", failcnt);
#endif
  return (void *) (ptrdiff_t) failcnt;
} 

void *
upward (void *arg)
{
  int err;
  DFP_SETROUND (FE_DEC_UPWARD);

  err = pthread_mutex_lock (&mut5);
  if (err != 0)
    error (EXIT_FAILURE, err, "upward: failed to lock");

  err = pthread_barrier_wait (&bar5);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("upward: barrier_wait failed");
      exit (1);
    } 

  err = pthread_cond_wait (&cond5, &mut5);
  if (err != 0)
    error (EXIT_FAILURE, err, "upward: failed to wait");

  err = pthread_mutex_unlock (&mut5);
  if (err != 0)
    error (EXIT_FAILURE, err, "upward: failed to unlock");

  if (!do_d64_to_d32 (1.1111125dd, 1.111113df)) FAILURE
  if (!do_d64_to_d32 (1.1111135dd, 1.111114df)) FAILURE
  if (!do_d64_to_d32 (-1.1111125dd, -1.111112df)) FAILURE
  if (!do_d64_to_d32 (-1.1111135dd, -1.111113df)) FAILURE
  if (!do_d128_to_d32 (1.1111125dl, 1.111113df)) FAILURE
  if (!do_d128_to_d32 (1.1111135dl, 1.111114df)) FAILURE
  if (!do_d128_to_d32 (-1.1111125dl, -1.111112df)) FAILURE
  if (!do_d128_to_d32 (-1.1111135dl, -1.111113df)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111125dl, 1.111111111111113dd)) FAILURE
  if (!do_d128_to_d64 (1.1111111111111135dl, 1.111111111111114dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111125dl, -1.111111111111112dd)) FAILURE
  if (!do_d128_to_d64 (-1.1111111111111135dl, -1.111111111111113dd)) FAILURE
  
#ifdef DBG
  if (failcnt)
    printf ("upward: %d fails\n", failcnt);
#endif
  return (void *) (ptrdiff_t) failcnt;
}

int
main (void)
{
  int err;
  int count = 0;
  void *ret;
  pthread_t down, up, tozero, fromzero, nearest;

  if (pthread_barrier_init (&bar1, NULL, 2) != 0
      || pthread_barrier_init (&bar2, NULL, 2) != 0
      || pthread_barrier_init (&bar3, NULL, 2) != 0
      || pthread_barrier_init (&bar4, NULL, 2) != 0
      || pthread_barrier_init (&bar5, NULL, 2) != 0)
    {
      puts ("parent: failed to init barrier");
      return 1;
    }

  if (pthread_create (&down, NULL, downward, NULL) != 0)
    {
      puts ("parent: failed to create");
      return 1;
    }

  if (pthread_create (&nearest, NULL, tonearest, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  if (pthread_create (&fromzero, NULL, toneareastfromzero, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  if (pthread_create (&up, NULL, upward, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  if (pthread_create (&tozero, NULL, towardzero, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  err = pthread_barrier_wait (&bar1);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent: failed to wait barrier 1");
      return 1;
    }
  err = pthread_barrier_wait (&bar2);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent: failed to wait barrier 2");
      return 1;
    }
  err = pthread_barrier_wait (&bar3);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent: failed to wait barrier 3");
      return 1;
    }
  err = pthread_barrier_wait (&bar4);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent: failed to wait barrier 4");
      return 1;
    }
  err = pthread_barrier_wait (&bar5);
  if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent: failed to wait barrier 5");
      return 1;
    }

  err = pthread_mutex_lock (&mut1);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: lock failed");
  err = pthread_mutex_lock (&mut2);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: lock failed");
  err = pthread_mutex_lock (&mut3);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: lock failed");
  err = pthread_mutex_lock (&mut4);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: lock failed");
  err = pthread_mutex_lock (&mut5);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: lock failed");

  err = pthread_cond_signal(&cond1);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: broadcast failed");
  err = pthread_cond_signal(&cond2);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: broadcast failed");
  err = pthread_cond_signal(&cond3);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: broadcast failed");
  err = pthread_cond_signal(&cond4);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: broadcast failed");
  err = pthread_cond_signal(&cond5);
  if (err != 0)
    error (EXIT_FAILURE, err, "parent: broadcast failed");

  err = pthread_mutex_unlock (&mut1);
  if (err != 0)
    {
      puts ("parent: failed to unlock");
      return 1;
    }
  err = pthread_mutex_unlock (&mut2);
  if (err != 0)
    {
      puts ("parent: failed to unlock");
      return 1;
    }
  err = pthread_mutex_unlock (&mut3);
  if (err != 0)
    {
      puts ("parent: failed to unlock");
      return 1;
    }
  err = pthread_mutex_unlock (&mut4);
  if (err != 0)
    {
      puts ("parent: failed to unlock");
      return 1;
    }
  err = pthread_mutex_unlock (&mut5);
  if (err != 0)
    {
      puts ("parent: failed to unlock");
      return 1;
    }

  if (pthread_join (down, &ret) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }
  count += (int) (ptrdiff_t) ret;

  if (pthread_join (up, &ret) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }
  count += (int) (ptrdiff_t) ret;

  if (pthread_join (tozero, &ret) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }
  count += (int) (ptrdiff_t) ret;

  if (pthread_join (fromzero, &ret) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }
  count += (int) (ptrdiff_t) ret;

  if (pthread_join (nearest, &ret) != 0)
    {
      puts ("pthread_join failed");
      return 1;
    }
  count += (int) (ptrdiff_t) ret;

  if (count)
    {
#ifdef DBG
      printf ("Total: %d fails\n", count);
#endif
      abort ();
    }

  return 0;
}