Changes in uspace/app/tester/float/softfloat1.c [000494d:7218fe6] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/tester/float/softfloat1.c
r000494d r7218fe6 34 34 #include <mul.h> 35 35 #include <div.h> 36 #include <bool.h> 36 #include <comparison.h> 37 #include <conversion.h> 38 #include <stdbool.h> 37 39 #include "../tester.h" 38 40 39 #define OPERANDS 641 #define OPERANDS 10 40 42 #define PRECISION 10000 41 43 … … 43 45 44 46 typedef int32_t cmptype_t; 45 typedef void (* float_op_t)(float, float, float *, float_t *); 46 typedef void (* double_op_t)(double, double, double *, double_t *); 47 typedef void (* template_t)(void *, unsigned, unsigned, cmptype_t *, 47 48 typedef void (* uint_to_double_op_t)(unsigned int, double *, double_t *); 49 typedef void (* double_to_uint_op_t)(double, unsigned int *, unsigned int *); 50 typedef void (* float_binary_op_t)(float, float, float *, float_t *); 51 typedef void (* double_binary_op_t)(double, double, double *, double_t *); 52 typedef void (* double_cmp_op_t)(double, double, cmptype_t *, cmptype_t *); 53 54 typedef void (* template_unary_t)(void *, unsigned, cmptype_t *, cmptype_t *); 55 typedef void (* template_binary_t)(void *, unsigned, unsigned, cmptype_t *, 48 56 cmptype_t *); 49 57 50 static float fop_a[OPERANDS] = 51 {3.5, -2.1, 100.0, 50.0, -1024.0, 0.0}; 52 53 static float fop_b[OPERANDS] = 54 {-2.1, 100.0, 50.0, -1024.0, 3.5, 0.0}; 55 56 static double dop_a[OPERANDS] = 57 {3.5, -2.1, 100.0, 50.0, -1024.0, 0.0}; 58 59 static double dop_b[OPERANDS] = 60 {-2.1, 100.0, 50.0, -1024.0, 3.5, 0.0}; 58 #define NUMBERS \ 59 3.5, -2.1, 100.0, 50.0, -1024.0, 0.0, 768.3156, 1080.499999, -600.0, 1.0 60 61 static float fop_a[OPERANDS] = { 62 NUMBERS 63 }; 64 65 static double dop_a[OPERANDS] = { 66 NUMBERS 67 }; 68 69 static unsigned int uop_a[OPERANDS] = { 70 4, -100, 100, 50, 1024, 0, 1000000, -1U, 0x80000000U, 500 71 }; 61 72 62 73 static cmptype_t cmpabs(cmptype_t a) … … 68 79 } 69 80 70 static void 71 float_template(void *f, unsigned i, unsigned j, cmptype_t *pic, 81 static int dcmp(double a, double b) 82 { 83 if (a < b) 84 return -1; 85 86 if (a > b) 87 return 1; 88 89 return 0; 90 } 91 92 static void uint_to_double_template(void *f, unsigned i, cmptype_t *pic, 72 93 cmptype_t *pisc) 73 94 { 95 uint_to_double_op_t op = (uint_to_double_op_t) f; 96 97 double c; 98 double_t sc; 99 op(uop_a[i], &c, &sc); 100 101 *pic = (cmptype_t) (c * PRECISION); 102 *pisc = (cmptype_t) (sc.val * PRECISION); 103 } 104 105 static void double_to_uint_template(void *f, unsigned i, cmptype_t *pic, 106 cmptype_t *pisc) 107 { 108 double_to_uint_op_t op = (double_to_uint_op_t) f; 109 110 unsigned int c; 111 unsigned int sc; 112 op(dop_a[i], &c, &sc); 113 114 *pic = (cmptype_t) c; 115 *pisc = (cmptype_t) sc; 116 } 117 118 119 static void float_template_binary(void *f, unsigned i, unsigned j, 120 cmptype_t *pic, cmptype_t *pisc) 121 { 122 float_binary_op_t op = (float_binary_op_t) f; 123 74 124 float c; 75 125 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 126 op(fop_a[i], fop_a[j], &c, &sc); 127 81 128 *pic = (cmptype_t) (c * PRECISION); 82 129 *pisc = (cmptype_t) (sc.val * PRECISION); 83 130 } 84 131 85 static void 86 double_template(void *f, unsigned i, unsigned j, cmptype_t *pic, 87 cmptype_t *pisc) 88 { 132 static void double_template_binary(void *f, unsigned i, unsigned j, 133 cmptype_t *pic, cmptype_t *pisc) 134 { 135 double_binary_op_t op = (double_binary_op_t) f; 136 89 137 double c; 90 138 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 139 op(dop_a[i], dop_a[j], &c, &sc); 140 96 141 *pic = (cmptype_t) (c * PRECISION); 97 142 *pisc = (cmptype_t) (sc.val * PRECISION); 98 143 } 99 144 100 static bool test_template(template_t template, void *f) 145 static void double_compare_template(void *f, unsigned i, unsigned j, 146 cmptype_t *pis, cmptype_t *piss) 147 { 148 double_cmp_op_t op = (double_cmp_op_t) f; 149 150 op(dop_a[i], dop_a[j], pis, piss); 151 } 152 153 static bool test_template_unary(template_unary_t template, void *f) 101 154 { 102 155 bool correct = true; 103 156 104 157 for (unsigned int i = 0; i < OPERANDS; i++) { 105 for (unsigned int j = 0; j < OPERANDS; j++) { 158 cmptype_t ic; 159 cmptype_t isc; 160 161 template(f, i, &ic, &isc); 162 cmptype_t diff = cmpabs(ic - isc); 163 164 if (diff != 0) { 165 TPRINTF("i=%u diff=%" PRIdCMPTYPE "\n", i, diff); 166 correct = false; 167 } 168 } 169 170 return correct; 171 } 172 173 static bool test_template_binary(template_binary_t template, void *f) 174 { 175 bool correct = true; 176 177 for (unsigned int i = 0; i < OPERANDS; i++) { 178 for (unsigned int j = 0; j < OPERANDS; j++) { 106 179 cmptype_t ic; 107 180 cmptype_t isc; 108 109 template(f, i, j, &ic, &isc); 181 182 template(f, i, j, &ic, &isc); 110 183 cmptype_t diff = cmpabs(ic - isc); 111 184 … … 121 194 } 122 195 196 static void uint_to_double_operator(unsigned int a, double *pc, double_t *psc) 197 { 198 *pc = (double) a; 199 psc->data = uint_to_double(a); 200 } 201 202 static void double_to_uint_operator(double a, unsigned int *pc, 203 unsigned int *psc) 204 { 205 double_t sa; 206 207 sa.val = a; 208 209 *pc = (unsigned int) a; 210 *psc = double_to_uint(sa.data); 211 } 212 213 static void double_to_int_operator(double a, unsigned int *pc, 214 unsigned int *psc) 215 { 216 double_t sa; 217 218 sa.val = a; 219 220 *pc = (int) a; 221 *psc = double_to_int(sa.data); 222 } 223 123 224 static void float_add_operator(float a, float b, float *pc, float_t *psc) 124 225 { … … 130 231 sa.val = a; 131 232 sb.val = b; 132 if (sa.data.parts.sign == sb.data.parts.sign) 233 234 if (sa.data.parts.sign == sb.data.parts.sign) { 133 235 psc->data = add_float(sa.data, sb.data); 134 else if (sa.data.parts.sign) {236 } else if (sa.data.parts.sign) { 135 237 sa.data.parts.sign = 0; 136 238 psc->data = sub_float(sb.data, sa.data); … … 160 262 return; 161 263 } 162 264 163 265 *pc = a / b; 164 266 … … 180 282 sa.val = a; 181 283 sb.val = b; 182 if (sa.data.parts.sign == sb.data.parts.sign) 284 285 if (sa.data.parts.sign == sb.data.parts.sign) { 183 286 psc->data = add_double(sa.data, sb.data); 184 else if (sa.data.parts.sign) {287 } else if (sa.data.parts.sign) { 185 288 sa.data.parts.sign = 0; 186 289 psc->data = sub_double(sb.data, sa.data); … … 210 313 return; 211 314 } 212 315 213 316 *pc = a / b; 214 317 … … 221 324 } 222 325 326 static void double_cmp_operator(double a, double b, cmptype_t *pis, 327 cmptype_t *piss) 328 { 329 *pis = dcmp(a, b); 330 331 double_t sa; 332 double_t sb; 333 334 sa.val = a; 335 sb.val = b; 336 337 if (is_double_lt(sa.data, sb.data)) 338 *piss = -1; 339 else if (is_double_gt(sa.data, sb.data)) 340 *piss = 1; 341 else if (is_double_eq(sa.data, sb.data)) 342 *piss = 0; 343 else 344 *piss = 42; 345 } 346 223 347 const char *test_softfloat1(void) 224 348 { 225 349 const char *err = NULL; 226 227 if (!test_template (float_template, float_add_operator)) {350 351 if (!test_template_binary(float_template_binary, float_add_operator)) { 228 352 err = "Float addition failed"; 229 353 TPRINTF("%s\n", err); 230 354 } 231 if (!test_template(float_template, float_mul_operator)) { 355 356 if (!test_template_binary(float_template_binary, float_mul_operator)) { 232 357 err = "Float multiplication failed"; 233 358 TPRINTF("%s\n", err); 234 359 } 235 if (!test_template(float_template, float_div_operator)) { 360 361 if (!test_template_binary(float_template_binary, float_div_operator)) { 236 362 err = "Float division failed"; 237 363 TPRINTF("%s\n", err); 238 364 } 239 if (!test_template(double_template, double_add_operator)) { 365 366 if (!test_template_binary(double_template_binary, double_add_operator)) { 240 367 err = "Double addition failed"; 241 368 TPRINTF("%s\n", err); 242 369 } 243 if (!test_template(double_template, double_mul_operator)) { 370 371 if (!test_template_binary(double_template_binary, double_mul_operator)) { 244 372 err = "Double multiplication failed"; 245 373 TPRINTF("%s\n", err); 246 374 } 247 if (!test_template(double_template, double_div_operator)) { 375 376 if (!test_template_binary(double_template_binary, double_div_operator)) { 248 377 err = "Double division failed"; 249 378 TPRINTF("%s\n", err); 250 379 } 251 380 381 if (!test_template_binary(double_compare_template, double_cmp_operator)) { 382 err = "Double comparison failed"; 383 TPRINTF("%s\n", err); 384 } 385 386 if (!test_template_unary(uint_to_double_template, 387 uint_to_double_operator)) { 388 err = "Conversion from unsigned int to double failed"; 389 TPRINTF("%s\n", err); 390 } 391 392 if (!test_template_unary(double_to_uint_template, 393 double_to_uint_operator)) { 394 err = "Conversion from double to unsigned int failed"; 395 TPRINTF("%s\n", err); 396 } 397 398 if (!test_template_unary(double_to_uint_template, 399 double_to_int_operator)) { 400 err = "Conversion from double to signed int failed"; 401 TPRINTF("%s\n", err); 402 } 403 252 404 return err; 253 405 } 254
Note:
See TracChangeset
for help on using the changeset viewer.