[Cfp-interest] revised exception handling example

David Hough CFP pcfp at oakapple.net
Wed May 21 16:44:01 PDT 2014


this is revised to match the revised directives.tx that follows in the next
email:

FE_EXCEPTIONAL example


        for (j = N - 1; j >= 0; j--) {
                f1j2 = f1j1;    /* Save this guy for recomputation -
                        won't be defined until j=N-2 but that's the
                        earliest it will be needed.. */
                f1j1 = f1;      /* Intermediate save. */
                d = x + f;
                q = b[j] / d;
                f = a[j] + q;
                d1 = 1.0 + f1;
	/* CRITICAL STEP */
                if (FE_EXCEPTIONAL( r = d1 / d , FE_INVALID_DIV) == 0) { 
                	/* r is a normal number */
                        f1 = -r * q;
                } else {
			/* r is NaN from 0/0 or inf/inf */
                        if (d1 == 0) {
                                /* f1 = (0/0)*inf so return infinity */
                                f1 = q;
                        } else {
                                /* f1 = (inf/inf)*0 so return limit value */
                                f1 = (1.0 + f1j2) * b[j + 2] / b[j + 1];
                        }
                }
        }

Note that FP_EXCEPTIONAL could be DETERMINISTIC (delayed) or
NONDETERMINISTIC (immediate) because the exception handling case does
not require any intermediate or final result in the test expression... 
there's just one intermediate or final result in this case: r.

Compare to the conventional code which looks simpler:

	/* CRITICAL STEP */
                r = d1 / d;     
                if (r == r) { 
			/* r is a normal number */
                        f1 = -r * q;
                } else { 
			/* r is NaN from 0/0 or inf/inf */
                        if (d1 == 0) {
                                /* f1 = (0/0)*inf so return infinity */
                                f1 = q;
                        } else {
                                /* f1 = (inf/inf)*0 so return limit value */
                                f1 = (1.0 + f1j2) * b[j + 2] / b[j + 1];
                        }
                }

which depends on an optimizer not optimizing away r==r to true -
otherwise one must invoke isnan(r) and hope for efficient inline code.
Worse, this conventional code's output
is contaminated by invalid flags or traps that don't reflect the
validity of the final result.    Put in code to eliminate those and it
doesn't look so simple, in fact the important part is buried under the
mechanism.      We want to bury the mechanism in the minds of a few compiler
writers rather than in the minds of legion application writers.



More information about the Cfp-interest mailing list