1 // Copyright (C) 2009-2013 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 
18 // { dg-require-effective-target dfp }
19 
20 // ISO/IEC TR 24733  3.2.5  Initialization from coefficient and exponent.
21 
22 #include <decimal/decimal>
23 #include <cstring>
24 #include <testsuite_hooks.h>
25 
26 #define PASTE2(A,B) A ## B
27 #define PASTE(A,B) PASTE2(A,B)
28 
29 #define TESTVAL_NEG(COEFF,ESIGN,EXP,SUF,NUM,SIZE)			\
30   x = PASTE(PASTE(PASTE(PASTE(PASTE(COEFF,.),E),ESIGN),EXP),SUF);	\
31   sll = PASTE(COEFF,LL);						\
32   i = ESIGN EXP;							\
33   a = PASTE(make_decimal,32) (sll, i);					\
34   b = PASTE(make_decimal,32) (PASTE(COEFF,LL), ESIGN EXP);	\
35   VERIFY ((std::memcmp ((void *)&x, (void *)&a, SIZE) == 0)	\
36           && (std::memcmp ((void *)&x, (void *)&b,SIZE) == 0));
37 
38 #define TESTVAL_NONNEG(COEFF,ESIGN,EXP,SUF,NUM,SIZE)			\
39   x = PASTE(PASTE(PASTE(PASTE(PASTE(COEFF,.),E),ESIGN),EXP),SUF);	\
40   sll = PASTE(COEFF,LL);						\
41   ull = PASTE(COEFF,ULL);						\
42   i = ESIGN EXP;							\
43   a = PASTE(make_decimal,32) (sll, i);					\
44   b = PASTE(make_decimal,32) (PASTE(COEFF,LL), ESIGN EXP);		\
45   c = PASTE(make_decimal,32) (ull, i);					\
46   d = PASTE(make_decimal,32) (PASTE(COEFF,ULL), ESIGN EXP);		\
47   VERIFY ((std::memcmp ((void *)&x, (void *)&a, SIZE) == 0)	\
48           && (std::memcmp ((void *)&x, (void *)&b,SIZE) == 0)	\
49           && (std::memcmp ((void *)&x, (void *)&c,SIZE) == 0)	\
50           && (std::memcmp ((void *)&x, (void *)&d,SIZE) == 0));
51 
52 using namespace std::decimal;
53 
54 void
make_decimal_32(void)55 make_decimal_32 (void)
56 {
57   bool test __attribute__((unused)) = true;
58   decimal32 a, b, c, d;
59   float x __attribute__((mode(SD)));
60   int i;
61   unsigned long sz = sizeof (decimal32);
62   volatile long long sll;
63   volatile unsigned long long ull;
64 
65   TESTVAL_NONNEG (0, +, 0, DF, 32, sz);
66   TESTVAL_NONNEG (5, +, 1, DF, 32, sz);
67   TESTVAL_NONNEG (50, +, 0, DF, 32, sz);
68   TESTVAL_NONNEG (500, +, 0, DF, 32, sz);
69   TESTVAL_NEG (-25, -, 3, DF, 32, sz)
70   TESTVAL_NEG (-500, +, 0, DF, 32, sz);
71   TESTVAL_NONNEG (999999, +, 91, DF, 32, sz);
72   TESTVAL_NONNEG (1, -, 9, DF, 32, sz);
73   TESTVAL_NONNEG (1, -, 90, DF, 32, sz);
74   TESTVAL_NONNEG (1, -, 95, DF, 32, sz);
75   TESTVAL_NONNEG (1, -, 101, DF, 32, sz);
76   TESTVAL_NEG (-1, -, 101, DF, 32, sz);
77 }
78 
79 void
make_decimal_64(void)80 make_decimal_64 (void)
81 {
82   bool test __attribute__((unused)) = true;
83   decimal64 a, b, c, d;
84   float x __attribute__((mode(DD)));
85   int i;
86   unsigned long sz = sizeof (decimal64);
87   volatile long long sll;
88   volatile unsigned long long ull;
89 
90   TESTVAL_NONNEG (0, +, 0, DF, 64, sz);
91   TESTVAL_NONNEG (5, +, 1, DF, 64, sz);
92   TESTVAL_NONNEG (50, +, 0, DF, 64, sz);
93   TESTVAL_NONNEG (500, +, 0, DF, 64, sz);
94   TESTVAL_NEG (-25, -, 3, DF, 64, sz)
95   TESTVAL_NEG (-500, +, 0, DF, 64, sz);
96   TESTVAL_NONNEG (999999, +, 91, DF, 64, sz);
97   TESTVAL_NONNEG (1, -, 9, DF, 64, sz);
98   TESTVAL_NONNEG (1, -, 90, DF, 64, sz);
99   TESTVAL_NONNEG (1, -, 95, DF, 64, sz);
100   TESTVAL_NONNEG (1, -, 101, DF, 64, sz);
101   TESTVAL_NEG (-1, -, 101, DF, 64, sz);
102 }
103 
104 void
make_decimal_128(void)105 make_decimal_128 (void)
106 {
107   bool test __attribute__((unused)) = true;
108   decimal128 a, b, c, d;
109   float x __attribute__((mode(TD)));
110   int i;
111   unsigned long sz = sizeof (decimal128);
112   volatile long long sll;
113   volatile unsigned long long ull;
114 
115   TESTVAL_NONNEG (0, +, 0, DF, 128, sz);
116   TESTVAL_NONNEG (5, +, 1, DF, 128, sz);
117   TESTVAL_NONNEG (50, +, 0, DF, 128, sz);
118   TESTVAL_NONNEG (500, +, 0, DF, 128, sz);
119   TESTVAL_NEG (-25, -, 3, DF, 128, sz)
120   TESTVAL_NEG (-500, +, 0, DF, 128, sz);
121   TESTVAL_NONNEG (999999, +, 91, DF, 128, sz);
122   TESTVAL_NONNEG (1, -, 9, DF, 128, sz);
123   TESTVAL_NONNEG (1, -, 90, DF, 128, sz);
124   TESTVAL_NONNEG (1, -, 95, DF, 128, sz);
125   TESTVAL_NONNEG (1, -, 101, DF, 128, sz);
126   TESTVAL_NEG (-1, -, 101, DF, 128, sz);
127 }
128 
129 int
main()130 main ()
131 {
132   make_decimal_32 ();
133   make_decimal_64 ();
134   make_decimal_128 ();
135 }
136