source: mainline/uspace/app/tester/float/softfloat1.c@ 9727b92

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9727b92 was 000494d, checked in by Jakub Jermar <jakub@…>, 13 years ago

Add double tests to softfloat1.
Avoid code duplication.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2012 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <sftypes.h>
32#include <add.h>
33#include <sub.h>
34#include <mul.h>
35#include <div.h>
36#include <bool.h>
37#include "../tester.h"
38
39#define OPERANDS 6
40#define PRECISION 10000
41
42#define PRIdCMPTYPE PRId32
43
44typedef int32_t cmptype_t;
45typedef void (* float_op_t)(float, float, float *, float_t *);
46typedef void (* double_op_t)(double, double, double *, double_t *);
47typedef void (* template_t)(void *, unsigned, unsigned, cmptype_t *,
48 cmptype_t *);
49
50static float fop_a[OPERANDS] =
51 {3.5, -2.1, 100.0, 50.0, -1024.0, 0.0};
52
53static float fop_b[OPERANDS] =
54 {-2.1, 100.0, 50.0, -1024.0, 3.5, 0.0};
55
56static double dop_a[OPERANDS] =
57 {3.5, -2.1, 100.0, 50.0, -1024.0, 0.0};
58
59static double dop_b[OPERANDS] =
60 {-2.1, 100.0, 50.0, -1024.0, 3.5, 0.0};
61
62static cmptype_t cmpabs(cmptype_t a)
63{
64 if (a >= 0)
65 return a;
66
67 return -a;
68}
69
70static void
71float_template(void *f, unsigned i, unsigned j, cmptype_t *pic,
72 cmptype_t *pisc)
73{
74 float c;
75 float_t sc;
76
77 float_op_t op = (float_op_t) f;
78
79 op(fop_a[i], fop_b[j], &c, &sc);
80
81 *pic = (cmptype_t) (c * PRECISION);
82 *pisc = (cmptype_t) (sc.val * PRECISION);
83}
84
85static void
86double_template(void *f, unsigned i, unsigned j, cmptype_t *pic,
87 cmptype_t *pisc)
88{
89 double c;
90 double_t sc;
91
92 double_op_t op = (double_op_t) f;
93
94 op(dop_a[i], dop_b[j], &c, &sc);
95
96 *pic = (cmptype_t) (c * PRECISION);
97 *pisc = (cmptype_t) (sc.val * PRECISION);
98}
99
100static bool test_template(template_t template, void *f)
101{
102 bool correct = true;
103
104 for (unsigned int i = 0; i < OPERANDS; i++) {
105 for (unsigned int j = 0; j < OPERANDS; j++) {
106 cmptype_t ic;
107 cmptype_t isc;
108
109 template(f, i, j, &ic, &isc);
110 cmptype_t diff = cmpabs(ic - isc);
111
112 if (diff != 0) {
113 TPRINTF("i=%u, j=%u diff=%" PRIdCMPTYPE "\n",
114 i, j, diff);
115 correct = false;
116 }
117 }
118 }
119
120 return correct;
121}
122
123static void float_add_operator(float a, float b, float *pc, float_t *psc)
124{
125 *pc = a + b;
126
127 float_t sa;
128 float_t sb;
129
130 sa.val = a;
131 sb.val = b;
132 if (sa.data.parts.sign == sb.data.parts.sign)
133 psc->data = add_float(sa.data, sb.data);
134 else if (sa.data.parts.sign) {
135 sa.data.parts.sign = 0;
136 psc->data = sub_float(sb.data, sa.data);
137 } else {
138 sb.data.parts.sign = 0;
139 psc->data = sub_float(sa.data, sb.data);
140 }
141}
142
143static void float_mul_operator(float a, float b, float *pc, float_t *psc)
144{
145 *pc = a * b;
146
147 float_t sa;
148 float_t sb;
149
150 sa.val = a;
151 sb.val = b;
152 psc->data = mul_float(sa.data, sb.data);
153}
154
155static void float_div_operator(float a, float b, float *pc, float_t *psc)
156{
157 if ((cmptype_t) b == 0) {
158 *pc = 0.0;
159 psc->val = 0.0;
160 return;
161 }
162
163 *pc = a / b;
164
165 float_t sa;
166 float_t sb;
167
168 sa.val = a;
169 sb.val = b;
170 psc->data = div_float(sa.data, sb.data);
171}
172
173static void double_add_operator(double a, double b, double *pc, double_t *psc)
174{
175 *pc = a + b;
176
177 double_t sa;
178 double_t sb;
179
180 sa.val = a;
181 sb.val = b;
182 if (sa.data.parts.sign == sb.data.parts.sign)
183 psc->data = add_double(sa.data, sb.data);
184 else if (sa.data.parts.sign) {
185 sa.data.parts.sign = 0;
186 psc->data = sub_double(sb.data, sa.data);
187 } else {
188 sb.data.parts.sign = 0;
189 psc->data = sub_double(sa.data, sb.data);
190 }
191}
192
193static void double_mul_operator(double a, double b, double *pc, double_t *psc)
194{
195 *pc = a * b;
196
197 double_t sa;
198 double_t sb;
199
200 sa.val = a;
201 sb.val = b;
202 psc->data = mul_double(sa.data, sb.data);
203}
204
205static void double_div_operator(double a, double b, double *pc, double_t *psc)
206{
207 if ((cmptype_t) b == 0) {
208 *pc = 0.0;
209 psc->val = 0.0;
210 return;
211 }
212
213 *pc = a / b;
214
215 double_t sa;
216 double_t sb;
217
218 sa.val = a;
219 sb.val = b;
220 psc->data = div_double(sa.data, sb.data);
221}
222
223const char *test_softfloat1(void)
224{
225 const char *err = NULL;
226
227 if (!test_template(float_template, float_add_operator)) {
228 err = "Float addition failed";
229 TPRINTF("%s\n", err);
230 }
231 if (!test_template(float_template, float_mul_operator)) {
232 err = "Float multiplication failed";
233 TPRINTF("%s\n", err);
234 }
235 if (!test_template(float_template, float_div_operator)) {
236 err = "Float division failed";
237 TPRINTF("%s\n", err);
238 }
239 if (!test_template(double_template, double_add_operator)) {
240 err = "Double addition failed";
241 TPRINTF("%s\n", err);
242 }
243 if (!test_template(double_template, double_mul_operator)) {
244 err = "Double multiplication failed";
245 TPRINTF("%s\n", err);
246 }
247 if (!test_template(double_template, double_div_operator)) {
248 err = "Double division failed";
249 TPRINTF("%s\n", err);
250 }
251
252 return err;
253}
254
Note: See TracBrowser for help on using the repository browser.