#include <stdio.h>
#include <stdint.h>
#include <assert.h>

// return x*2^p for p = -31..31
int32_t shiftS32(int32_t x, int32_t p)
{
  return (p >= 0) ? (x << p) :
    ((int32_t)(x + ((uint32_t)(-(x < 0)) >> (p + 32))) >> -p);
}

int main(void)
{
  for (int32_t x = -10; x < 10; x++)
    for (int32_t p = -4; p <= 4; p++)
    {
      int32_t r = shiftS32(x, p);
      printf("shiftS32(%ld, %ld) = %ld\n", (long)x, (long)p, (long)r);
      if (p < 0)
      {
        if (x >= 0) { assert(r == (x >> -p)); }
        else { assert(r == -(-x >> -p)); }
      }
    }
  return 0;
}
