Thread: Portability question: int32s wrapping around?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Portability question: int32s wrapping around?

    I'm writing a program that translates code written in another programming language into C.
    I've found out that I can optimize the outputted code in this way:

    Code:
    long a, b, c, d, e;
    
    a = b * c + d * e
    Thing is, what if b * c is a positive value that doesn't fit in a long, and d * e is a negative value, that when added with b * c produces a value that fits in a long (ie it produces valid output). On Intel processors b * c would "wrap", and when I then add d * e to it, it would "wrap back", producing the same output as if I had written a = (long) ((float) b * (float) c + (float) d * (float) e). But I don't know how processors of other brands would react to this. Would they produce the same output?

    Regards, Alexander Toresson

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Would they produce the same output?
    There's not guarantee of anything.

    C only defines wraparound for the unsigned types (incrementing an unsigned value with all 1's set gets you back to all 0's set)

    Some processors can (and will) throw hardware exceptions if you try what you are proposing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Long Question about Portability
    By kinghajj in forum C Programming
    Replies: 1
    Last Post: 05-16-2004, 06:55 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM