summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/dfp/convert-dfp.c
blob: 148dff60e3fe8409252c65a9706c968d5a570b95 (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
/* { dg-options "-std=gnu99 -O0" } */

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

   Test various conversions involving decimal floating types. */

#ifndef	__STDC_WANT_DEC_FP__
#define __STDC_WANT_DEC_FP__ 1
#endif

#include <float.h>

extern void abort (void);
static int failcnt;

/* 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

volatile _Decimal32 d32;
volatile _Decimal64 d64;
volatile _Decimal128 d128;

int
main ()
{
  /* Conversions to larger types.  */
  d32 = 123.4df;
  d64 = d32;
  if (d64 != 123.4dd)
    FAILURE
  d128 = d32;
  if (d128 != 123.4dl)
    FAILURE
  d64 = 345.678dd;
  d128 = d64;
  if (d128 != 345.678dl)
    FAILURE

  /* Conversions to smaller types for which the value fits.  */
  d64 = 3456.789dd;
  d32 = d64;
  if (d32 != 3456.789df)
    FAILURE
  d128 = 123.4567dl;
  d32 = d128;
  if (d32 != 123.4567df)
    FAILURE

  d128 = 1234567890.123456dl;
  d64 = d128;
  if (d64 != 1234567890.123456dd)
    FAILURE

  /* Test demotion to non-representable decimal floating type. */

  /* Assumes a default rounding mode of 'near'.  This uses the rules
     describe in the 27 July 2005 draft of IEEE 754r, which are much
     more clear that what's described in draft 5 of N1107.  */

  /* Rounds to what _Decimal32 can handle.  */
  d64 = 9.99999949E96DD;
  d32 = d64;
  if (d32 != DEC32_MAX)
    FAILURE

  /* Rounds to more than _Decimal32 can handle.  */
  d64 = 9.9999995E96DD;
  d32 = d64;
  if (d32 != __builtin_infd32())
    FAILURE

  /* Rounds to what _Decimal32 can handle.  */
  d128 = 9.99999949E96DD;
  d32 = d128;
  if (d32 != DEC32_MAX)
    FAILURE

  /* Rounds to more than _Decimal32 can handle.  */
  d128= 9.9999995E96DD;
  d32 = d128;
  if (d32 != __builtin_infd32())
    FAILURE

  /* Rounds to what _Decimal64 can handle.  */
  d128 = 9.99999999999999949E384DL;
  d64 = d128;
  if (d64 != DEC64_MAX)
    FAILURE

  /* Rounds to more than _Decimal64 can handle.  */
  d128 = 9.9999999999999995E384DL;
  d64 = d128;
  if (d64 != __builtin_infd64())
    FAILURE

  if (failcnt != 0)
    abort ();

  return 0;
}