fb0ef1ddc5945c4f7db3126758fb19cd00568875
[riscv-isa-sim.git] / softfloat / s_roundPackToF64.c
1
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include "platform.h"
5 #include "primitives.h"
6 #include "internals.h"
7 #include "softfloat.h"
8
9 float64_t
10 softfloat_roundPackToF64( bool sign, int_fast16_t exp, uint_fast64_t sig )
11 {
12 int roundingMode;
13 bool roundNearestEven;
14 int roundIncrement, roundBits;
15 bool isTiny;
16 uint_fast64_t uiZ;
17 union ui64_f64 uZ;
18
19 roundingMode = softfloat_roundingMode;
20 roundNearestEven = ( roundingMode == softfloat_round_nearest_even );
21 roundIncrement = 0x200;
22 if (
23 ! roundNearestEven
24 && ( roundingMode != softfloat_round_nearest_maxMag )
25 ) {
26 roundIncrement =
27 ( roundingMode == softfloat_round_minMag )
28 || ( roundingMode
29 == ( sign ? softfloat_round_max : softfloat_round_min ) )
30 ? 0
31 : 0x3FF;
32 }
33 roundBits = sig & 0x3FF;
34 if ( 0x7FD <= (uint16_t) exp ) {
35 if ( exp < 0 ) {
36 isTiny =
37 ( softfloat_detectTininess
38 == softfloat_tininess_beforeRounding )
39 || ( exp < -1 )
40 || ( sig + roundIncrement < UINT64_C( 0x8000000000000000 ) );
41 sig = softfloat_shift64RightJam( sig, - exp );
42 exp = 0;
43 roundBits = sig & 0x3FF;
44 if ( isTiny && roundBits ) {
45 softfloat_raiseFlags( softfloat_flag_underflow );
46 }
47 } else if (
48 ( 0x7FD < exp )
49 || ( UINT64_C( 0x8000000000000000 ) <= sig + roundIncrement )
50 ) {
51 softfloat_raiseFlags(
52 softfloat_flag_overflow | softfloat_flag_inexact );
53 uiZ = packToF64UI( sign, 0x7FF, 0 ) - ! roundIncrement;
54 goto uiZ;
55 }
56 }
57 if ( roundBits ) softfloat_exceptionFlags |= softfloat_flag_inexact;
58 sig = ( sig + roundIncrement )>>10;
59 sig &= ~ ( ! ( roundBits ^ 0x200 ) & roundNearestEven );
60 uiZ = packToF64UI( sign, sig ? exp : 0, sig );
61 uiZ:
62 uZ.ui = uiZ;
63 return uZ.f;
64
65 }
66