kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

iarith_long.c (1059B)


      1 /* iarith: 64-bit (long) add/sub/mul/sdiv/udiv/rem.
      2  *
      3  * volatile longs force the 64-bit `x`-register forms: `add x`/`sub x`/`mul x`,
      4  * `sdiv x`/`udiv x` and the `msub x` remainder. Values exceed 32 bits so the
      5  * computation genuinely needs 64-bit registers.
      6  *
      7  *   p   = 0x1_0000_0007 * 3          = 0x3_0000_0015
      8  *   p  += 0x2_0000_0000              = 0x5_0000_0015
      9  *   q   = p / 0x1_0000_0000 (=5)
     10  *   r   = p % 0x1_0000_0000 (=0x15=21)
     11  *   sub = q*0x100000000 ... avoided; keep it simple below. */
     12 int test_main(void) {
     13   volatile long a = 0x100000007L, b = 3, c = 0x200000000L, d = 0x100000000L;
     14   long p = a * b;                                         /* 0x300000015 */
     15   p = p + c;                                              /* 0x500000015 */
     16   long q = p / d;                                         /* 5 */
     17   long r = p % d;                                         /* 0x15 = 21 */
     18   unsigned long uq = (unsigned long)p / (unsigned long)d; /* 5, udiv x */
     19   long res = q + r + (long)uq + 11; /* 5 + 21 + 5 + 11 = 42 */
     20   return (int)res;
     21 }