9 #define BAIL (1 << (BITS + 1))
 
  10 #define BAIL_A (1 << BITS)
 
  13 typedef unsigned short num_t;
 
  25 static inline cplx_t convert(num_t x, num_t y)
 
  28         r.mag = x > MAXNUM ? -x : x;
 
  29         r.sig = x > MAXNUM ? 1 : 0;
 
  30         i.mag = y > MAXNUM ? -y : y;
 
  31         i.sig = y > MAXNUM ? 1 : 0;
 
  33 //      printf("convert: output: x = %d, y = %d\n", res.r.mag, res.i.mag);
 
  37 static inline sigmag_t mul(sigmag_t x, sigmag_t y)
 
  42                     (ym & 0x2000 ? xm       : 0) +
 
  43                     (ym & 0x1000 ? xm >> 1  : 0) +
 
  44                     (ym & 0x0800 ? xm >> 2  : 0) +
 
  45                     (ym & 0x0400 ? xm >> 3  : 0) +
 
  46                     (ym & 0x0200 ? xm >> 4  : 0) +
 
  47                     (ym & 0x0100 ? xm >> 5  : 0) +
 
  48                     (ym & 0x0080 ? xm >> 6  : 0) +
 
  49                     (ym & 0x0040 ? xm >> 7  : 0) +
 
  50                     (ym & 0x0020 ? xm >> 8  : 0) +
 
  51                     (ym & 0x0010 ? xm >> 9  : 0) +
 
  52                     (ym & 0x0008 ? xm >> 10 : 0) +
 
  53                     (ym & 0x0004 ? xm >> 11 : 0) +
 
  54                     (ym & 0x0002 ? xm >> 12 : 0) +
 
  55                     (ym & 0x0001 ? xm >> 13 : 0);
 
  57         num_t sig = x.sig ^ y.sig;
 
  58         sigmag_t res = {mag, sig};
 
  62 static inline unsigned int calc_piqsl(num_t x, num_t y)
 
  64 //      printf("calc_piqsl: x = %d, y = %d ############################ \n", x, y);
 
  66         cplx_t c = convert(x, y);
 
  68 //      printf("xmag = %d, xsig = %d, ymag = %d, ysig = %d\n", c.r.mag, c.r.sig, c.i.mag, c.i.sig);
 
  69         for(i = 0; i < MAX_ITER; i++) {
 
  70                 sigmag_t r2 = mul(z.r, z.r);
 
  71                 sigmag_t i2 = mul(z.i, z.i);
 
  72                 sigmag_t ri = mul(z.r, z.i);
 
  73 //              printf("r2mag: %d  i2mag: %d  i: %d\n", r2.mag, i2.mag, i);
 
  74                 if((r2.mag + i2.mag) & BAIL || z.r.mag & BAIL   || z.i.mag & BAIL 
 
  75                                             || z.r.mag & BAIL_A || z.i.mag & BAIL_A) {
 
  78                 num_t rmag = r2.mag - i2.mag + (c.r.sig ? -c.r.mag : c.r.mag);
 
  79                 num_t imag = (ri.sig ? -ri.mag << 1 : ri.mag << 1) + (c.i.sig ? -c.i.mag : c.i.mag);
 
  80                 z = convert(rmag, imag);
 
  83 //      printf("bailed at %d\n", i);
 
  84         return (i == MAX_ITER ? 0 : i);
 
  94         SDL_LockSurface(screen);
 
  97         for(i = 0; i < XRES; i++) {
 
  98                 for(j = 0; j <3 *dick; j++) {
 
  99                         res = calc_piqsl((i - XRES/2) << zoom, (j - YRES/2) << zoom);
 
 100                         res = res == 0 ? 0 : MAX_ITER - res - 3;
 
 101                         p[i+j*XRES] = ((res & 0x1) << 23) | ((res & 0x8)  << 18) | ((res & 0x40) << 13) |
 
 102                                       ((res & 0x2) << 14) | ((res & 0x10) <<  9) | ((res & 0x80) << 4) |
 
 103                                       ((res & 0x4) <<  5) | ((res & 0x20));
 
 108         SDL_UnlockSurface(screen);
 
 116         if(SDL_Init(SDL_INIT_VIDEO) < 0) {
 
 117                 printf("Video init failed.\n");
 
 121         screen = SDL_SetVideoMode(XRES, YRES, 32, SDL_SWSURFACE);
 
 123                 printf("Screen init failed.\n");
 
 129         while(SDL_WaitEvent(&ev)) {
 
 131                 case SDL_QUIT: exit(0);