Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a05cc3a49a1bd2e81fb6274af755f3fc40a8e264
Ticket: 402781ffffffffffffffffffffffffffffffffff
Fix for Bug 121072
User & Date: dgp on 2000-12-13 06:25:23
Changes

  1. icomment:
    Following up on Jeff's claim that this bug is not
    present on all 64-bit systems after all, I built
    Tcl on an Alpha using both the Gnu C compiler, and
    the Compaq C Compiler
    
    http://www5.compaq.com/products/software/compilers/candcxx.html
    
    Sure enough, the Compaq C Compiler produces a Tcl
    library that does not exhibit Bug 121072.  The Tcl
    library compiled by the GNU C Compiler does suffer
    from Bug 121072.
    
    The difference comes down to the compilers' interpretation
    of line 4099 of revision 1.17 of generic/tclExecute.c.
    That line includes the expression
    
    iPtr->randSeed - tmp*RAND_IQ
    
    where iPtr->randSeed is a long, tmp is an int, and 
    RAND_IQ is the literal 127773.  The Compaq compiler
    promotes all three quantities to longs before computing
    the expression.  The GNU compiler, however, sees the
    product of tmp (an int) and 127773 (an int constant),
    and computes the multiply using ints, which can overflow
    (and does overflow in the Bug 121072 example).  This
    difference causes the two compilers to produce code
    that compute different results.
    
    The strange thing is that by my reading of K&R's ANSI
    C book, it is the GNU Compiler that is behaving correctly -- the product of two ints should be stored in an int -- and Tcl's code is depending on the broken behavior of the Compaq compiler (and others?).  A colleague reports that SGI's
    C compiler with the -64 option also works correctly, so a Tcl library built on SGI with the -64 option also exhibits Bug 121072.
    
    Isn't it fun working around compiler bugs!  :( [**]
    
    The tiniest fix for Bug 121072 is to declare tmp to be
    of type long, rather than type int, and leave everything
    else alone.  That will fix the problem.  So, if you prefer
    a minimum-code-changes approach, we could just do that.
    
    However, Patch 102781 provides a more substantial rewrite.
    It avoids the overflow problem entirely by keeping Tcl's
    implementation within the constraints of the algorithm
    described in the Press & Teukolsky reference.  After all,
    the P&T algortihm was especially written to avoid
    overflows of 32-bit registers.  Because Patch 102781 is a more faithful implementation of P&T's algortihm, it is also able to completely dispense with the bizarre [*] incantation currently in place to perform bit-masking across many compilers and platforms.  IMHO, the  additional comments also do an improved job of explaining what is going on.
    
    Another reason Patch 102781 is better is that it is
    future-proof against a world where ints are 64 bits, and longs are even longer.  The P&T algorithm makes the specific assumption of a 32 bit register.  Patch 102781 properly masks seed values to 31 bits before the recurrence begins, so this assumption is enforced.
    
    If we assume that Tcl will never be built on a platform
    with ints smaller than 32 bits, then the promotion of the variable tmp to type long in Patch 102781 is unnecessary.  It was added simply because I was already modifying the code, and it seemed a good opportunity to better conform to ANSI standards.  It is not essential to proper working of
    Patch 102781.  But it doesn't hurt, and it might even help someone someday trying to port Tcl to an exotic platform, so I'd prefer to keep it in.
    
    [*] Not my description, that's straight from a comment
    already in the code.
    
    [**] Try this test program to see if your compiler is
    broken.  It should print the same value twice:
    
    #include <stdio.h>
    
    #define Q 127773
    
    int main(int argc, char **argv)
    {
    long l = 1L << 37;
    int tmp = l / Q;
    int i = tmp*Q;
    
    fprintf(stderr, "d   = %ld\n", l - tmp*Q);
    fprintf(stderr, "l-i = %ld\n", l - i);
    }
    
    
    
  2. login: "dgp"
  3. mimetype: "text/plain"
  4. username: "dgp"