Changeset afffa1e in mainline for softfloat/generic/conversion.c


Ignore:
Timestamp:
2006-02-20T23:12:05Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2cb202e
Parents:
d9f51ccc
Message:

Conversion functions from float to int added.
Files arch.h and types.h from arch subdirectory should be replaced later with equivalent files from libc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • softfloat/generic/conversion.c

    rd9f51ccc rafffa1e  
    2929#include "sftypes.h"
    3030#include "conversion.h"
     31#include "comparison.h"
    3132
    3233float64 convertFloat32ToFloat64(float32 a)
     
    6768        return result;
    6869       
    69 };
     70}
    7071
    7172float32 convertFloat64ToFloat32(float64 a)
     
    136137        result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
    137138        return result;
    138 };
    139 
     139}
     140
     141
     142/** Helping procedure for converting float32 to uint32
     143 * @param a floating point number in normalized form (no NaNs or Inf are checked )
     144 * @return unsigned integer
     145 */
     146static __u32 _float32_to_uint32_helper(float32 a)
     147{
     148        __u32 frac;
     149       
     150        if (a.parts.exp < FLOAT32_BIAS) {
     151                /*TODO: rounding*/
     152                return 0;
     153        }
     154       
     155        frac = a.parts.fraction;
     156       
     157        frac |= FLOAT32_HIDDEN_BIT_MASK;
     158        /* shift fraction to left so hidden bit will be the most significant bit */
     159        frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
     160
     161        frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
     162        if ((a.parts.sign == 1) && (frac != 0)) {
     163                frac = ~frac;
     164                ++frac;
     165        }
     166       
     167        return frac;
     168}
     169
     170/* Convert float to unsigned int32
     171 * FIXME: Im not sure what to return if overflow/underflow happens
     172 *      - now its the biggest or the smallest int
     173 */
     174__u32 float32_to_uint32(float32 a)
     175{
     176        if (isFloat32NaN(a)) {
     177                return MAX_UINT32;
     178        }
     179       
     180        if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
     181                if (a.parts.sign) {
     182                        return MIN_UINT32;
     183                }
     184                return MAX_UINT32;
     185        }
     186       
     187        return _float32_to_uint32_helper(a);   
     188}
     189
     190/* Convert float to signed int32
     191 * FIXME: Im not sure what to return if overflow/underflow happens
     192 *      - now its the biggest or the smallest int
     193 */
     194__s32 float32_to_int32(float32 a)
     195{
     196        if (isFloat32NaN(a)) {
     197                return MAX_INT32;
     198        }
     199       
     200        if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
     201                if (a.parts.sign) {
     202                        return MIN_INT32;
     203                }
     204                return MAX_INT32;
     205        }
     206        return _float32_to_uint32_helper(a);
     207}       
     208
     209
     210
Note: See TracChangeset for help on using the changeset viewer.