home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 010 / pentabug.zip / PENTABUG.C next >
C/C++ Source or Header  |  1994-12-11  |  2KB  |  50 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /* Demonstration of the Pentium Floating-Point Division Bug.          */
  4. /*                                                                    */
  5. /* The program performs the floating-point calculation C = (A/B)*B    */
  6. /* for all values of A and B over a carefully selected limited range. */
  7. /* If A and C differ by more than 1 binary count, the program reports */
  8. /* the error. Printout columns 1,2,3 are A,B,C in decimal. (Notice    */
  9. /* that columns 1 and 3 are unequal.) Columns 4,5,6 are A,B,C in      */
  10. /* hexadecimal. Column 7 is C-A, the error's magnitude.               */
  11. /*                                                                    */
  12. /* The division bug doesn't seem to care about the sign or binary     */
  13. /* exponent, so similar errors occur if you use negative values or    */
  14. /* values scaled by a power of two.                                   */
  15. /*                                                                    */
  16. /* The bug seems to be 100% stable and consistent on 90MHz Pentiums.  */
  17. /* A similar bug exists in double precision division.                 */
  18. /*                                                                    */
  19. /**********************************************************************/
  20.  
  21. #include <stdio.h>
  22.  
  23.  
  24. int main(void)
  25. {
  26.   int mistakes=0;
  27.   float a, b, c;
  28.   #define i (*(unsigned long*)&a)    /* access the floats as ulongs */
  29.   #define j (*(unsigned long*)&b)
  30.   #define k (*(unsigned long*)&c)
  31.  
  32.   printf("\nChecking for Pentium floating-point division bug...\n");
  33.   for (i=0x40000000L; i<=0x40001000L; i++)   /* Don't care bits: 0xFF800000 */
  34.     for (j=0x403FFFC0L; j<=0x40400000L; j++) /* Don't care bits: 0xFF800000 */
  35.     {
  36.       c = a/b;
  37.       c *= b;
  38.       if ((long)(i-k)<-1 || (long)(i-k)>1)
  39.       {
  40.         printf("  %.7f / %.7f : %.7f    %08lX / %08lX : %08lX (%+ld)\n", a, b, c, i, j, k, k-i);
  41.         mistakes++;
  42.       }
  43.     }
  44.   if (mistakes)
  45.     printf("%d mistakes. You have a genuine Intel Pentium CPU.\n", mistakes);
  46.   else
  47.     printf("No mistakes.\n");
  48.   return 0;
  49. }
  50.