static int compare()

in math-emu/reg_compare.c [24:167]


static int compare(FPU_REG const *b, int tagb)
{
	int diff, exp0, expb;
	u_char st0_tag;
	FPU_REG *st0_ptr;
	FPU_REG x, y;
	u_char st0_sign, signb = getsign(b);

	st0_ptr = &st(0);
	st0_tag = FPU_gettag0();
	st0_sign = getsign(st0_ptr);

	if (tagb == TAG_Special)
		tagb = FPU_Special(b);
	if (st0_tag == TAG_Special)
		st0_tag = FPU_Special(st0_ptr);

	if (((st0_tag != TAG_Valid) && (st0_tag != TW_Denormal))
	    || ((tagb != TAG_Valid) && (tagb != TW_Denormal))) {
		if (st0_tag == TAG_Zero) {
			if (tagb == TAG_Zero)
				return COMP_A_eq_B;
			if (tagb == TAG_Valid)
				return ((signb ==
					 SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B);
			if (tagb == TW_Denormal)
				return ((signb ==
					 SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
				    | COMP_Denormal;
		} else if (tagb == TAG_Zero) {
			if (st0_tag == TAG_Valid)
				return ((st0_sign ==
					 SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
			if (st0_tag == TW_Denormal)
				return ((st0_sign ==
					 SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
				    | COMP_Denormal;
		}

		if (st0_tag == TW_Infinity) {
			if ((tagb == TAG_Valid) || (tagb == TAG_Zero))
				return ((st0_sign ==
					 SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
			else if (tagb == TW_Denormal)
				return ((st0_sign ==
					 SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
				    | COMP_Denormal;
			else if (tagb == TW_Infinity) {
				/* The 80486 book says that infinities can be equal! */
				return (st0_sign == signb) ? COMP_A_eq_B :
				    ((st0_sign ==
				      SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
			}
			/* Fall through to the NaN code */
		} else if (tagb == TW_Infinity) {
			if ((st0_tag == TAG_Valid) || (st0_tag == TAG_Zero))
				return ((signb ==
					 SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B);
			if (st0_tag == TW_Denormal)
				return ((signb ==
					 SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
				    | COMP_Denormal;
			/* Fall through to the NaN code */
		}

		/* The only possibility now should be that one of the arguments
		   is a NaN */
		if ((st0_tag == TW_NaN) || (tagb == TW_NaN)) {
			int signalling = 0, unsupported = 0;
			if (st0_tag == TW_NaN) {
				signalling =
				    (st0_ptr->sigh & 0xc0000000) == 0x80000000;
				unsupported = !((exponent(st0_ptr) == EXP_OVER)
						&& (st0_ptr->
						    sigh & 0x80000000));
			}
			if (tagb == TW_NaN) {
				signalling |=
				    (b->sigh & 0xc0000000) == 0x80000000;
				unsupported |= !((exponent(b) == EXP_OVER)
						 && (b->sigh & 0x80000000));
			}
			if (signalling || unsupported)
				return COMP_No_Comp | COMP_SNaN | COMP_NaN;
			else
				/* Neither is a signaling NaN */
				return COMP_No_Comp | COMP_NaN;
		}

		EXCEPTION(EX_Invalid);
	}

	if (st0_sign != signb) {
		return ((st0_sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
		    | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
		       COMP_Denormal : 0);
	}

	if ((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) {
		FPU_to_exp16(st0_ptr, &x);
		FPU_to_exp16(b, &y);
		st0_ptr = &x;
		b = &y;
		exp0 = exponent16(st0_ptr);
		expb = exponent16(b);
	} else {
		exp0 = exponent(st0_ptr);
		expb = exponent(b);
	}

#ifdef PARANOID
	if (!(st0_ptr->sigh & 0x80000000))
		EXCEPTION(EX_Invalid);
	if (!(b->sigh & 0x80000000))
		EXCEPTION(EX_Invalid);
#endif /* PARANOID */

	diff = exp0 - expb;
	if (diff == 0) {
		diff = st0_ptr->sigh - b->sigh;	/* Works only if ms bits are
						   identical */
		if (diff == 0) {
			diff = st0_ptr->sigl > b->sigl;
			if (diff == 0)
				diff = -(st0_ptr->sigl < b->sigl);
		}
	}

	if (diff > 0) {
		return ((st0_sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B)
		    | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
		       COMP_Denormal : 0);
	}
	if (diff < 0) {
		return ((st0_sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B)
		    | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
		       COMP_Denormal : 0);
	}

	return COMP_A_eq_B
	    | (((st0_tag == TW_Denormal) || (tagb == TW_Denormal)) ?
	       COMP_Denormal : 0);

}