Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/softfloat/generic/conversion.c

    rc67aff2 r9539be6  
    11/*
    22 * Copyright (c) 2005 Josef Cejka
    3  * Copyright (c) 2011 Petr Koupy
    43 * All rights reserved.
    54 *
     
    3130 * @{
    3231 */
    33 /** @file Conversion of precision and conversion between integers and floats.
    34  */
    35 
    36 #include <sftypes.h>
    37 #include <conversion.h>
    38 #include <comparison.h>
    39 #include <common.h>
     32/** @file
     33 */
     34
     35#include "sftypes.h"
     36#include "conversion.h"
     37#include "comparison.h"
     38#include "common.h"
    4039
    4140float64 convertFloat32ToFloat64(float32 a)
     
    4948       
    5049        if ((isFloat32Infinity(a)) || (isFloat32NaN(a))) {
    51                 result.parts.exp = FLOAT64_MAX_EXPONENT;
     50                result.parts.exp = 0x7FF;
    5251                /* TODO; check if its correct for SigNaNs*/
    5352                return result;
    54         }
     53        };
    5554       
    5655        result.parts.exp = a.parts.exp + ((int) FLOAT64_BIAS - FLOAT32_BIAS);
     
    5857                /* normalize denormalized numbers */
    5958
    60                 if (result.parts.fraction == 0) { /* fix zero */
    61                         result.parts.exp = 0;
     59                if (result.parts.fraction == 0ll) { /* fix zero */
     60                        result.parts.exp = 0ll;
    6261                        return result;
    6362                }
     
    6564                frac = result.parts.fraction;
    6665               
    67                 while (!(frac & FLOAT64_HIDDEN_BIT_MASK)) {
     66                while (!(frac & (0x10000000000000ll))) {
    6867                        frac <<= 1;
    6968                        --result.parts.exp;
    70                 }
     69                };
    7170               
    7271                ++result.parts.exp;
    7372                result.parts.fraction = frac;
    74         }
     73        };
    7574       
    7675        return result;
    77 }
    78 
    79 float128 convertFloat32ToFloat128(float32 a)
    80 {
    81         float128 result;
    82         uint64_t frac_hi, frac_lo;
    83         uint64_t tmp_hi, tmp_lo;
    84 
    85         result.parts.sign = a.parts.sign;
    86         result.parts.frac_hi = 0;
    87         result.parts.frac_lo = a.parts.fraction;
    88         lshift128(result.parts.frac_hi, result.parts.frac_lo,
    89             (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE),
    90             &frac_hi, &frac_lo);
    91         result.parts.frac_hi = frac_hi;
    92         result.parts.frac_lo = frac_lo;
    93 
    94         if ((isFloat32Infinity(a)) || (isFloat32NaN(a))) {
    95                 result.parts.exp = FLOAT128_MAX_EXPONENT;
    96                 /* TODO; check if its correct for SigNaNs*/
    97                 return result;
    98         }
    99 
    100         result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT32_BIAS);
    101         if (a.parts.exp == 0) {
    102                 /* normalize denormalized numbers */
    103 
    104                 if (eq128(result.parts.frac_hi,
    105                     result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */
    106                         result.parts.exp = 0;
    107                         return result;
    108                 }
    109 
    110                 frac_hi = result.parts.frac_hi;
    111                 frac_lo = result.parts.frac_lo;
    112 
    113                 and128(frac_hi, frac_lo,
    114                     FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO,
    115                     &tmp_hi, &tmp_lo);
    116                 while (!lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) {
    117                         lshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
    118                         --result.parts.exp;
    119                 }
    120 
    121                 ++result.parts.exp;
    122                 result.parts.frac_hi = frac_hi;
    123                 result.parts.frac_lo = frac_lo;
    124         }
    125 
    126         return result;
    127 }
    128 
    129 float128 convertFloat64ToFloat128(float64 a)
    130 {
    131         float128 result;
    132         uint64_t frac_hi, frac_lo;
    133         uint64_t tmp_hi, tmp_lo;
    134 
    135         result.parts.sign = a.parts.sign;
    136         result.parts.frac_hi = 0;
    137         result.parts.frac_lo = a.parts.fraction;
    138         lshift128(result.parts.frac_hi, result.parts.frac_lo,
    139             (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE),
    140             &frac_hi, &frac_lo);
    141         result.parts.frac_hi = frac_hi;
    142         result.parts.frac_lo = frac_lo;
    143 
    144         if ((isFloat64Infinity(a)) || (isFloat64NaN(a))) {
    145                 result.parts.exp = FLOAT128_MAX_EXPONENT;
    146                 /* TODO; check if its correct for SigNaNs*/
    147                 return result;
    148         }
    149 
    150         result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT64_BIAS);
    151         if (a.parts.exp == 0) {
    152                 /* normalize denormalized numbers */
    153 
    154                 if (eq128(result.parts.frac_hi,
    155                     result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */
    156                         result.parts.exp = 0;
    157                         return result;
    158                 }
    159 
    160                 frac_hi = result.parts.frac_hi;
    161                 frac_lo = result.parts.frac_lo;
    162 
    163                 and128(frac_hi, frac_lo,
    164                     FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO,
    165                     &tmp_hi, &tmp_lo);
    166                 while (!lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) {
    167                         lshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
    168                         --result.parts.exp;
    169                 }
    170 
    171                 ++result.parts.exp;
    172                 result.parts.frac_hi = frac_hi;
    173                 result.parts.frac_lo = frac_lo;
    174         }
    175 
    176         return result;
     76       
    17777}
    17878
     
    18686       
    18787        if (isFloat64NaN(a)) {
    188                 result.parts.exp = FLOAT32_MAX_EXPONENT;
     88               
     89                result.parts.exp = 0xFF;
    18990               
    19091                if (isFloat64SigNaN(a)) {
    191                         /* set first bit of fraction nonzero */
    192                         result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1;
     92                        result.parts.fraction = 0x400000; /* set first bit of fraction nonzero */
    19393                        return result;
    19494                }
    195 
    196                 /* fraction nonzero but its first bit is zero */
    197                 result.parts.fraction = 0x1;
    198                 return result;
    199         }
     95       
     96                result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */
     97                return result;
     98        };
    20099
    201100        if (isFloat64Infinity(a)) {
    202101                result.parts.fraction = 0;
    203                 result.parts.exp = FLOAT32_MAX_EXPONENT;
    204                 return result;
    205         }
    206 
    207         exp = (int) a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
    208        
    209         if (exp >= FLOAT32_MAX_EXPONENT) {
    210                 /* FIXME: overflow */
     102                result.parts.exp = 0xFF;
     103                return result;
     104        };
     105
     106        exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
     107       
     108        if (exp >= 0xFF) {
     109                /*FIXME: overflow*/
    211110                result.parts.fraction = 0;
    212                 result.parts.exp = FLOAT32_MAX_EXPONENT;
    213                 return result;
    214         } else if (exp <= 0) {
     111                result.parts.exp = 0xFF;
     112                return result;
     113               
     114        } else if (exp <= 0 ) {
     115               
    215116                /* underflow or denormalized */
    216117               
     
    218119               
    219120                exp *= -1;     
    220                 if (exp > FLOAT32_FRACTION_SIZE) {
     121                if (exp > FLOAT32_FRACTION_SIZE ) {
    221122                        /* FIXME: underflow */
    222123                        result.parts.fraction = 0;
    223124                        return result;
    224                 }
     125                };
    225126               
    226127                /* denormalized */
    227128               
    228129                frac = a.parts.fraction;
    229                 frac |= FLOAT64_HIDDEN_BIT_MASK; /* denormalize and set hidden bit */
     130                frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
    230131               
    231132                frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
     
    234135                        --exp;
    235136                        frac >>= 1;
    236                 }
     137                };
    237138                result.parts.fraction = frac;
    238139               
    239140                return result;
    240         }
     141        };
    241142
    242143        result.parts.exp = exp;
    243         result.parts.fraction =
    244             a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
     144        result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
    245145        return result;
    246146}
    247147
    248 float32 convertFloat128ToFloat32(float128 a)
    249 {
    250         float32 result;
    251         int32_t exp;
    252         uint64_t frac_hi, frac_lo;
    253 
    254         result.parts.sign = a.parts.sign;
    255 
    256         if (isFloat128NaN(a)) {
    257                 result.parts.exp = FLOAT32_MAX_EXPONENT;
    258 
    259                 if (isFloat128SigNaN(a)) {
    260                         /* set first bit of fraction nonzero */
    261                         result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1;
    262                         return result;
    263                 }
    264 
    265                 /* fraction nonzero but its first bit is zero */
    266                 result.parts.fraction = 0x1;
    267                 return result;
    268         }
    269 
    270         if (isFloat128Infinity(a)) {
    271                 result.parts.fraction = 0;
    272                 result.parts.exp = FLOAT32_MAX_EXPONENT;
    273                 return result;
    274         }
    275 
    276         exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT32_BIAS;
    277 
    278         if (exp >= FLOAT32_MAX_EXPONENT) {
    279                 /* FIXME: overflow */
    280                 result.parts.fraction = 0;
    281                 result.parts.exp = FLOAT32_MAX_EXPONENT;
    282                 return result;
    283         } else if (exp <= 0) {
    284                 /* underflow or denormalized */
    285 
    286                 result.parts.exp = 0;
    287 
    288                 exp *= -1;
    289                 if (exp > FLOAT32_FRACTION_SIZE) {
    290                         /* FIXME: underflow */
    291                         result.parts.fraction = 0;
    292                         return result;
    293                 }
    294 
    295                 /* denormalized */
    296 
    297                 frac_hi = a.parts.frac_hi;
    298                 frac_lo = a.parts.frac_lo;
    299 
    300                 /* denormalize and set hidden bit */
    301                 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
    302 
    303                 rshift128(frac_hi, frac_lo,
    304                     (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1),
    305                     &frac_hi, &frac_lo);
    306 
    307                 while (exp > 0) {
    308                         --exp;
    309                         rshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
    310                 }
    311                 result.parts.fraction = frac_lo;
    312 
    313                 return result;
    314         }
    315 
    316         result.parts.exp = exp;
    317         frac_hi = a.parts.frac_hi;
    318         frac_lo = a.parts.frac_lo;
    319         rshift128(frac_hi, frac_lo,
    320             (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1),
    321             &frac_hi, &frac_lo);
    322         result.parts.fraction = frac_lo;
    323         return result;
    324 }
    325 
    326 float64 convertFloat128ToFloat64(float128 a)
    327 {
    328         float64 result;
    329         int32_t exp;
    330         uint64_t frac_hi, frac_lo;
    331 
    332         result.parts.sign = a.parts.sign;
    333 
    334         if (isFloat128NaN(a)) {
    335                 result.parts.exp = FLOAT64_MAX_EXPONENT;
    336 
    337                 if (isFloat128SigNaN(a)) {
    338                         /* set first bit of fraction nonzero */
    339                         result.parts.fraction = FLOAT64_HIDDEN_BIT_MASK >> 1;
    340                         return result;
    341                 }
    342 
    343                 /* fraction nonzero but its first bit is zero */
    344                 result.parts.fraction = 0x1;
    345                 return result;
    346         }
    347 
    348         if (isFloat128Infinity(a)) {
    349                 result.parts.fraction = 0;
    350                 result.parts.exp = FLOAT64_MAX_EXPONENT;
    351                 return result;
    352         }
    353 
    354         exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT64_BIAS;
    355 
    356         if (exp >= FLOAT64_MAX_EXPONENT) {
    357                 /* FIXME: overflow */
    358                 result.parts.fraction = 0;
    359                 result.parts.exp = FLOAT64_MAX_EXPONENT;
    360                 return result;
    361         } else if (exp <= 0) {
    362                 /* underflow or denormalized */
    363 
    364                 result.parts.exp = 0;
    365 
    366                 exp *= -1;
    367                 if (exp > FLOAT64_FRACTION_SIZE) {
    368                         /* FIXME: underflow */
    369                         result.parts.fraction = 0;
    370                         return result;
    371                 }
    372 
    373                 /* denormalized */
    374 
    375                 frac_hi = a.parts.frac_hi;
    376                 frac_lo = a.parts.frac_lo;
    377 
    378                 /* denormalize and set hidden bit */
    379                 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
    380 
    381                 rshift128(frac_hi, frac_lo,
    382                     (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE + 1),
    383                     &frac_hi, &frac_lo);
    384 
    385                 while (exp > 0) {
    386                         --exp;
    387                         rshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
    388                 }
    389                 result.parts.fraction = frac_lo;
    390 
    391                 return result;
    392         }
    393 
    394         result.parts.exp = exp;
    395         frac_hi = a.parts.frac_hi;
    396         frac_lo = a.parts.frac_lo;
    397         rshift128(frac_hi, frac_lo,
    398             (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE + 1),
    399             &frac_hi, &frac_lo);
    400         result.parts.fraction = frac_lo;
    401         return result;
    402 }
    403 
    404 
    405 /**
    406  * Helping procedure for converting float32 to uint32.
    407  *
    408  * @param a Floating point number in normalized form
    409  *     (NaNs or Inf are not checked).
    410  * @return Converted unsigned integer.
     148
     149/** Helping procedure for converting float32 to uint32
     150 * @param a floating point number in normalized form (no NaNs or Inf are checked )
     151 * @return unsigned integer
    411152 */
    412153static uint32_t _float32_to_uint32_helper(float32 a)
     
    415156       
    416157        if (a.parts.exp < FLOAT32_BIAS) {
    417                 /* TODO: rounding */
     158                /*TODO: rounding*/
    418159                return 0;
    419160        }
     
    434175}
    435176
    436 /*
     177/* Convert float to unsigned int32
    437178 * FIXME: Im not sure what to return if overflow/underflow happens
    438179 *      - now its the biggest or the smallest int
     
    453194}
    454195
    455 /*
     196/* Convert float to signed int32
    456197 * FIXME: Im not sure what to return if overflow/underflow happens
    457198 *      - now its the biggest or the smallest int
     
    473214
    474215
    475 /**
    476  * Helping procedure for converting float32 to uint64.
    477  *
    478  * @param a Floating point number in normalized form
    479  *     (NaNs or Inf are not checked).
    480  * @return Converted unsigned integer.
     216/** Helping procedure for converting float64 to uint64
     217 * @param a floating point number in normalized form (no NaNs or Inf are checked )
     218 * @return unsigned integer
     219 */
     220static uint64_t _float64_to_uint64_helper(float64 a)
     221{
     222        uint64_t frac;
     223       
     224        if (a.parts.exp < FLOAT64_BIAS) {
     225                /*TODO: rounding*/
     226                return 0;
     227        }
     228       
     229        frac = a.parts.fraction;
     230       
     231        frac |= FLOAT64_HIDDEN_BIT_MASK;
     232        /* shift fraction to left so hidden bit will be the most significant bit */
     233        frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
     234
     235        frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
     236        if ((a.parts.sign == 1) && (frac != 0)) {
     237                frac = ~frac;
     238                ++frac;
     239        }
     240       
     241        return frac;
     242}
     243
     244/* Convert float to unsigned int64
     245 * FIXME: Im not sure what to return if overflow/underflow happens
     246 *      - now its the biggest or the smallest int
     247 */
     248uint64_t float64_to_uint64(float64 a)
     249{
     250        if (isFloat64NaN(a))
     251                return UINT64_MAX;
     252       
     253       
     254        if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
     255                if (a.parts.sign)
     256                        return UINT64_MIN;
     257               
     258                return UINT64_MAX;
     259        }
     260       
     261        return _float64_to_uint64_helper(a);
     262}
     263
     264/* Convert float to signed int64
     265 * FIXME: Im not sure what to return if overflow/underflow happens
     266 *      - now its the biggest or the smallest int
     267 */
     268int64_t float64_to_int64(float64 a)
     269{
     270        if (isFloat64NaN(a))
     271                return INT64_MAX;
     272       
     273       
     274        if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
     275                if (a.parts.sign)
     276                        return INT64_MIN;
     277               
     278                return INT64_MAX;
     279        }
     280       
     281        return _float64_to_uint64_helper(a);
     282}
     283
     284
     285
     286
     287
     288/** Helping procedure for converting float32 to uint64
     289 * @param a floating point number in normalized form (no NaNs or Inf are checked )
     290 * @return unsigned integer
    481291 */
    482292static uint64_t _float32_to_uint64_helper(float32 a)
    483293{
    484294        uint64_t frac;
    485 
     295       
    486296        if (a.parts.exp < FLOAT32_BIAS) {
    487297                /*TODO: rounding*/
    488298                return 0;
    489299        }
    490 
     300       
    491301        frac = a.parts.fraction;
    492 
     302       
    493303        frac |= FLOAT32_HIDDEN_BIT_MASK;
    494304        /* shift fraction to left so hidden bit will be the most significant bit */
    495         frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
     305        frac <<= 64 - FLOAT32_FRACTION_SIZE - 1; 
    496306
    497307        frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
     
    500310                ++frac;
    501311        }
    502 
     312       
    503313        return frac;
    504314}
    505315
    506 /*
    507  * FIXME: Im not sure what to return if overflow/underflow happens
    508  *      - now its the biggest or the smallest int
    509  */
     316/* Convert float to unsigned int64
     317 * FIXME: Im not sure what to return if overflow/underflow happens 
     318 *      - now its the biggest or the smallest int
     319 */ 
    510320uint64_t float32_to_uint64(float32 a)
    511321{
    512322        if (isFloat32NaN(a))
    513323                return UINT64_MAX;
    514 
    515 
     324       
     325       
    516326        if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
    517327                if (a.parts.sign)
    518328                        return UINT64_MIN;
    519 
     329               
    520330                return UINT64_MAX;
    521331        }
    522 
     332       
    523333        return _float32_to_uint64_helper(a);
    524334}
    525335
    526 /*
    527  * FIXME: Im not sure what to return if overflow/underflow happens
    528  *      - now its the biggest or the smallest int
    529  */
     336/* Convert float to signed int64
     337 * FIXME: Im not sure what to return if overflow/underflow happens 
     338 *      - now its the biggest or the smallest int
     339 */ 
    530340int64_t float32_to_int64(float32 a)
    531341{
    532342        if (isFloat32NaN(a))
    533343                return INT64_MAX;
    534 
     344       
    535345        if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
    536346                if (a.parts.sign)
    537347                        return INT64_MIN;
    538 
     348               
    539349                return INT64_MAX;
    540350        }
    541 
     351       
    542352        return _float32_to_uint64_helper(a);
    543353}
    544354
    545355
    546 /**
    547  * Helping procedure for converting float64 to uint64.
    548  *
    549  * @param a Floating point number in normalized form
    550  *     (NaNs or Inf are not checked).
    551  * @return Converted unsigned integer.
    552  */
    553 static uint64_t _float64_to_uint64_helper(float64 a)
    554 {
    555         uint64_t frac;
    556 
    557         if (a.parts.exp < FLOAT64_BIAS) {
    558                 /*TODO: rounding*/
    559                 return 0;
    560         }
    561 
    562         frac = a.parts.fraction;
    563 
    564         frac |= FLOAT64_HIDDEN_BIT_MASK;
    565         /* shift fraction to left so hidden bit will be the most significant bit */
    566         frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
    567 
    568         frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
    569         if ((a.parts.sign == 1) && (frac != 0)) {
    570                 frac = ~frac;
    571                 ++frac;
    572         }
    573 
    574         return frac;
    575 }
    576 
    577 /*
    578  * FIXME: Im not sure what to return if overflow/underflow happens
    579  *      - now its the biggest or the smallest int
    580  */
     356/* Convert float64 to unsigned int32
     357 * FIXME: Im not sure what to return if overflow/underflow happens
     358 *      - now its the biggest or the smallest int
     359 */
    581360uint32_t float64_to_uint32(float64 a)
    582361{
    583362        if (isFloat64NaN(a))
    584363                return UINT32_MAX;
    585 
     364       
     365       
    586366        if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
    587367                if (a.parts.sign)
    588368                        return UINT32_MIN;
    589 
     369               
    590370                return UINT32_MAX;
    591371        }
    592 
     372       
    593373        return (uint32_t) _float64_to_uint64_helper(a);
    594374}
    595375
    596 /*
    597  * FIXME: Im not sure what to return if overflow/underflow happens
    598  *      - now its the biggest or the smallest int
    599  */
     376/* Convert float64 to signed int32
     377 * FIXME: Im not sure what to return if overflow/underflow happens 
     378 *      - now its the biggest or the smallest int
     379 */ 
    600380int32_t float64_to_int32(float64 a)
    601381{
    602382        if (isFloat64NaN(a))
    603383                return INT32_MAX;
    604 
     384       
     385       
    605386        if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
    606387                if (a.parts.sign)
    607388                        return INT32_MIN;
    608 
     389               
    609390                return INT32_MAX;
    610391        }
    611 
     392       
    612393        return (int32_t) _float64_to_uint64_helper(a);
    613394}
    614395
    615 
    616 /*
    617  * FIXME: Im not sure what to return if overflow/underflow happens
    618  *      - now its the biggest or the smallest int
    619  */
    620 uint64_t float64_to_uint64(float64 a)
    621 {
    622         if (isFloat64NaN(a))
    623                 return UINT64_MAX;
    624        
    625         if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
    626                 if (a.parts.sign)
    627                         return UINT64_MIN;
    628                
    629                 return UINT64_MAX;
    630         }
    631        
    632         return _float64_to_uint64_helper(a);
    633 }
    634 
    635 /*
    636  * FIXME: Im not sure what to return if overflow/underflow happens
    637  *      - now its the biggest or the smallest int
    638  */
    639 int64_t float64_to_int64(float64 a)
    640 {
    641         if (isFloat64NaN(a))
    642                 return INT64_MAX;
    643        
    644         if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
    645                 if (a.parts.sign)
    646                         return INT64_MIN;
    647                
    648                 return INT64_MAX;
    649         }
    650        
    651         return _float64_to_uint64_helper(a);
    652 }
    653 
    654 
    655 /**
    656  * Helping procedure for converting float128 to uint64.
    657  *
    658  * @param a Floating point number in normalized form
    659  *     (NaNs or Inf are not checked).
    660  * @return Converted unsigned integer.
    661  */
    662 static uint64_t _float128_to_uint64_helper(float128 a)
    663 {
    664         uint64_t frac_hi, frac_lo;
    665 
    666         if (a.parts.exp < FLOAT128_BIAS) {
    667                 /*TODO: rounding*/
    668                 return 0;
    669         }
    670 
    671         frac_hi = a.parts.frac_hi;
    672         frac_lo = a.parts.frac_lo;
    673 
    674         frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
    675         /* shift fraction to left so hidden bit will be the most significant bit */
    676         lshift128(frac_hi, frac_lo,
    677             (128 - FLOAT128_FRACTION_SIZE - 1), &frac_hi, &frac_lo);
    678 
    679         rshift128(frac_hi, frac_lo,
    680             (128 - (a.parts.exp - FLOAT128_BIAS) - 1), &frac_hi, &frac_lo);
    681         if ((a.parts.sign == 1) && !eq128(frac_hi, frac_lo, 0x0ll, 0x0ll)) {
    682                 not128(frac_hi, frac_lo, &frac_hi, &frac_lo);
    683                 add128(frac_hi, frac_lo, 0x0ll, 0x1ll, &frac_hi, &frac_lo);
    684         }
    685 
    686         return frac_lo;
    687 }
    688 
    689 /*
    690  * FIXME: Im not sure what to return if overflow/underflow happens
    691  *      - now its the biggest or the smallest int
    692  */
    693 uint32_t float128_to_uint32(float128 a)
    694 {
    695         if (isFloat128NaN(a))
    696                 return UINT32_MAX;
    697 
    698         if (isFloat128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {
    699                 if (a.parts.sign)
    700                         return UINT32_MIN;
    701 
    702                 return UINT32_MAX;
    703         }
    704 
    705         return (uint32_t) _float128_to_uint64_helper(a);
    706 }
    707 
    708 /*
    709  * FIXME: Im not sure what to return if overflow/underflow happens
    710  *      - now its the biggest or the smallest int
    711  */
    712 int32_t float128_to_int32(float128 a)
    713 {
    714         if (isFloat128NaN(a))
    715                 return INT32_MAX;
    716 
    717         if (isFloat128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {
    718                 if (a.parts.sign)
    719                         return INT32_MIN;
    720 
    721                 return INT32_MAX;
    722         }
    723 
    724         return (int32_t) _float128_to_uint64_helper(a);
    725 }
    726 
    727 
    728 /*
    729  * FIXME: Im not sure what to return if overflow/underflow happens
    730  *      - now its the biggest or the smallest int
    731  */
    732 uint64_t float128_to_uint64(float128 a)
    733 {
    734         if (isFloat128NaN(a))
    735                 return UINT64_MAX;
    736 
    737         if (isFloat128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {
    738                 if (a.parts.sign)
    739                         return UINT64_MIN;
    740 
    741                 return UINT64_MAX;
    742         }
    743 
    744         return _float128_to_uint64_helper(a);
    745 }
    746 
    747 /*
    748  * FIXME: Im not sure what to return if overflow/underflow happens
    749  *      - now its the biggest or the smallest int
    750  */
    751 int64_t float128_to_int64(float128 a)
    752 {
    753         if (isFloat128NaN(a))
    754                 return INT64_MAX;
    755 
    756         if (isFloat128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {
    757                 if (a.parts.sign)
    758                         return INT64_MIN;
    759 
    760                 return INT64_MAX;
    761         }
    762 
    763         return _float128_to_uint64_helper(a);
    764 }
    765 
    766 
     396/** Convert unsigned integer to float32
     397 *
     398 *
     399 */
    767400float32 uint32_to_float32(uint32_t i)
    768401{
     
    791424        roundFloat32(&exp, &i);
    792425
    793         result.parts.fraction = i >> (32 - FLOAT32_FRACTION_SIZE - 2);
     426        result.parts.fraction = i >> 7;
    794427        result.parts.exp = exp;
    795428
     
    802435
    803436        if (i < 0) {
    804                 result = uint32_to_float32((uint32_t) (-i));
    805         } else {
    806                 result = uint32_to_float32((uint32_t) i);
     437                result = uint32_to_float32((uint32_t)(-i));
     438        } else {
     439                result = uint32_to_float32((uint32_t)i);
    807440        }
    808441       
     
    832465        }
    833466       
    834         /* Shift all to the first 31 bits (31st will be hidden 1) */
     467        /* Shift all to the first 31 bits (31. will be hidden 1)*/
    835468        if (counter > 33) {
    836469                i <<= counter - 1 - 32;
     
    839472        }
    840473       
    841         j = (uint32_t) i;
     474        j = (uint32_t)i;
    842475        roundFloat32(&exp, &j);
    843476
    844         result.parts.fraction = j >> (32 - FLOAT32_FRACTION_SIZE - 2);
     477        result.parts.fraction = j >> 7;
    845478        result.parts.exp = exp;
    846479        return result;
     
    852485
    853486        if (i < 0) {
    854                 result = uint64_to_float32((uint64_t) (-i));
    855         } else {
    856                 result = uint64_to_float32((uint64_t) i);
     487                result = uint64_to_float32((uint64_t)(-i));
     488        } else {
     489                result = uint64_to_float32((uint64_t)i);
    857490        }
    858491       
     
    862495}
    863496
     497/** Convert unsigned integer to float64
     498 *
     499 *
     500 */
    864501float64 uint32_to_float64(uint32_t i)
    865502{
     
    886523        roundFloat64(&exp, &frac);
    887524
    888         result.parts.fraction = frac >> (64 - FLOAT64_FRACTION_SIZE - 2);
     525        result.parts.fraction = frac >> 10;
    889526        result.parts.exp = exp;
    890527
     
    897534
    898535        if (i < 0) {
    899                 result = uint32_to_float64((uint32_t) (-i));
    900         } else {
    901                 result = uint32_to_float64((uint32_t) i);
     536                result = uint32_to_float64((uint32_t)(-i));
     537        } else {
     538                result = uint32_to_float64((uint32_t)i);
    902539        }
    903540       
     
    934571        roundFloat64(&exp, &i);
    935572
    936         result.parts.fraction = i >> (64 - FLOAT64_FRACTION_SIZE - 2);
     573        result.parts.fraction = i >> 10;
    937574        result.parts.exp = exp;
    938575        return result;
     
    944581
    945582        if (i < 0) {
    946                 result = uint64_to_float64((uint64_t) (-i));
    947         } else {
    948                 result = uint64_to_float64((uint64_t) i);
     583                result = uint64_to_float64((uint64_t)(-i));
     584        } else {
     585                result = uint64_to_float64((uint64_t)i);
    949586        }
    950587       
     
    954591}
    955592
    956 
    957 float128 uint32_to_float128(uint32_t i)
    958 {
    959         int counter;
    960         int32_t exp;
    961         float128 result;
    962         uint64_t frac_hi, frac_lo;
    963 
    964         result.parts.sign = 0;
    965         result.parts.frac_hi = 0;
    966         result.parts.frac_lo = 0;
    967 
    968         counter = countZeroes32(i);
    969 
    970         exp = FLOAT128_BIAS + 32 - counter - 1;
    971 
    972         if (counter == 32) {
    973                 result.binary.hi = 0;
    974                 result.binary.lo = 0;
    975                 return result;
    976         }
    977 
    978         frac_hi = 0;
    979         frac_lo = i;
    980         lshift128(frac_hi, frac_lo, (counter + 96 - 1), &frac_hi, &frac_lo);
    981 
    982         roundFloat128(&exp, &frac_hi, &frac_lo);
    983 
    984         rshift128(frac_hi, frac_lo,
    985             (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo);
    986         result.parts.frac_hi = frac_hi;
    987         result.parts.frac_lo = frac_lo;
    988         result.parts.exp = exp;
    989 
    990         return result;
    991 }
    992 
    993 float128 int32_to_float128(int32_t i)
    994 {
    995         float128 result;
    996 
    997         if (i < 0) {
    998                 result = uint32_to_float128((uint32_t) (-i));
    999         } else {
    1000                 result = uint32_to_float128((uint32_t) i);
    1001         }
    1002 
    1003         result.parts.sign = i < 0;
    1004 
    1005         return result;
    1006 }
    1007 
    1008 
    1009 float128 uint64_to_float128(uint64_t i)
    1010 {
    1011         int counter;
    1012         int32_t exp;
    1013         float128 result;
    1014         uint64_t frac_hi, frac_lo;
    1015 
    1016         result.parts.sign = 0;
    1017         result.parts.frac_hi = 0;
    1018         result.parts.frac_lo = 0;
    1019 
    1020         counter = countZeroes64(i);
    1021 
    1022         exp = FLOAT128_BIAS + 64 - counter - 1;
    1023 
    1024         if (counter == 64) {
    1025                 result.binary.hi = 0;
    1026                 result.binary.lo = 0;
    1027                 return result;
    1028         }
    1029 
    1030         frac_hi = 0;
    1031         frac_lo = i;
    1032         lshift128(frac_hi, frac_lo, (counter + 64 - 1), &frac_hi, &frac_lo);
    1033 
    1034         roundFloat128(&exp, &frac_hi, &frac_lo);
    1035 
    1036         rshift128(frac_hi, frac_lo,
    1037             (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo);
    1038         result.parts.frac_hi = frac_hi;
    1039         result.parts.frac_lo = frac_lo;
    1040         result.parts.exp = exp;
    1041 
    1042         return result;
    1043 }
    1044 
    1045 float128 int64_to_float128(int64_t i)
    1046 {
    1047         float128 result;
    1048 
    1049         if (i < 0) {
    1050                 result = uint64_to_float128((uint64_t) (-i));
    1051         } else {
    1052                 result = uint64_to_float128((uint64_t) i);
    1053         }
    1054 
    1055         result.parts.sign = i < 0;
    1056 
    1057         return result;
    1058 }
    1059 
    1060593/** @}
    1061594 */
Note: See TracChangeset for help on using the changeset viewer.