[Cfp-interest] an exceptional example

David Hough CFP pcfp at oakapple.net
Tue May 13 15:01:27 PDT 2014


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( d1 / d , FE_INVALID_DIV) { 
				/* d1/d 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];
                        }
                }
                else { /* d1/d is a normal number */
                        f1 = -(d1 / d) * q;
                }
        }


Compare to the conventional code which looks simpler:

                r = d1 / d;     /* CRITICAL STEP */
                if (r != r) { /* 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];
                        }
                }
                else { /* r is a normal number */
                        f1 = -r * q;
                }

but is contaminated by invalid flags or traps that don't affect 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 mind of the compiler
writer rather than in the mind of the application writer.



More information about the Cfp-interest mailing list