kit

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

conv_intwiden_s.c (1087B)


      1 /* Signed integer widening: char/short -> int -> long via sign-extension.
      2  * Negative source values exercise the sign bits so SXTB/SXTH/SXTW (sbfm
      3  * aliases) are genuinely required, not a zero/move. volatile sources defeat
      4  * folding so the extends are really emitted.
      5  *
      6  *   sc = -2  : (int)sc      = -2          (sxtb w)
      7  *   ss = -3  : (int)ss      = -3          (sxth w)
      8  *   si = -4  : (long)si     = -4          (sxtw x, sxtw alias of sbfm x)
      9  *   also char->long directly (sxtb into x).
     10  * Sum the absolute contributions back up to 42. */
     11 int test_main(void) {
     12   volatile signed char sc = -2;
     13   volatile short ss = -3;
     14   volatile int si = -4;
     15   int a = (int)sc;                                      /* -2,  sxtb */
     16   int b = (int)ss;                                      /* -3,  sxth */
     17   long c = (long)si;                                    /* -4,  sxtw */
     18   long d = (long)sc;                                    /* -2,  sxtb -> x */
     19   long acc = -(long)a + -(long)b + -(long)c + -(long)d; /* 2+3+4+2 = 11 */
     20   long base = 31;
     21   return (int)(acc + base); /* 11 + 31 = 42 */
     22 }