Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/float/softfloat1.c

    r000494d r7218fe6  
    3434#include <mul.h>
    3535#include <div.h>
    36 #include <bool.h>
     36#include <comparison.h>
     37#include <conversion.h>
     38#include <stdbool.h>
    3739#include "../tester.h"
    3840
    39 #define OPERANDS  6
     41#define OPERANDS   10
    4042#define PRECISION  10000
    4143
     
    4345
    4446typedef 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
     48typedef void (* uint_to_double_op_t)(unsigned int, double *, double_t *);
     49typedef void (* double_to_uint_op_t)(double, unsigned int *, unsigned int *);
     50typedef void (* float_binary_op_t)(float, float, float *, float_t *);
     51typedef void (* double_binary_op_t)(double, double, double *, double_t *);
     52typedef void (* double_cmp_op_t)(double, double, cmptype_t *, cmptype_t *);
     53
     54typedef void (* template_unary_t)(void *, unsigned, cmptype_t *, cmptype_t *);
     55typedef void (* template_binary_t)(void *, unsigned, unsigned, cmptype_t *,
    4856    cmptype_t *);
    4957
    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
     61static float fop_a[OPERANDS] = {
     62        NUMBERS
     63};
     64
     65static double dop_a[OPERANDS] = {
     66        NUMBERS
     67};
     68
     69static unsigned int uop_a[OPERANDS] = {
     70        4, -100, 100, 50, 1024, 0, 1000000, -1U, 0x80000000U, 500
     71};
    6172
    6273static cmptype_t cmpabs(cmptype_t a)
     
    6879}
    6980
    70 static void
    71 float_template(void *f, unsigned i, unsigned j, cmptype_t *pic,
     81static 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
     92static void uint_to_double_template(void *f, unsigned i, cmptype_t *pic,
    7293    cmptype_t *pisc)
    7394{
     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
     105static 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
     119static 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       
    74124        float c;
    75125        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       
    81128        *pic = (cmptype_t) (c * PRECISION);
    82129        *pisc = (cmptype_t) (sc.val * PRECISION);
    83130}
    84131
    85 static void
    86 double_template(void *f, unsigned i, unsigned j, cmptype_t *pic,
    87     cmptype_t *pisc)
    88 {
     132static 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       
    89137        double c;
    90138        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       
    96141        *pic = (cmptype_t) (c * PRECISION);
    97142        *pisc = (cmptype_t) (sc.val * PRECISION);
    98143}
    99144
    100 static bool test_template(template_t template, void *f)
     145static 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
     153static bool test_template_unary(template_unary_t template, void *f)
    101154{
    102155        bool correct = true;
    103156       
    104157        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
     173static 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++) {
    106179                        cmptype_t ic;
    107180                        cmptype_t isc;
    108 
    109                         template(f, i, j, &ic, &isc);   
     181                       
     182                        template(f, i, j, &ic, &isc);
    110183                        cmptype_t diff = cmpabs(ic - isc);
    111184                       
     
    121194}
    122195
     196static 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
     202static 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
     213static 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
    123224static void float_add_operator(float a, float b, float *pc, float_t *psc)
    124225{
     
    130231        sa.val = a;
    131232        sb.val = b;
    132         if (sa.data.parts.sign == sb.data.parts.sign)
     233       
     234        if (sa.data.parts.sign == sb.data.parts.sign) {
    133235                psc->data = add_float(sa.data, sb.data);
    134         else if (sa.data.parts.sign) {
     236        } else if (sa.data.parts.sign) {
    135237                sa.data.parts.sign = 0;
    136238                psc->data = sub_float(sb.data, sa.data);
     
    160262                return;
    161263        }
    162 
     264       
    163265        *pc = a / b;
    164266       
     
    180282        sa.val = a;
    181283        sb.val = b;
    182         if (sa.data.parts.sign == sb.data.parts.sign)
     284       
     285        if (sa.data.parts.sign == sb.data.parts.sign) {
    183286                psc->data = add_double(sa.data, sb.data);
    184         else if (sa.data.parts.sign) {
     287        } else if (sa.data.parts.sign) {
    185288                sa.data.parts.sign = 0;
    186289                psc->data = sub_double(sb.data, sa.data);
     
    210313                return;
    211314        }
    212 
     315       
    213316        *pc = a / b;
    214317       
     
    221324}
    222325
     326static 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
    223347const char *test_softfloat1(void)
    224348{
    225349        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)) {
    228352                err = "Float addition failed";
    229353                TPRINTF("%s\n", err);
    230354        }
    231         if (!test_template(float_template, float_mul_operator)) {
     355       
     356        if (!test_template_binary(float_template_binary, float_mul_operator)) {
    232357                err = "Float multiplication failed";
    233358                TPRINTF("%s\n", err);
    234359        }
    235         if (!test_template(float_template, float_div_operator)) {
     360       
     361        if (!test_template_binary(float_template_binary, float_div_operator)) {
    236362                err = "Float division failed";
    237363                TPRINTF("%s\n", err);
    238364        }
    239         if (!test_template(double_template, double_add_operator)) {
     365       
     366        if (!test_template_binary(double_template_binary, double_add_operator)) {
    240367                err = "Double addition failed";
    241368                TPRINTF("%s\n", err);
    242369        }
    243         if (!test_template(double_template, double_mul_operator)) {
     370       
     371        if (!test_template_binary(double_template_binary, double_mul_operator)) {
    244372                err = "Double multiplication failed";
    245373                TPRINTF("%s\n", err);
    246374        }
    247         if (!test_template(double_template, double_div_operator)) {
     375       
     376        if (!test_template_binary(double_template_binary, double_div_operator)) {
    248377                err = "Double division failed";
    249378                TPRINTF("%s\n", err);
    250379        }
    251380       
     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       
    252404        return err;
    253405}
    254 
Note: See TracChangeset for help on using the changeset viewer.