kit

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

6_8_29_switch_signed_negative.c (844B)


      1 /* Signed selector with negative case values. A dense range around zero
      2  * (-3..3) exercises the (sel - vmin) compute in a jump-table lowering
      3  * where vmin is negative; the bounds check must use unsigned compare or
      4  * sign-extension correctly. */
      5 static int pick(int x) {
      6   switch (x) {
      7     case -3:
      8       return 30;
      9     case -2:
     10       return 31;
     11     case -1:
     12       return 32;
     13     case 0:
     14       return 33;
     15     case 1:
     16       return 34;
     17     case 2:
     18       return 35;
     19     case 3:
     20       return 36;
     21     default:
     22       return 99;
     23   }
     24 }
     25 
     26 int test_main(void) {
     27   int s = 0;
     28   s += pick(-3);  /* vmin           -> 30 */
     29   s += pick(3);   /* vmax           -> 36 */
     30   s += pick(0);   /* zero           -> 33 */
     31   s += pick(-4);  /* below vmin     -> 99 */
     32   s += pick(4);   /* above vmax     -> 99 */
     33   return s - 197; /* 30+36+33+99+99 - 197 = 100 */
     34 }