Сделай тесты для кода:
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

#define FP16_SIGN_MASK 0x8000
#define FP16_EXP_MASK 0x1F
#define FP16_MANT_MASK 0x3FF
#define FP16_EXP_BIAS 15
#define FP16_MAX_EXP 31
#define FP16_INFINITY 0x7C00
#define FP16_MANTISSA_BITS 10
#define DIFF 13
#define MAXIMUM 0x7FFF

#define FP16_SIGN(x) ((x) >> 15)
#define FP16_EXP(x) (((x) >> 10) & FP16_EXP_MASK)
#define FP16_MANT(x) ((x)&FP16_MANT_MASK)

uint16_t fp16_cast(unsigned int x) {
    if (x == 0) {
        return 0;
    }

    int p = 0;
    unsigned int temp = x;
    while ((temp >>= 1)) {
        p++;
    }

    int exponent = p + FP16_EXP_BIAS;

    if (exponent >= FP16_MAX_EXP) {
        return FP16_INFINITY;
    }

    unsigned int mantissa;
    unsigned int g = 0, r = 0, s = 0;
    int shift = p - FP16_MANTISSA_BITS;

    if (shift > 0) {
        mantissa = (x >> shift) & FP16_MANT_MASK;

        g = (x >> (shift - 1)) & 1;

        if (shift - 2 >= 0) {
            r = (x >> (shift - 2)) & 1;
            if (shift - 2 > 0) {
                s = (x & ((1U << (shift - 2)) - 1)) != 0;
            } else {
                s = 0;
            }
        } else {
            r = 0;
            s = 0;
        }

        if ((g && (r || s)) || (g && !r && !s && (mantissa & 1))) {
            mantissa += 1;
            if (mantissa > FP16_MANT_MASK) {
                mantissa = 0;
                exponent += 1;
                if (exponent >= FP16_MAX_EXP) {
                    return FP16_INFINITY;
                }
            }
        }
    } else {
        mantissa = (x << (-shift)) & FP16_MANT_MASK;
    }

    return (exponent << FP16_MANTISSA_BITS) | mantissa;
}

uint16_t fp16_mul2(uint16_t x) {
    uint16_t sign = x & FP16_SIGN_MASK;
    uint16_t exp = (x >> FP16_MANTISSA_BITS) & FP16_EXP_MASK;
    uint16_t mant = x & FP16_MANT_MASK;

    if (exp == FP16_MAX_EXP) {
        return x;
    }

    if (exp == 0) {
        mant <<= 1;
        if (mant & (1 << FP16_MANTISSA_BITS)) {
            mant &= FP16_MANT_MASK;
            exp = 1;
        }
    } else {
        exp += 1;
        if (exp == FP16_MAX_EXP) {
            mant = 0;
        }
    }

    return sign | (exp << FP16_MANTISSA_BITS) | mant;
}

uint16_t fp16_div2(uint16_t x) {
    uint16_t sign = x & FP16_SIGN_MASK;
    uint16_t exp = (x >> FP16_MANTISSA_BITS) & FP16_EXP_MASK;
    uint16_t mant = x & FP16_MANT_MASK;

    if (exp == FP16_MAX_EXP) {
        return x;
    }

    if (exp == 0) {
        mant >>= 1;
    } else {
        if (exp > 1) {
            exp -= 1;
        } else {
            exp = 0;
            mant = (mant | (1 << FP16_MANTISSA_BITS)) >> 1;
        }
    }

    return sign | (exp << FP16_MANTISSA_BITS) | mant;
}

uint16_t fp16_neg(uint16_t x) {
    return x ^ FP16_SIGN_MASK;
}

uint16_t fp16_add(uint16_t x, uint16_t y) {
    uint16_t sign_x = FP16_SIGN(x);
    uint16_t exp_x = FP16_EXP(x);
    uint16_t mant_x = FP16_MANT(x);

    uint16_t sign_y = FP16_SIGN(y);
    uint16_t exp_y = FP16_EXP(y);
    uint16_t mant_y = FP16_MANT(y);

    if (exp_x == FP16_MAX_EXP) {
        if (mant_x != 0) {
            return x;
        }
        if (exp_y == FP16_MAX_EXP && mant_y != 0) {
            return y;
        }
        return x;
    }
    if (exp_y == FP16_MAX_EXP) {
        if (mant_y != 0) {
            return y;
        }
        return y;
    }

    uint32_t mantissa_x;
    if (exp_x != 0) {
        mantissa_x = (1 << FP16_MANTISSA_BITS) | mant_x;
    } else {
        mantissa_x = mant_x;
    }

    uint32_t mantissa_y;
    if (exp_y != 0) {
        mantissa_y = (1 << FP16_MANTISSA_BITS) | mant_y;
    } else {
        mantissa_y = mant_y;
    }

    if (exp_x > exp_y) {
        int delta = exp_x - exp_y;
        if (delta > DIFF) {
            mantissa_y = 0;
            exp_y = exp_x;
        } else {
            mantissa_y >>= delta;
            exp_y = exp_x;
        }
    } else if (exp_y > exp_x) {
        int delta = exp_y - exp_x;
        if (delta > DIFF) {
            mantissa_x = 0;
            exp_x = exp_y;
        } else {
            mantissa_x >>= delta;
            exp_x = exp_y;
        }
    }

    uint16_t result_sign;
    uint32_t result_mantissa;

    if (sign_x == sign_y) {
        result_mantissa = mantissa_x + mantissa_y;
        result_sign = sign_x;
        if (result_mantissa & (1 << (FP16_MANTISSA_BITS + 1))) {
            result_mantissa >>= 1;
            exp_x += 1;
            if (exp_x == FP16_MAX_EXP) {
                return (result_sign << FP16_EXP_BIAS) | FP16_INFINITY;
            }
        }
    } else {
        if (mantissa_x > mantissa_y) {
            result_mantissa = mantissa_x - mantissa_y;
            result_sign = sign_x;
        } else if (mantissa_y > mantissa_x) {
            result_mantissa = mantissa_y - mantissa_x;
            result_sign = sign_y;
        } else {
            return 0;
        }

        while ((result_mantissa & (1 << FP16_MANTISSA_BITS)) == 0 &&
               exp_x > 0) {
            result_mantissa <<= 1;
            exp_x -= 1;
        }
    }

    if (exp_x == 0) {
        result_mantissa >>= 1;
    }

    if (result_mantissa & (1 << (FP16_MANTISSA_BITS + 1))) {
        result_mantissa >>= 1;
        exp_x += 1;
    }

    if (exp_x >= FP16_MAX_EXP) {
        return (result_sign << FP16_EXP_BIAS) | FP16_INFINITY;
    }

    uint16_t result = (result_sign << FP16_EXP_BIAS) |
                      (exp_x << FP16_MANTISSA_BITS) |
                      (result_mantissa & FP16_MANT_MASK);

    if ((result & MAXIMUM) == 0) {
        result = 0;
    }

    return result;
}

int fp16_cmp(uint16_t x, uint16_t y) {
    uint16_t sign_x = FP16_SIGN(x);
    uint16_t sign_y = FP16_SIGN(y);
    uint16_t exp_x = FP16_EXP(x);
    uint16_t exp_y = FP16_EXP(y);
    uint16_t mant_x = FP16_MANT(x);
    uint16_t mant_y = FP16_MANT(y);

    if (exp_x == FP16_MAX_EXP && mant_x != 0) {
        return (exp_y == FP16_MAX_EXP && mant_y != 0) ? 0 : 1;
    }
    if (exp_y == FP16_MAX_EXP && mant_y != 0) {
        return -1;
    }

    if ((x & ~FP16_SIGN_MASK) == 0 && (y & ~FP16_SIGN_MASK) == 0) {
        return 0;
    }

    if (sign_x != sign_y) {
        return sign_x ? -1 : 1;
    }

    int cmp;
    if (exp_x != exp_y) {
        cmp = (exp_x > exp_y) ? 1 : -1;
    } else if (mant_x != mant_y) {
        cmp = (mant_x > mant_y) ? 1 : -1;
    } else {
        cmp = 0;
    }

    return sign_x ? -cmp : cmp;
}