title: How do I correctly use ADDIS and ADDI to sum a 32 bit constant directly to a register?
body: <p>I have been trying to use the ADDIS and ADDI instructions for a basic function that sums constants to an array, I followed the instructions in pages 48 and 49 of <a href="https://cr.yp.to/2005-590/powerpc-cwg.pdf" rel="nofollow noreferrer">https://cr.yp.to/2005-590/powerpc-cwg.pdf</a>, but the behavior isn't what I expected.</p>
  
  <p>What I'm trying to do is create this function in assembler, but for certain constants ADDIS decrements them by 1, and the behavior is not consistent with what the manual says, that is checking if bit 16 is set.
  GCC and clang generate the instructions correctly, what is the actual rule they are using?
  Not doing the incrementing or implementing the rule as the manual says just gives me wrong sums.</p>
  
  <p>Tested in a ppc970 machine, running a 64 bit linux kernel and userspace.</p>
  
  <p>Example of compiler assembler output from C function:</p>
  
  <pre><code>void func(int* r){
  r[0] += r[0] + 0x9f321062; //addis r3,r3,0x9f32 #has bit 16 set to 1 but is not incremented.
                             //addi  r3,r3,0x1062
  
  r[1] += r[1] + 0x760ae53;  //addis r4,r4,0x761  #compiler correctly increments this from 0x0760 to 0x0761, but bit 16 of this number is 0!
                             //addi r4,r4,0xae53
  }
  </code></pre>