1#include <stdio.h>
2#include <sys/types.h>
3#include "opcodes.h"
4#include "dfp_utils.h"
5
6/* Following macros adopted from dfp/math.h from libdfp */
7#define DEC_INFINITY    __builtin_infd64()
8#define DEC_NAN         (0.0DF * DEC_INFINITY)
9
10/* Test DFP value and exponent comparison for  64/128-bit. */
11
12#define CMP_DFP(insn, op1, op2, type, cc)                               \
13  ({                                                                    \
14  register type d1 asm("f0") =  op1;                                    \
15  register type d2 asm("f1") =  op2;                                    \
16  /* cc = d1 (cmp) d2    */                                             \
17  asm volatile(insn(0,1)                                                \
18               "ipm %0\n\t"                                             \
19               "srl %0,28\n\t"                                          \
20               :"=d" (cc)                                               \
21               :"f"(d1), "f"(d2)                                        \
22               );                                                       \
23  cc;                                                                   \
24})
25
26
27#define COMPARE(insn, v1, v2, type)                                     \
28  {                                                                     \
29    int cc;                                                             \
30    CMP_DFP(insn, v1, v2, type, cc);                                    \
31    DFP_VAL_PRINT(v1, type);                                            \
32    switch (cc) {                                                       \
33    case 0:                                                             \
34      printf(" == ");                                                   \
35      break;                                                            \
36    case 1:                                                             \
37      printf(" < ");                                                    \
38      break;                                                            \
39    case 2:                                                             \
40      printf(" > ");                                                    \
41      break;                                                            \
42    case 3:                                                             \
43      printf(" <> ");                                                   \
44      break;                                                            \
45    }                                                                   \
46    DFP_VAL_PRINT(v2, type);                                            \
47    printf(" (cc == %d)\n", cc);                                        \
48}
49
50int main(void)
51{
52   _Decimal64 d64_1, d64_2;
53   _Decimal128 d128_1, d128_2;
54
55   /* compare 8 bytes DFP value */
56   printf("cdtr:\n");
57   d64_1 = 5.000005DD;
58   d64_2 = 50000000000000000.000005DD;
59   COMPARE(CDTR, d64_1, d64_1, _Decimal64);
60   COMPARE(CDTR, d64_1, d64_2, _Decimal64);
61   COMPARE(CDTR, d64_2, d64_1, _Decimal64);
62
63   /* compare NAN and INF operands */
64   d64_1 = DEC_INFINITY;
65   d64_2 = DEC_NAN;
66   COMPARE(CDTR, d64_1, d64_2, _Decimal64);
67   COMPARE(CDTR, d64_1, d64_1, _Decimal64);
68   COMPARE(CDTR, d64_2, d64_2, _Decimal64);
69
70   /* compare 16 bytes DFP value */
71   printf("cxtr:\n");
72   d128_1 = 5.00005DL;
73   d128_2 = 5000000000000000.5DL;
74   COMPARE(CXTR, d128_1, d128_1, _Decimal128);
75   COMPARE(CXTR, d128_1, d128_2, _Decimal128);
76   COMPARE(CXTR, d128_2, d128_1, _Decimal128);
77
78   /* compare NAN and INF operands */
79   d128_1 = DEC_INFINITY;
80   d128_2 = DEC_NAN;
81   COMPARE(CXTR, d128_1, d128_2, _Decimal128);
82   COMPARE(CXTR, d128_1, d128_1, _Decimal128);
83   COMPARE(CXTR, d128_2, d128_2, _Decimal128);
84
85   /* compare exponents of 8 bytes DFP value */
86   printf("cedtr:\n");
87   d64_1 = 5.000005DD;
88   d64_2 = 50000000000000000.000005DD;
89   COMPARE(CEDTR, d64_1, d64_1, _Decimal64);
90   COMPARE(CEDTR, d64_1, d64_2, _Decimal64);
91   COMPARE(CEDTR, d64_2, d64_1, _Decimal64);
92
93   /* compare NAN and INF operands */
94   d64_1 = DEC_INFINITY;
95   d64_2 = DEC_NAN;
96   COMPARE(CEDTR, d64_1, d64_2, _Decimal64);
97   COMPARE(CEDTR, d64_1, d64_1, _Decimal64);
98   COMPARE(CEDTR, d64_2, d64_2, _Decimal64);
99
100   /* compare exponents of 16 bytes DFP value */
101   printf("cextr:\n");
102   d128_1 = 5.00005DL;
103   d128_2 = 5000000000000000.5DL;
104   COMPARE(CEXTR, d128_1, d128_1, _Decimal128);
105   COMPARE(CEXTR, d128_1, d128_2, _Decimal128);
106   COMPARE(CEXTR, d128_2, d128_1, _Decimal128);
107
108   /* compare NAN and INF operands */
109   d128_1 = DEC_INFINITY;
110   d128_2 = DEC_NAN;
111   COMPARE(CEXTR, d128_1, d128_2, _Decimal128);
112   COMPARE(CEXTR, d128_1, d128_1, _Decimal128);
113   COMPARE(CEXTR, d128_2, d128_2, _Decimal128);
114
115   return 0;
116}
117