• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include "opcodes.h"
3 #include "dfp_utils.h"
4 
5 volatile _Decimal64 d64_1, d64_2, result_64;
6 volatile _Decimal128 d128_1, d128_2, result_128;
7 
8 #define DFP_BINOP(insn, op1, op2, type, round, cc)                      \
9   ({                                                                    \
10     register type d1 asm("f0") =  op1;                                  \
11     register type d2 asm("f1") =  op2;                                  \
12     /* d1 = d1 (op) d2    */                                            \
13     asm volatile(insn(1,round,0,0)                                      \
14                  "ipm %1\n\t"                                           \
15                  "srl %1,28\n\t"                                        \
16                  :"+f" (d1), "=d" (cc)                                  \
17                  :"f"(d2)                                               \
18                  );                                                     \
19     d1;                                                                 \
20   })
21 
main()22 int main() {
23   int cc;
24 
25   printf("Decimal floating point arithmetic\n");
26 
27     /* 64-bit ADD */
28   printf("64-bit ADD\n");
29   /* case 1: cc = 2 */
30   d64_1 = 3.14DD;
31   d64_2 = 0.005DD;
32   result_64 = DFP_BINOP(ADTRA, d64_1, d64_2, _Decimal64, 1, cc);
33   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "+", cc);
34   /* case 2: cc = 1 */
35   d64_1 = -3.14DD;
36   d64_2 = 0.005DD;
37   result_64 = DFP_BINOP(ADTRA, d64_1, d64_2, _Decimal64, 1, cc);
38   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "+", cc);
39   /* case 2: cc = 0 */
40   d64_1 = 3.14DD;
41   d64_2 = -d64_1;
42   result_64 = DFP_BINOP(ADTRA, d64_1, d64_2, _Decimal64, 3, cc);
43   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "+", cc);
44 
45 
46     /* 64-bit SUBTRACT */
47   printf("64-bit SUBTRACT\n");
48   /* case 1: cc = 2 */
49   d64_1 = 3.14DD;
50   d64_2 = 0.005DD;
51   result_64 = DFP_BINOP(SDTRA, d64_1, d64_2, _Decimal64, 4, cc);
52   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "-", cc);
53   /* case 2: cc = 1 */
54   d64_1 = -3.14DD;
55   d64_2 = 0.005DD;
56   result_64 = DFP_BINOP(SDTRA, d64_1, d64_2, _Decimal64, 5, cc);
57   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "-", cc);
58   /* case 3: cc = 0 */
59   d64_1 = 3.14DD;
60   d64_2 = d64_1;
61   result_64 = DFP_BINOP(SDTRA, d64_1, d64_2, _Decimal64, 5, cc);
62   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "-", cc);
63 
64     /* 64-bit MULTIPLY */
65   printf("64-bit MULTIPLY\n");
66   /* case 1: cc = 2 */
67   d64_1 = 3.14DD;
68   d64_2 = 7.DD;
69   result_64 = DFP_BINOP(MDTRA, d64_1, d64_2, _Decimal64, 6, cc);
70   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "*", cc);
71   /* case 2: cc = 1 */
72   d64_1 = -3.14DD;
73   d64_2 = 7.DD;
74   result_64 = DFP_BINOP(MDTRA, d64_1, d64_2, _Decimal64, 7, cc);
75   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "*", cc);
76   /* case 3: cc = 0 */
77   d64_1 = -3.14DD;
78   d64_2 = 0.DD;
79   result_64 = DFP_BINOP(MDTRA, d64_1, d64_2, _Decimal64, 7, cc);
80   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "*", cc);
81 
82     /* 64-bit DIVIDE */
83   printf("64-bit DIVIDE\n");
84   /* case 1: cc = 2 */
85   d64_1 = 22.DD;
86   d64_2 = 7.DD;
87   result_64 = DFP_BINOP(DDTRA, d64_1, d64_2, _Decimal64, d, cc);
88   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "/", cc);
89   /* case 2: cc = 1 */
90   d64_1 = -22.DD;
91   d64_2 = 7.DD;
92   result_64 = DFP_BINOP(DDTRA, d64_1, d64_2, _Decimal64, e, cc);
93   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "/", cc);
94   /* case 3: cc = 0 */
95   d64_1 = 0.DD;
96   d64_2 = 7.DD;
97   result_64 = DFP_BINOP(DDTRA, d64_1, d64_2, _Decimal64, e, cc);
98   DFP_BINOP_PRINT(d64_1, d64_2, result_64, _Decimal64, "/", cc);
99 
100     /* 128-bit ADD */
101   printf("128-bit ADD\n");
102   /* case 1: cc = 2 */
103   d128_1 = 3.14DL;
104   d128_2 = 0.005DL;
105   result_128 = DFP_BINOP(AXTRA, d128_1, d128_2, _Decimal128, 1, cc);
106   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "+", cc);
107   /* case 2: cc = 1 */
108   d128_1 = -3.14DL;
109   d128_2 = 0.005DL;
110   result_128 = DFP_BINOP(AXTRA, d128_1, d128_2, _Decimal128, 1, cc);
111   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "+", cc);
112   /* case 3: cc = 0 */
113   d128_1 = 3.14DL;
114   d128_2 = -d128_1;
115   result_128 = DFP_BINOP(AXTRA, d128_1, d128_2, _Decimal128, 3, cc);
116   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "+", cc);
117 
118     /* 128-bit SUBTRACT */
119   printf("128-bit SUBTRACT\n");
120   /* case 1: cc = 2 */
121   d128_1 = 3.14DL;
122   d128_2 = 0.005DL;
123   result_128 = DFP_BINOP(SXTRA, d128_1, d128_2, _Decimal128, 4, cc);
124   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "-", cc);
125   /* case 2: cc = 1 */
126   d128_1 = -3.14DL;
127   d128_2 = 0.005DL;
128   result_128 = DFP_BINOP(SXTRA, d128_1, d128_2, _Decimal128, 5, cc);
129   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "-", cc);
130   /* case 3: cc = 0 */
131   d128_1 = 3.14DL;
132   d128_2 = d128_1;
133   result_128 = DFP_BINOP(SXTRA, d128_1, d128_2, _Decimal128, 5, cc);
134   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "-", cc);
135 
136     /* 128-bit MULTIPLY */
137   printf("128-bit MULTIPLY\n");
138   /* case 1: cc = 2 */
139   d128_1 = 3.14DL;
140   d128_2 = 7.DL;
141   result_128 = DFP_BINOP(MXTRA, d128_1, d128_2, _Decimal128, 6, cc);
142   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "*", cc);
143   /* case 2: cc = 1 */
144   d128_1 = -3.14DL;
145   d128_2 = 7.DL;
146   result_128 = DFP_BINOP(MXTRA, d128_1, d128_2, _Decimal128, 7, cc);
147   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "*", cc);
148   /* case 3: cc = 0 */
149   d128_1 = 3.14DL;
150   d128_2 = 0.DL;
151   result_128 = DFP_BINOP(MXTRA, d128_1, d128_2, _Decimal128, 7, cc);
152   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "*", cc);
153 
154     /* 128-bit DIVIDE */
155   printf("128-bit DIVIDE\n");
156   /* case 1: cc = 2 */
157   d128_1 = 22.DL;
158   d128_2 = 7.DL;
159   result_128 = DFP_BINOP(DXTRA, d128_1, d128_2, _Decimal128, d, cc);
160   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "/", cc);
161   /* case 2: cc = 1 */
162   d128_1 = -22.DL;
163   d128_2 = 7.DL;
164   result_128 = DFP_BINOP(DXTRA, d128_1, d128_2, _Decimal128, e, cc);
165   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "/", cc);
166   /* case 3: cc = 0 */
167   d128_1 = 0.DL;
168   d128_2 = 7.DL;
169   result_128 = DFP_BINOP(DXTRA, d128_1, d128_2, _Decimal128, e, cc);
170   DFP_BINOP_PRINT(d128_1, d128_2, result_128, _Decimal128, "/", cc);
171 
172   return 0;
173 }
174