Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/math/generic/trunc.c

    r9adb61d rb336613f  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
    32 * Copyright (c) 2014 Martin Decky
    43 * All rights reserved.
     
    3938/** Truncate fractional part (round towards zero)
    4039 *
    41  * Truncate the fractional part of IEEE 754 single
    42  * precision floating point number by zeroing fraction
    43  * bits, effectively rounding the number towards zero
    44  * to the nearest whole number.
    45  *
    46  * If the argument is infinity or NaN, an exception
    47  * should be indicated. This is not implemented yet.
    48  *
    49  * @param val Floating point number.
    50  *
    51  * @return Number rounded towards zero.
    52  *
    53  */
    54 float32_t float32_trunc(float32_t val)
    55 {
    56         float32_u v;
    57         int32_t exp;
    58        
    59         v.val = val;
    60         exp = v.data.parts.exp - FLOAT32_BIAS;
    61        
    62         if (exp < 0) {
    63                 /* -1 < val < 1 => result is +0 or -0 */
    64                 v.data.parts.exp = 0;
    65                 v.data.parts.fraction = 0;
    66         } else if (exp >= FLOAT32_FRACTION_SIZE) {
    67                 if (exp == 1024) {
    68                         /* val is +inf, -inf or NaN => trigger an exception */
    69                         // FIXME TODO
    70                 }
    71                
    72                 /* All bits in val are relevant for the result */
    73         } else {
    74                 /* Truncate irrelevant fraction bits */
    75                 v.data.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp);
    76         }
    77        
    78         return v.val;
    79 }
    80 
    81 /** Truncate fractional part (round towards zero)
    82  *
    8340 * Truncate the fractional part of IEEE 754 double
    8441 * precision floating point number by zeroing fraction
     
    9451 *
    9552 */
    96 float64_t float64_trunc(float64_t val)
     53float64 trunc_float64(float64 val)
    9754{
    98         float64_u v;
    99         int32_t exp;
    100        
    101         v.val = val;
    102         exp = v.data.parts.exp - FLOAT64_BIAS;
     55        int32_t exp = val.parts.exp - FLOAT64_BIAS;
    10356       
    10457        if (exp < 0) {
    10558                /* -1 < val < 1 => result is +0 or -0 */
    106                 v.data.parts.exp = 0;
    107                 v.data.parts.fraction = 0;
     59                val.parts.exp = 0;
     60                val.parts.fraction = 0;
    10861        } else if (exp >= FLOAT64_FRACTION_SIZE) {
    10962                if (exp == 1024) {
     
    11568        } else {
    11669                /* Truncate irrelevant fraction bits */
    117                 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
     70                val.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
    11871        }
    11972       
    120         return v.val;
     73        return val;
    12174}
    12275
Note: See TracChangeset for help on using the changeset viewer.