// File: /workspace/github_c_makefile_star2_O1/mbitsnbites[P]SoftFP/softfloat.c
// Function: roundAndPackFloat16
static float32 roundAndPackFloat16(flag zSign, int_fast16_t zExp,
                                   uint32_t zSig, flag ieee,
                                   float_status *status)
{
    int maxexp = ieee ? 29 : 30;
    uint32_t mask;
    uint32_t increment;
    bool rounding_bumps_exp;
    bool is_tiny = false;

    /* Calculate the mask of bits of the mantissa which are not
     * representable in half-precision and will be lost.
     */
    if (zExp < 1) {
        /* Will be denormal in halfprec */
        mask = 0x00ffffff;
        if (zExp >= -11) {
            mask >>= 11 + zExp;
        }
    } else {
        /* Normal number in halfprec */
        mask = 0x00001fff;
    }

    switch (status->float_rounding_mode) {
    case float_round_nearest_even:
        increment = (mask + 1) >> 1;
        if ((zSig & mask) == increment) {
            increment = zSig & (increment << 1);
        }
        break;
    case float_round_ties_away:
        increment = (mask + 1) >> 1;
        break;
    case float_round_up:
        increment = zSign ? 0 : mask;
        break;
    case float_round_down:
        increment = zSign ? mask : 0;
        break;
    default: /* round_to_zero */
        increment = 0;
        break;
    }

    rounding_bumps_exp = (zSig + increment >= 0x01000000);

    if (zExp > maxexp || (zExp == maxexp && rounding_bumps_exp)) {
        if (ieee) {
            float_raise(float_flag_overflow | float_flag_inexact, status);
            return packFloat16(zSign, 0x1f, 0);
        } else {
            float_raise(float_flag_invalid, status);
            return packFloat16(zSign, 0x1f, 0x3ff);
        }
    }

    if (zExp < 0) {
        /* Note that flush-to-zero does not affect half-precision results */
        is_tiny =
            (status->float_detect_tininess == float_tininess_before_rounding)
            || (zExp < -1)
            || (!rounding_bumps_exp);
    }
    if (zSig & mask) {
        float_raise(float_flag_inexact, status);
        if (is_tiny) {
            float_raise(float_flag_underflow, status);
        }
    }

    zSig += increment;
    if (rounding_bumps_exp) {
        zSig >>= 1;
        zExp++;
    }

    if (zExp < -10) {
        return packFloat16(zSign, 0, 0);
    }
    if (zExp < 0) {
        zSig >>= -zExp;
        zExp = 0;
    }
    return packFloat16(zSign, zExp, zSig >> 13);
}

// Assembly:
movl %ecx, %eax
movl $0x1fff, %r10d          # imm = 0x1FFF
testq %rsi, %rsi
jg 0xd3e2
movl $0xffffff, %r10d        # imm = 0xFFFFFF
cmpq $-0xb, %rsi
jl 0xd3e2
leal 0xb(%rsi), %ecx
movl $0xffffff, %r10d        # imm = 0xFFFFFF
shrl %cl, %r10d
movzbl 0x1(%r8), %ecx
xorl %r9d, %r9d
cmpq $0x4, %rcx
ja 0xd43e
leaq 0x9415(%rip), %r11      # 0x1680c
movslq (%r11,%rcx,4), %rcx
addq %r11, %rcx
jmpq *%rcx
leal 0x1(%r10), %ecx
movl %ecx, %r9d
shrl %r9d
movl %r10d, %r11d
andl %edx, %r11d
cmpl %r9d, %r11d
jne 0xd43e
andl %edx, %ecx
andl $-0x2, %ecx
movl %ecx, %r9d
jmp 0xd43e
xorl %r9d, %r9d
testb %dil, %dil
cmovel %r10d, %r9d
jmp 0xd43e
xorl %r9d, %r9d
testb %dil, %dil
cmovnel %r10d, %r9d
jmp 0xd43e
leal 0x1(%r10), %r9d
shrl %r9d
addl %edx, %r9d
cmpl $0x1000000, %r9d        # imm = 0x1000000
setb %cl
xorl %r11d, %r11d
cmpb $0x1, %al
adcq $0x1d, %r11
cmpq %rsi, %r11
setne %r11b
jl 0xd46b
orb %cl, %r11b
je 0xd46b
testq %rsi, %rsi
js 0xd49e
xorl %eax, %eax
jmp 0xd4bc
movzbl %dil, %ecx
shll $0xf, %ecx
movb 0x2(%r8), %dl
testb %al, %al
je 0xd48c
orb $0x28, %dl
movb %dl, 0x2(%r8)
orl $0x7c00, %ecx           # imm = 0x7C00
jmp 0xd52c
orb $0x1, %dl
movb %dl, 0x2(%r8)
orl $0x7fff, %ecx           # imm = 0x7FFF
jmp 0xd52c
cmpl $0x1000000, %r9d        # imm = 0x1000000
setb %cl
cmpb $0x1, (%r8)
sete %r11b
cmpq $-0x1, %rsi
setne %al
orb %cl, %al
orb %r11b, %al
testl %edx, %r10d
je 0xd4d9
movb 0x2(%r8), %cl
movl %ecx, %edx
orb $0x20, %dl
movb %dl, 0x2(%r8)
testb %al, %al
je 0xd4d9
orb $0x30, %cl
movb %cl, 0x2(%r8)
cmpl $0x1000000, %r9d        # imm = 0x1000000
sbbq $-0x1, %rsi
cmpq $-0xb, %rsi
jg 0xd4f3
movzbl %dil, %ecx
shll $0xf, %ecx
jmp 0xd52c
cmpl $0x1000000, %r9d        # imm = 0x1000000
setae %cl
shrl %cl, %r9d
movl %esi, %eax
negb %al
movq %rsi, %rcx
sarq $0x3f, %rcx
andb %al, %cl
shrl %cl, %r9d
xorl %ecx, %ecx
testq %rsi, %rsi
cmovgq %rsi, %rcx
shrl $0xd, %r9d
movzbl %dil, %eax
shll $0xf, %eax
shll $0xa, %ecx
addl %eax, %ecx
addl %r9d, %ecx
movzwl %cx, %eax
retq