Changeset ff381a7 in mainline for uspace/lib/math/generic/trunc.c


Ignore:
Timestamp:
2015-11-02T20:54:19Z (8 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d8513177
Parents:
3feeab2 (diff), 5265eea4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    r3feeab2 rff381a7  
    11/*
     2 * Copyright (c) 2015 Jiri Svoboda
    23 * Copyright (c) 2014 Martin Decky
    34 * All rights reserved.
     
    3839/** Truncate fractional part (round towards zero)
    3940 *
     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 */
     54float32_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 *
    4083 * Truncate the fractional part of IEEE 754 double
    4184 * precision floating point number by zeroing fraction
     
    5194 *
    5295 */
    53 float64 trunc_float64(float64 val)
     96float64_t float64_trunc(float64_t val)
    5497{
    55         int32_t exp = val.parts.exp - FLOAT64_BIAS;
     98        float64_u v;
     99        int32_t exp;
     100       
     101        v.val = val;
     102        exp = v.data.parts.exp - FLOAT64_BIAS;
    56103       
    57104        if (exp < 0) {
    58105                /* -1 < val < 1 => result is +0 or -0 */
    59                 val.parts.exp = 0;
    60                 val.parts.fraction = 0;
     106                v.data.parts.exp = 0;
     107                v.data.parts.fraction = 0;
    61108        } else if (exp >= FLOAT64_FRACTION_SIZE) {
    62109                if (exp == 1024) {
     
    68115        } else {
    69116                /* Truncate irrelevant fraction bits */
    70                 val.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
     117                v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
    71118        }
    72119       
    73         return val;
     120        return v.val;
    74121}
    75122
Note: See TracChangeset for help on using the changeset viewer.