pointer aliasing

This is a discussion on pointer aliasing within the C Programming forums, part of the General Programming Boards category; So my current project which I'd been compiling so far with no -O flag, breaks some things on-O2 (although it's ...

  1. #1
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    pointer aliasing

    So my current project which I'd been compiling so far with no -O flag, breaks some things on-O2 (although it's spiffy faster). I've been scrounging around a little bit and read up on the evilness of modifying something with a pointer of a different type and how it can break optimizations, particularly in GCC.

    Code:
      int rdhi, rdlo, rs, rm;
      u32 regs[16];
    
      ...
    
      u64 result = regs[rm] * (u64) regs[rs];
      result += regs[rdlo];
      result += (u64) regs[rdhi] << 32;
      regs[rdhi] = ((u32 *) (&result))[1];
      regs[rdlo] = ((u32 *) (&result))[0];
    Do you think this particular code could break optimizations. Even though I am using a u32* to point to a u64, I'm only using it to read, not modify, and the u32* is not a "mysterious pointer" pointing to an unknown place - it's made directly from &result and dereferenced in the same statement. So it doesn't seem to me to fit the mold of aliasing that breaks optimization - where there's a mysterious pointer floating around that the compiler can have no idea where it points to, and it uses the same type rule to limit the number of options.
    hello, internet!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > regs[rdhi] = ((u32 *) (&result))[1];
    > regs[rdlo] = ((u32 *) (&result))[0];

    I don't have an answer to your question. But have you tried making these:
    Code:
      regs[rdhi] = result >> 32;
      regs[rdlo] = result;

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by swoopy
    > regs[rdhi] = ((u32 *) (&result))[1];
    > regs[rdlo] = ((u32 *) (&result))[0];

    I don't have an answer to your question. But have you tried making these:
    Code:
      regs[rdhi] = result >> 32;
      regs[rdlo] = result;
    Yeah, that should work; I'll probably switch to it... in GCC I could also use the union hack

    Code:
    union
    {
      u64 big;
      u32 small[2];
    } foo;
    It's more a question of correctness, to increase my knowledge, than anything else. Unfortunately I only use this "trick" in the various 64 bit multiply routines, so most of my problems from -O2 must be coming from somewhere else... but that will have to wait until I've had more time to narrow things down.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21