aboutsummaryrefslogtreecommitdiff
path: root/softfloat/s_roundPackToI32.c
blob: 6f63a3b30557b5f0ebd17c32cb50cb69bcd99c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
#include "internals.h"
#include "softfloat.h"

int_fast32_t
 softfloat_roundPackToI32(
     bool sign, uint_fast64_t sig, int_fast8_t roundingMode, bool exact )
{
    bool roundNearestEven;
    int roundIncrement, roundBits;
    uint_fast32_t sig32;
    union { uint32_t ui; int32_t i; } uZ;
    int_fast32_t z;

    roundNearestEven = ( roundingMode == softfloat_round_nearest_even );
    roundIncrement = 0x40;
    if (
           ! roundNearestEven
        && ( roundingMode != softfloat_round_nearest_maxMag )
    ) {
        roundIncrement =
               ( roundingMode == softfloat_round_minMag )
            || ( roundingMode
                     == ( sign ? softfloat_round_max : softfloat_round_min ) )
                ? 0
                : 0x7F;
    }
    roundBits = sig & 0x7F;
    sig += roundIncrement;
    if ( sig & UINT64_C( 0xFFFFFF8000000000 ) ) goto invalid;
    sig32 = sig>>7;
    sig32 &= ~ ( ! ( roundBits ^ 0x40 ) & roundNearestEven );
    uZ.ui = sign ? - sig32 : sig32;
    z = uZ.i;
    if ( z && ( ( z < 0 ) ^ sign ) ) goto invalid;
    if ( exact && roundBits ) {
        softfloat_raiseFlags( softfloat_flag_inexact );
    }
    return z;
 invalid:
    softfloat_raiseFlags( softfloat_flag_invalid );
    return sign ? -0x7FFFFFFF - 1 : 0x7FFFFFFF;

}