aboutsummaryrefslogtreecommitdiff
path: root/softfloat/f64_to_f32.c
blob: 6882e9d6b6044d063a0fcada23ee7bdeb3025fde (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
// See LICENSE.SoftFloat for license details.


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

float32_t f64_to_f32( float64_t a )
{
    union ui64_f64 uA;
    uint_fast64_t uiA;
    bool sign;
    int_fast16_t exp;
    uint_fast64_t sig;
    uint_fast32_t uiZ, sig32;
    union ui32_f32 uZ;

    uA.f = a;
    uiA = uA.ui;
    sign = signF64UI( uiA );
    exp = expF64UI( uiA );
    sig = fracF64UI( uiA );
    if ( exp == 0x7FF ) {
        uiZ =
            sig ? softfloat_commonNaNToF32UI(
                      softfloat_f64UIToCommonNaN( uiA ) )
                : packToF32UI( sign, 0xFF, 0 );
        goto uiZ;
    }
    sig32 = softfloat_shortShift64RightJam( sig, 22 );
    if ( ! ( exp | sig32 ) ) {
        uiZ = packToF32UI( sign, 0, 0 );
        goto uiZ;
    }
    return softfloat_roundPackToF32( sign, exp - 0x381, sig32 | 0x40000000 );
 uiZ:
    uZ.ui = uiZ;
    return uZ.f;

}