1/*  Copyright (C) 2007 IBM
2
3    Author: Pete Eberlein  eberlein@us.ibm.com
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307, USA.
19
20    The GNU General Public License is contained in the file COPYING.
21*/
22
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <strings.h>
28
29#define POS_NORMAL   0x4000
30#define NEG_NORMAL   0x8000
31#define POS_INF      0x5000
32#define NEG_INF      0x9000
33#define POS_ZERO     0x2000
34#define NEG_ZERO     0x12000
35#define POS_DENORMAL 0x14000
36#define NEG_DENORMAL 0x18000
37#define NAN          0x11000
38#define FPRF_MASK    0x1F000
39
40
41int main(int argc, char *argv[])
42{
43
44   double inf, neg0, nan;
45   union {
46      double d;
47      struct {
48         unsigned int dummy, dummy2:15, fprf:17;
49      };
50   } fpscr;
51
52   inf = strtod("inf", NULL);
53   neg0 = strtod("-0", NULL);
54   nan = strtod("nan", NULL);
55
56
57   /* This set is disabled until fprf is implemented. */
58   if (0) {
59      double set[] = { inf, 1.5, 0, neg0, -1.5, -inf, nan };
60      int i, j, fprf;
61      for (i = 0; i < 7; ++i) {
62         for (j = 0; j < 7; ++j) {
63          asm("fcmpu 1, %1, %2\n\t" "mffs %0\n":"=f"(fpscr.d)
64          :    "f"(set[i]), "f"(set[j])
65                );
66
67            if (i == 6 || j == 6) {
68               fprf = 0x1000;   // Unordered
69            } else if (i == j || (i == 2 && j == 3) || (i == 3 && j == 2)) {
70               fprf = 0x2000;   // Equal
71            } else if (i < j) {
72               fprf = 0x4000;   // Greater Than
73            } else if (i > j) {
74               fprf = 0x8000;   // Less Than
75            }
76
77            printf("fcmpu\t%.1f\t%.1f\t%x\t%s\n", set[i], set[j],
78                   fpscr.fprf, fpscr.fprf == fprf ? "PASS" : "FAIL");
79         }
80      }
81   }
82
83   {
84      double set[] = { inf, 1.9, 1.1, 0, neg0, -1.1, -1.9, -inf, nan };
85      double frin[] = { inf, 2.0, 1.0, 0, neg0, -1.0, -2.0, -inf, nan };
86      double friz[] = { inf, 1.0, 1.0, 0, neg0, -1.0, -1.0, -inf, nan };
87      double frip[] = { inf, 2.0, 2.0, 0, neg0, -1.0, -1.0, -inf, nan };
88      double frim[] = { inf, 1.0, 1.0, 0, neg0, -2.0, -2.0, -inf, nan };
89      int fprf[] = { POS_INF, POS_NORMAL, POS_NORMAL, POS_ZERO, NEG_ZERO,
90         NEG_NORMAL, NEG_NORMAL, NEG_INF, NAN
91      };
92      double set2[] = { 0.9, 0.1, -0.1, -0.9, 1e-40, -1e-40 };
93      double frin2[] = { 1.0, 0.0, -0.0, -1.0, 0.0, -0.0 };
94      int frin2rf[] =
95          { POS_NORMAL, POS_ZERO, NEG_ZERO, NEG_NORMAL, POS_ZERO,
96 NEG_ZERO };
97      double friz2[] = { 0.0, 0.0, -0.0, -0.0, 0.0, -0.0 };
98      int friz2rf[] =
99          { POS_ZERO, POS_ZERO, NEG_ZERO, NEG_ZERO, POS_ZERO, NEG_ZERO };
100      double frip2[] = { 1.0, 1.0, -0.0, -0.0, 1.0, -0.0 };
101      int frip2rf[] =
102          { POS_NORMAL, POS_NORMAL, NEG_ZERO, NEG_ZERO, POS_NORMAL,
103 NEG_ZERO };
104      double frim2[] = { 0.0, 0.0, -1.0, -1.0, 0.0, -1.0 };
105      int frim2rf[] =
106          { POS_ZERO, POS_ZERO, NEG_NORMAL, NEG_NORMAL, POS_ZERO,
107 NEG_NORMAL };
108      double ret;
109      int i;
110
111#define DO_TEST(op,in,out,rf)  for (i=0; i<sizeof(in)/sizeof(double); ++i) { \
112    asm (#op" %0, %2\n\t" \
113       "mffs %1\n" \
114    : "=f" (ret), "=f" (fpscr.d) \
115    : "f" (in[i]) \
116    ); \
117    printf(#op"\t%g\t%g\t%x\t%s\n", in[i], ret, fpscr.fprf, \
118           (!bcmp(&ret, &out[i], sizeof(double))) /*&& (rf[i] == fpscr.fprf)*/ \
119	   ? "PASS" : "FAIL"); \
120  }
121      /* Note: fprf check above is disabled until fprf is implemented. */
122
123
124      DO_TEST(frin, set, frin, fprf);
125      DO_TEST(frin, set2, frin2, frin2rf);
126      DO_TEST(friz, set, friz, fprf);
127      DO_TEST(friz, set2, friz2, friz2rf);
128      DO_TEST(frip, set, frip, fprf);
129      DO_TEST(frip, set2, frip2, frip2rf);
130      DO_TEST(frim, set, frim, fprf);
131      DO_TEST(frim, set2, frim2, frim2rf);
132   }
133
134   /* This set is disabled until fprf is implemented. */
135   if (0) {
136      double set1[] = { inf, 0.9, 0.1, 0, neg0, -0.1, -0.9, -inf, nan };
137      double frsp1[] =
138          { inf, 0.9f, 0.1f, 0, neg0, -0.1f, -0.9f, -inf, nan };
139      int fprf1[] =
140          { POS_INF, POS_NORMAL, POS_NORMAL, POS_ZERO, NEG_ZERO,
141   NEG_NORMAL,
142         NEG_NORMAL, NEG_INF, NAN
143      };
144      double set2[] =
145          { 1.2e-38, 1.1e-38, 1e-40, 8e-44, 9e-44, 8e-46, 7e-46 };
146      double frsp2[] =
147          { 1.2e-38f, 1.1e-38f, 1e-40f, 8e-44f, 9e-44f, 8e-46f, 0.0 };
148      int fprf2[] = { POS_NORMAL, POS_DENORMAL, POS_DENORMAL, POS_DENORMAL,
149         POS_DENORMAL, POS_DENORMAL, POS_ZERO
150      };
151      double set3[] =
152          { -1.2e-38, -1.1e-38, -1e-40, -8e-44, -9e-44, -8e-46, -7e-46 };
153      double frsp3[] =
154          { -1.2e-38f, -1.1e-38f, -1e-40f, -8e-44f, -9e-44f, -8e-46f,
155-0.0 };
156      int fprf3[] = { NEG_NORMAL, NEG_DENORMAL, NEG_DENORMAL, NEG_DENORMAL,
157         NEG_DENORMAL, NEG_DENORMAL, NEG_ZERO
158      };
159      double ret;
160      int i;
161      DO_TEST(frsp, set1, frsp1, fprf1);
162      DO_TEST(frsp, set2, frsp2, fprf2);
163      DO_TEST(frsp, set3, frsp3, fprf3);
164   }
165
166
167   return 0;
168}
169