Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: code generation bug? multiply affects later shift? (Teensy 4.0) can loop?

Threaded View

  1. #1
    Senior Member
    Join Date
    Jul 2020
    Posts
    384

    code generation bug? multiply affects later shift? can loop? (Teensy 4.0)

    Board Teensy 4.0
    System MacOS 10.15.5 / Macbook Pro
    Arduino 1.8.12 / Teensyduino 1.52

    I whittled down to a simpler testcase:
    Code:
    #define START 4  // values less than 4 work, more than 4 hang after first line of output
    #define END 8
    
    
    void setup() 
    {
      Serial.begin (115200) ;
      for (int n = START ; n <= END ; n++)
      {
        // value of 1<<n prints correct here:
        Serial.print (n) ; Serial.print (" shifted=") ; Serial.print (1<<n) ;  Serial.print (" ... ") ;
        int foo;
        for (int i = 0 ; i < (1<<n) ; i++)
        {
          foo = i * 0x08000000 ;  
        }
       // The value of 1<<n is printed as 16 whatever n is at this point
       Serial.print (n) ; Serial.print (" shifted=") ; Serial.print (1<<n) ; Serial.print ("   foo=0x") ; Serial.println (foo, HEX) ;
       delay (50) ;
      }
    }
    
    void loop() 
    {
    }
    The output is
    Code:
    4 shifted=16 ... 4 shifted=16   foo=0x78000000
    5 shifted=32 ... 5 shifted=16   foo=0xF8000000
    6 shifted=64 ... 6 shifted=16   foo=0xF8000000
    7 shifted=128 ... 7 shifted=16   foo=0xF8000000
    8 shifted=256 ... 8 shifted=16   foo=0xF8000000
    So after the crucial multiplication(s) in the loop the attempt to do a left shift of 1 by n always seems to produce 16.
    The multiplication seems to have to overflow (perhaps in a specific way) to trigger the issue. This feels like a
    code-generator bug to do with modelling the kill set of such instructions.

    If START is changed to less than 4, it seems to work OK though:
    Code:
    3 shifted=8 ... 3 shifted=8   foo=0x38000000
    4 shifted=16 ... 4 shifted=16   foo=0x78000000
    5 shifted=32 ... 5 shifted=32   foo=0xF8000000
    6 shifted=64 ... 6 shifted=64   foo=0xF8000000
    7 shifted=128 ... 7 shifted=128   foo=0xF8000000
    8 shifted=256 ... 8 shifted=256   foo=0xF8000000
    Which is very counter-intuitive

    Or if START is more than 4 it appears to jam the processor:
    Code:
    5 shifted=32 ...
    Last edited by MarkT; 07-04-2020 at 11:48 PM. Reason: Improve subject line

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •