int threshold(VMPM *vmpm) { unsigned int i; if (vmpm->buffer_high) free(vmpm->buffer_high); if ((vmpm->buffer_high = malloc(vmpm->bufferused)) == NULL) return 0; if (vmpm->buffer_low) free(vmpm->buffer_low); if ((vmpm->buffer_low = malloc(vmpm->bufferused)) == NULL) return 0; vmpm->bits_per_symbol = 8 - vmpm->nlowbits; vmpm->alphabetsize = 1 << vmpm->bits_per_symbol; vmpm->buffer_low_size = vmpm->bufferused; for (i = 0; i < vmpm->bufferused; i++) { vmpm->buffer_low[i] = vmpm->buffer[i] & LOW_MASK(vmpm->nlowbits); vmpm->buffer[i] >>= vmpm->nlowbits; vmpm->buffer_high[i] = vmpm->buffer[i]; } return 1; }
REGPARM_ATTR (1) static void cfdiv_r_2exp (mpz_ptr w, mpz_srcptr u, unsigned long cnt, int dir) { mp_size_t usize, abs_usize, limb_cnt, i; mp_srcptr up; mp_ptr wp; mp_limb_t high; usize = SIZ(u); if (usize == 0) { SIZ(w) = 0; return; } limb_cnt = cnt / GMP_NUMB_BITS; cnt %= GMP_NUMB_BITS; abs_usize = ABS (usize); /* MPZ_REALLOC(w) below is only when w!=u, so we can fetch PTR(u) here nice and early */ up = PTR(u); if ((usize ^ dir) < 0) { /* Round towards zero, means just truncate */ if (w == u) { /* if already smaller than limb_cnt then do nothing */ if (abs_usize <= limb_cnt) return; wp = PTR(w); } else { i = MIN (abs_usize, limb_cnt+1); MPZ_REALLOC (w, i); wp = PTR(w); MPN_COPY (wp, up, i); /* if smaller than limb_cnt then only the copy is needed */ if (abs_usize <= limb_cnt) { SIZ(w) = usize; return; } } } else { /* Round away from zero, means twos complement if non-zero */ /* if u!=0 and smaller than divisor, then must negate */ if (abs_usize <= limb_cnt) goto negate; /* if non-zero low limb, then must negate */ for (i = 0; i < limb_cnt; i++) if (up[i] != 0) goto negate; /* if non-zero partial limb, then must negate */ if ((up[limb_cnt] & LOW_MASK (cnt)) != 0) goto negate; /* otherwise low bits of u are zero, so that's the result */ SIZ(w) = 0; return; negate: /* twos complement negation to get 2**cnt-u */ MPZ_REALLOC (w, limb_cnt+1); up = PTR(u); wp = PTR(w); /* Ones complement */ i = MIN (abs_usize, limb_cnt+1); mpn_com_n (wp, up, i); for ( ; i <= limb_cnt; i++) wp[i] = GMP_NUMB_MAX; /* Twos complement. Since u!=0 in the relevant part, the twos complement never gives 0 and a carry, so can use MPN_INCR_U. */ MPN_INCR_U (wp, limb_cnt+1, CNST_LIMB(1)); usize = -usize; } /* Mask the high limb */ high = wp[limb_cnt]; high &= LOW_MASK (cnt); wp[limb_cnt] = high; /* Strip any consequent high zeros */ while (high == 0) { limb_cnt--; if (limb_cnt < 0) { SIZ(w) = 0; return; } high = wp[limb_cnt]; } limb_cnt++; SIZ(w) = (usize >= 0 ? limb_cnt : -limb_cnt); }
uint16_t tile_mask_calculate(uint8_t tl, uint8_t tr, uint8_t bl, uint8_t br) { /* Helpers */ #define TILE_HAS(type) _tile_has_type(tl, tr, bl, br, type) #define LOW_MASK(first) _tile_boundry_low_mask_get(tl, tr, bl, br, first) uint16_t tile = 0x0000; if (tile_solid_is(tl, tr, bl, br)) { switch (tl & 0x0f) /* tl == tr == bl == br */ { case TILE_WATER_LIGHT: tile = 0x0010; break; case TILE_WATER_DARK: tile = 0x0020; break; case TILE_GROUND_LIGHT: tile = 0x0030; break; case TILE_GROUND_DARK: tile = 0x0040; break; case TILE_GRASS_LIGHT: tile = 0x0050; break; case TILE_GRASS_DARK: tile = 0x0060; break; case TILE_TREES: tile = 0x0070; break; case TILE_ROCKS: tile = 0x0080; break; case TILE_HUMAN_WALL: tile = (tl & TILE_WALL_OPEN) ? 0x00b0 : 0x0090; break; case TILE_ORC_WALL: tile = (tl & TILE_WALL_OPEN) ? 0x00c0 : 0x00a0; break; default: CRI("Unhandled solid tile %x", tl); goto fail; } } else { if (TILE_HAS(TILE_GRASS_LIGHT)) { if (TILE_HAS(TILE_TREES)) { tile = 0x0700 | LOW_MASK(TILE_TREES); } else if (TILE_HAS(TILE_GRASS_DARK)) { tile = 0x0600 | LOW_MASK(TILE_GRASS_DARK); } else if (TILE_HAS(TILE_GROUND_LIGHT)) { tile = 0x0500 | LOW_MASK(TILE_GROUND_LIGHT); } else { CRI("Invalid disposition of tiles (with grass light)"); goto fail; } } else if (TILE_HAS(TILE_GROUND_LIGHT)) { if (TILE_HAS(TILE_GROUND_DARK)) { tile = 0x0300 | LOW_MASK(TILE_GROUND_DARK); } else if (TILE_HAS(TILE_ROCKS)) { tile = 0x0400 | LOW_MASK(TILE_ROCKS); } else if (TILE_HAS(TILE_WATER_LIGHT)) { tile = 0x0200 | LOW_MASK(TILE_WATER_LIGHT); } else { CRI("Invalid disposition of tiles (with ground light)"); goto fail; } } else if (TILE_HAS(TILE_WATER_LIGHT)) { if (TILE_HAS(TILE_WATER_DARK)) { tile = 0x0100 | LOW_MASK(TILE_WATER_DARK); } else { CRI("Invalid disposition of tiles (with water light)"); goto fail; } } else if (TILE_HAS(TILE_ORC_WALL)) { tile = 0x0900 | _tile_wall_mask_get(tl, tr, bl, br); } else if (TILE_HAS(TILE_HUMAN_WALL)) { tile = 0x0800 | _tile_wall_mask_get(tl, tr, bl, br); } else { CRI("Uncovered tile disposition"); goto fail; } } return tile; fail: CRI("Analysis of tile (tl, tr, bl, br) = (0x%02x, 0x%02x, 0x%02x, 0x%02x) failed", tl, tr, bl, br); return 0x0000; #undef LOW_MASK #undef TILE_HAS }