aboutsummaryrefslogtreecommitdiff
path: root/softfloat/SoftFloat-3/source/s_roundPackToF32.c
blob: 11764f1507894f09721ac0877af71aac3be0e26a (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

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

float32_t
 softfloat_roundPackToF32( bool sign, int_fast16_t exp, uint_fast32_t sig )
{
    int roundingMode;
    bool roundNearestEven;
    int roundIncrement, roundBits;
    bool isTiny;
    uint_fast32_t uiZ;
    union ui32_f32 uZ;

    roundingMode = softfloat_roundingMode;
    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;
    if ( 0xFD <= (uint16_t) exp ) {
        if ( exp < 0 ) {
            isTiny =
                   ( softfloat_detectTininess
                         == softfloat_tininess_beforeRounding )
                || ( exp < -1 )
                || ( sig + roundIncrement < 0x80000000 );
            sig = softfloat_shift32RightJam( sig, - exp );
            exp = 0;
            roundBits = sig & 0x7F;
            if ( isTiny && roundBits ) {
                softfloat_raiseFlags( softfloat_flag_underflow );
            }
        } else if (
            ( 0xFD < exp ) || ( 0x80000000 <= sig + roundIncrement )
        ) {
            softfloat_raiseFlags(
                softfloat_flag_overflow | softfloat_flag_inexact );
            uiZ = packToF32UI( sign, 0xFF, 0 ) - ! roundIncrement;
            goto uiZ;
        }
    }
    if ( roundBits ) softfloat_exceptionFlags |= softfloat_flag_inexact;
    sig = ( sig + roundIncrement )>>7;
    sig &= ~ ( ! ( roundBits ^ 0x40 ) & roundNearestEven );
    uiZ = packToF32UI( sign, sig ? exp : 0, sig );
 uiZ:
    uZ.ui = uiZ;
    return uZ.f;

}