Ignore:
File:
1 edited

Legend:

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

    rc0c38c7c r7218fe6  
    2929#include <stdio.h>
    3030#include <stdlib.h>
    31 #include <mathtypes.h>
     31#include <sftypes.h>
    3232#include <add.h>
    3333#include <sub.h>
     
    3939#include "../tester.h"
    4040
    41 #define add_float  __addsf3
    42 #define sub_float  __subsf3
    43 #define mul_float  __mulsf3
    44 #define div_float  __divsf3
    45 
    46 #define is_float_lt  __ltsf2
    47 #define is_float_gt  __gtsf2
    48 #define is_float_eq  __eqsf2
    49 
    50 #define add_double  __adddf3
    51 #define sub_double  __subdf3
    52 #define mul_double  __muldf3
    53 #define div_double  __divdf3
    54 
    55 #define is_double_lt  __ltdf2
    56 #define is_double_gt  __gtdf2
    57 #define is_double_eq  __eqdf2
    58 
    59 #define uint_to_double  __floatsidf
    60 #define double_to_uint  __fixunsdfsi
    61 #define double_to_int   __fixdfsi
    62 
    6341#define OPERANDS   10
    64 #define PRECISION  1000
     42#define PRECISION  10000
    6543
    6644#define PRIdCMPTYPE  PRId32
     
    6846typedef int32_t cmptype_t;
    6947
    70 typedef void (* uint_to_double_op_t)(unsigned int, double *, double *);
     48typedef void (* uint_to_double_op_t)(unsigned int, double *, double_t *);
    7149typedef void (* double_to_uint_op_t)(double, unsigned int *, unsigned int *);
    72 typedef void (* float_binary_op_t)(float, float, float *, float *);
    73 typedef void (* float_cmp_op_t)(float, float, cmptype_t *, cmptype_t *);
    74 typedef void (* double_binary_op_t)(double, double, double *, double *);
     50typedef void (* float_binary_op_t)(float, float, float *, float_t *);
     51typedef void (* double_binary_op_t)(double, double, double *, double_t *);
    7552typedef void (* double_cmp_op_t)(double, double, cmptype_t *, cmptype_t *);
    7653
     
    9168
    9269static unsigned int uop_a[OPERANDS] = {
    93         4, 2, 100, 50, 1024, 0, 1000000, 1, 0x8000000, 500
     70        4, -100, 100, 50, 1024, 0, 1000000, -1U, 0x80000000U, 500
    9471};
    9572
    96 static int fcmp(float a, float b)
     73static cmptype_t cmpabs(cmptype_t a)
     74{
     75        if (a >= 0)
     76                return a;
     77       
     78        return -a;
     79}
     80
     81static int dcmp(double a, double b)
    9782{
    9883        if (a < b)
     
    10590}
    10691
    107 static int dcmp(double a, double b)
    108 {
    109         if (a < b)
    110                 return -1;
    111        
    112         if (a > b)
    113                 return 1;
    114        
    115         return 0;
    116 }
    117 
    11892static void uint_to_double_template(void *f, unsigned i, cmptype_t *pic,
    11993    cmptype_t *pisc)
     
    12296       
    12397        double c;
    124         double sc;
     98        double_t sc;
    12599        op(uop_a[i], &c, &sc);
    126100       
    127101        *pic = (cmptype_t) (c * PRECISION);
    128         *pisc = (cmptype_t) (sc * PRECISION);
     102        *pisc = (cmptype_t) (sc.val * PRECISION);
    129103}
    130104
     
    142116}
    143117
     118
    144119static void float_template_binary(void *f, unsigned i, unsigned j,
    145120    cmptype_t *pic, cmptype_t *pisc)
     
    148123       
    149124        float c;
    150         float sc;
     125        float_t sc;
    151126        op(fop_a[i], fop_a[j], &c, &sc);
    152127       
    153128        *pic = (cmptype_t) (c * PRECISION);
    154         *pisc = (cmptype_t) (sc * PRECISION);
    155 }
    156 
    157 static void float_compare_template(void *f, unsigned i, unsigned j,
    158     cmptype_t *pis, cmptype_t *piss)
    159 {
    160         float_cmp_op_t op = (float_cmp_op_t) f;
    161        
    162         op(dop_a[i], dop_a[j], pis, piss);
     129        *pisc = (cmptype_t) (sc.val * PRECISION);
    163130}
    164131
     
    169136       
    170137        double c;
    171         double sc;
     138        double_t sc;
    172139        op(dop_a[i], dop_a[j], &c, &sc);
    173140       
    174141        *pic = (cmptype_t) (c * PRECISION);
    175         *pisc = (cmptype_t) (sc * PRECISION);
     142        *pisc = (cmptype_t) (sc.val * PRECISION);
    176143}
    177144
     
    193160               
    194161                template(f, i, &ic, &isc);
    195                 cmptype_t diff = ic - isc;
     162                cmptype_t diff = cmpabs(ic - isc);
    196163               
    197164                if (diff != 0) {
    198                         TPRINTF("i=%u ic=%" PRIdCMPTYPE " isc=%" PRIdCMPTYPE "\n",
    199                             i, ic, isc);
     165                        TPRINTF("i=%u diff=%" PRIdCMPTYPE "\n", i, diff);
    200166                        correct = false;
    201167                }
     
    215181                       
    216182                        template(f, i, j, &ic, &isc);
    217                         cmptype_t diff = ic - isc;
     183                        cmptype_t diff = cmpabs(ic - isc);
    218184                       
    219185                        if (diff != 0) {
    220                                 TPRINTF("i=%u, j=%u ic=%" PRIdCMPTYPE
    221                                     " isc=%" PRIdCMPTYPE "\n", i, j, ic, isc);
     186                                TPRINTF("i=%u, j=%u diff=%" PRIdCMPTYPE "\n",
     187                                    i, j, diff);
    222188                                correct = false;
    223189                        }
     
    228194}
    229195
    230 static void uint_to_double_operator(unsigned int a, double *pc, double *psc)
     196static void uint_to_double_operator(unsigned int a, double *pc, double_t *psc)
    231197{
    232198        *pc = (double) a;
    233         *psc = uint_to_double(a);
     199        psc->data = uint_to_double(a);
    234200}
    235201
     
    237203    unsigned int *psc)
    238204{
     205        double_t sa;
     206       
     207        sa.val = a;
     208       
    239209        *pc = (unsigned int) a;
    240         *psc = double_to_uint(a);
     210        *psc = double_to_uint(sa.data);
    241211}
    242212
     
    244214    unsigned int *psc)
    245215{
     216        double_t sa;
     217       
     218        sa.val = a;
     219       
    246220        *pc = (int) a;
    247         *psc = double_to_int(a);
    248 }
    249 
    250 static void float_add_operator(float a, float b, float *pc, float *psc)
     221        *psc = double_to_int(sa.data);
     222}
     223
     224static void float_add_operator(float a, float b, float *pc, float_t *psc)
    251225{
    252226        *pc = a + b;
    253         *psc = add_float(a, b);
    254 }
    255 
    256 static void float_sub_operator(float a, float b, float *pc, float *psc)
    257 {
    258         *pc = a - b;
    259         *psc = sub_float(a, b);
    260 }
    261 
    262 static void float_mul_operator(float a, float b, float *pc, float *psc)
     227       
     228        float_t sa;
     229        float_t sb;
     230       
     231        sa.val = a;
     232        sb.val = b;
     233       
     234        if (sa.data.parts.sign == sb.data.parts.sign) {
     235                psc->data = add_float(sa.data, sb.data);
     236        } else if (sa.data.parts.sign) {
     237                sa.data.parts.sign = 0;
     238                psc->data = sub_float(sb.data, sa.data);
     239        } else {
     240                sb.data.parts.sign = 0;
     241                psc->data = sub_float(sa.data, sb.data);
     242        }
     243}
     244
     245static void float_mul_operator(float a, float b, float *pc, float_t *psc)
    263246{
    264247        *pc = a * b;
    265         *psc = mul_float(a, b);
    266 }
    267 
    268 static void float_div_operator(float a, float b, float *pc, float *psc)
     248       
     249        float_t sa;
     250        float_t sb;
     251       
     252        sa.val = a;
     253        sb.val = b;
     254        psc->data = mul_float(sa.data, sb.data);
     255}
     256
     257static void float_div_operator(float a, float b, float *pc, float_t *psc)
    269258{
    270259        if ((cmptype_t) b == 0) {
    271260                *pc = 0.0;
    272                 *psc = 0.0;
     261                psc->val = 0.0;
    273262                return;
    274263        }
    275264       
    276265        *pc = a / b;
    277         *psc = div_float(a, b);
    278 }
    279 
    280 static void float_cmp_operator(float a, float b, cmptype_t *pis,
     266       
     267        float_t sa;
     268        float_t sb;
     269       
     270        sa.val = a;
     271        sb.val = b;
     272        psc->data = div_float(sa.data, sb.data);
     273}
     274
     275static void double_add_operator(double a, double b, double *pc, double_t *psc)
     276{
     277        *pc = a + b;
     278       
     279        double_t sa;
     280        double_t sb;
     281       
     282        sa.val = a;
     283        sb.val = b;
     284       
     285        if (sa.data.parts.sign == sb.data.parts.sign) {
     286                psc->data = add_double(sa.data, sb.data);
     287        } else if (sa.data.parts.sign) {
     288                sa.data.parts.sign = 0;
     289                psc->data = sub_double(sb.data, sa.data);
     290        } else {
     291                sb.data.parts.sign = 0;
     292                psc->data = sub_double(sa.data, sb.data);
     293        }
     294}
     295
     296static void double_mul_operator(double a, double b, double *pc, double_t *psc)
     297{
     298        *pc = a * b;
     299       
     300        double_t sa;
     301        double_t sb;
     302       
     303        sa.val = a;
     304        sb.val = b;
     305        psc->data = mul_double(sa.data, sb.data);
     306}
     307
     308static void double_div_operator(double a, double b, double *pc, double_t *psc)
     309{
     310        if ((cmptype_t) b == 0) {
     311                *pc = 0.0;
     312                psc->val = 0.0;
     313                return;
     314        }
     315       
     316        *pc = a / b;
     317       
     318        double_t sa;
     319        double_t sb;
     320       
     321        sa.val = a;
     322        sb.val = b;
     323        psc->data = div_double(sa.data, sb.data);
     324}
     325
     326static void double_cmp_operator(double a, double b, cmptype_t *pis,
    281327    cmptype_t *piss)
    282328{
    283         *pis = fcmp(a, b);
    284        
    285         if (is_float_lt(a, b) == -1)
     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))
    286338                *piss = -1;
    287         else if (is_float_gt(a, b) == 1)
     339        else if (is_double_gt(sa.data, sb.data))
    288340                *piss = 1;
    289         else if (is_float_eq(a, b) == 0)
     341        else if (is_double_eq(sa.data, sb.data))
    290342                *piss = 0;
    291343        else
     
    293345}
    294346
    295 static void double_add_operator(double a, double b, double *pc, double *psc)
    296 {
    297         *pc = a + b;
    298         *psc = add_double(a, b);
    299 }
    300 
    301 static void double_sub_operator(double a, double b, double *pc, double *psc)
    302 {
    303         *pc = a - b;
    304         *psc = sub_double(a, b);
    305 }
    306 
    307 static void double_mul_operator(double a, double b, double *pc, double *psc)
    308 {
    309         *pc = a * b;
    310         *psc = mul_double(a, b);
    311 }
    312 
    313 static void double_div_operator(double a, double b, double *pc, double *psc)
    314 {
    315         if ((cmptype_t) b == 0) {
    316                 *pc = 0.0;
    317                 *psc = 0.0;
    318                 return;
    319         }
    320        
    321         *pc = a / b;
    322         *psc = div_double(a, b);
    323 }
    324 
    325 static void double_cmp_operator(double a, double b, cmptype_t *pis,
    326     cmptype_t *piss)
    327 {
    328         *pis = dcmp(a, b);
    329        
    330         if (is_double_lt(a, b) == -1)
    331                 *piss = -1;
    332         else if (is_double_gt(a, b) == 1)
    333                 *piss = 1;
    334         else if (is_double_eq(a, b) == 0)
    335                 *piss = 0;
    336         else
    337                 *piss = 42;
    338 }
    339 
    340347const char *test_softfloat1(void)
    341348{
    342         bool err = false;
     349        const char *err = NULL;
    343350       
    344351        if (!test_template_binary(float_template_binary, float_add_operator)) {
    345                 err = true;
    346                 TPRINTF("%s\n", "Float addition failed");
    347         }
    348        
    349         if (!test_template_binary(float_template_binary, float_sub_operator)) {
    350                 err = true;
    351                 TPRINTF("%s\n", "Float addition failed");
     352                err = "Float addition failed";
     353                TPRINTF("%s\n", err);
    352354        }
    353355       
    354356        if (!test_template_binary(float_template_binary, float_mul_operator)) {
    355                 err = true;
    356                 TPRINTF("%s\n", "Float multiplication failed");
     357                err = "Float multiplication failed";
     358                TPRINTF("%s\n", err);
    357359        }
    358360       
    359361        if (!test_template_binary(float_template_binary, float_div_operator)) {
    360                 err = true;
    361                 TPRINTF("%s\n", "Float division failed");
    362         }
    363        
    364         if (!test_template_binary(float_compare_template, float_cmp_operator)) {
    365                 err = true;
    366                 TPRINTF("%s\n", "Float comparison failed");
     362                err = "Float division failed";
     363                TPRINTF("%s\n", err);
    367364        }
    368365       
    369366        if (!test_template_binary(double_template_binary, double_add_operator)) {
    370                 err = true;
    371                 TPRINTF("%s\n", "Double addition failed");
    372         }
    373        
    374         if (!test_template_binary(double_template_binary, double_sub_operator)) {
    375                 err = true;
    376                 TPRINTF("%s\n", "Double addition failed");
     367                err = "Double addition failed";
     368                TPRINTF("%s\n", err);
    377369        }
    378370       
    379371        if (!test_template_binary(double_template_binary, double_mul_operator)) {
    380                 err = true;
    381                 TPRINTF("%s\n", "Double multiplication failed");
     372                err = "Double multiplication failed";
     373                TPRINTF("%s\n", err);
    382374        }
    383375       
    384376        if (!test_template_binary(double_template_binary, double_div_operator)) {
    385                 err = true;
    386                 TPRINTF("%s\n", "Double division failed");
     377                err = "Double division failed";
     378                TPRINTF("%s\n", err);
    387379        }
    388380       
    389381        if (!test_template_binary(double_compare_template, double_cmp_operator)) {
    390                 err = true;
    391                 TPRINTF("%s\n", "Double comparison failed");
     382                err = "Double comparison failed";
     383                TPRINTF("%s\n", err);
    392384        }
    393385       
    394386        if (!test_template_unary(uint_to_double_template,
    395387            uint_to_double_operator)) {
    396                 err = true;
    397                 TPRINTF("%s\n", "Conversion from unsigned int to double failed");
     388                err = "Conversion from unsigned int to double failed";
     389                TPRINTF("%s\n", err);
    398390        }
    399391       
    400392        if (!test_template_unary(double_to_uint_template,
    401393            double_to_uint_operator)) {
    402                 err = true;
    403                 TPRINTF("%s\n", "Conversion from double to unsigned int failed");
     394                err = "Conversion from double to unsigned int failed";
     395                TPRINTF("%s\n", err);
    404396        }
    405397       
    406398        if (!test_template_unary(double_to_uint_template,
    407399            double_to_int_operator)) {
    408                 err = true;
    409                 TPRINTF("%s\n", "Conversion from double to signed int failed");
    410         }
    411        
    412         if (err)
    413                 return "Software floating point imprecision";
    414        
    415         return NULL;
    416 }
     400                err = "Conversion from double to signed int failed";
     401                TPRINTF("%s\n", err);
     402        }
     403       
     404        return err;
     405}
Note: See TracChangeset for help on using the changeset viewer.