
#include "first0.h"  /* LOOKUP_FIRSTBLANK */

#define FIRST0_FFFFFFFE /* Disable non-FFFFFFFE for now, as it seems to be slower!?!!  */

#define SETUP_BITMAPS							\
uint32_t list[MAXMARKS][NUMWORDS]/*={0}*/;   /* list bitmap */		\
uint32_t dist[MAXMARKS][NUMWORDS]/*={0}*/;   /* dist bitmap */		\
uint32_t comp[MAXMARKS][NUMWORDS]/*={0}*/;   /* dist bitmap */		\
uint32_t newbit=1;

#ifdef FIRST0_FFFFFFFE

#define SET_NEXT_MARK							\
if (comp[depth][0] < 0xfffffffe) {					\
    int s = LOOKUP_FIRSTBLANK( comp[depth][0] );			\
        if ((count[depth] += s) > limit[depth]) {break;} /* no space */	\
        else {								\
            CLLR_XX_BITMAPS;						\
        }								\
} else { /* s>=32 */							\
    if ((count[depth] += 32) > limit[depth]) {break;} /* no space */	\
    else {								\
        uint32_t temp = comp[depth][0];					\
        CLLR_32_BITMAPS;						\
        if (temp == 0xffffffff) goto stay;				\
    }									\
}

#else

#define SET_NEXT_MARK							\
while (comp[depth][0] == 0xffffffff) {					\
    if ((count[depth] += 32) > limit[depth]) {break;} /* no space */	\
    else {								\
        CLLR_32_BITMAPS;						\
    }									\
}	/*this can lead to shifting by 32 which is undefined in C*/	\
int s = LOOKUP_FIRSTBLANK( comp[depth][0] );				\
if ((count[depth] += s) > limit[depth]) {break;} /* no space */		\
else {									\
    CLLR_XX_BITMAPS;							\
}

#endif

#define UP_BITMAPS							\
newbit=0;

#define GET_DIST_32							\
((uint32_t)dist[depth][0])

#define GET_COMP_32							\
((uint32_t)comp[depth][0])

#define GET_LIST_32							\
((uint32_t)list[depth][0])


#if (NUMWORDS==2)

#define INTITIALIZE_BITMAPS						\
list[depth][0]=0;							\
list[depth][1]=0;							\
dist[depth][0]=0;							\
dist[depth][1]=0;							\
comp[depth][0]=0;							\
comp[depth][1]=0;

#define CLLR_XX_BITMAPS							\
    uint32_t temp0, temp1;						\
    int ss=32-s;							\
    temp0 = (list[depth][0]) << ss; list[depth][0] = (list[depth][0] >> s) | (newbit << ss);	\
    /*temp1 = (list[depth][1]) << ss;*/ list[depth][1] = (list[depth][1] >> s) | temp0;		\
    temp1 = (comp[depth][1]) >> ss; comp[depth][1] = (comp[depth][1] << s)/* | temp0*/;		\
    comp[depth][0] = (comp[depth][0] << s) | temp1;

#define CLLR_32_BITMAPS							\
    comp[depth][0] = comp[depth][1];					\
    comp[depth][1] = 0;							\
    list[depth][1] = list[depth][0];					\
    list[depth][0] = newbit;						\
    newbit = 0;

#define DEEPER_BITMAPS							\
/*#define depthp1 depth+1*/						\
int depthp1 = depth+1;							\
list[depthp1][0] = list[depth][0];					\
list[depthp1][1] = list[depth][1];					\
dist[depthp1][0] = list[depthp1][0] | dist[depth][0];			\
dist[depthp1][1] = list[depthp1][1] | dist[depth][1];			\
comp[depthp1][0] = dist[depthp1][0] | comp[depth][0];			\
comp[depthp1][1] = dist[depthp1][1] | comp[depth][1];			\
newbit=1;

#define DEEPER_BITMAPS_FIRST_WORD					\
list[depth][0] = list[depthm1][0];					\
dist[depth][0] = list[depth][0] | dist[depthm1][0];			\
comp[depth][0] = dist[depth][0] | comp[depthm1][0];

#define DEEPER_BITMAPS_REST						\
list[depth][1] = list[depthm1][1];					\
dist[depth][1] = list[depth][1] | dist[depthm1][1];			\
comp[depth][1] = dist[depth][1] | comp[depthm1][1];			\
newbit=1;

#elif (NUMWORDS==3)

#define INTITIALIZE_BITMAPS						\
list[depth][0]=0;							\
list[depth][1]=0;							\
list[depth][2]=0;							\
dist[depth][0]=0;							\
dist[depth][1]=0;							\
dist[depth][2]=0;							\
comp[depth][0]=0;							\
comp[depth][1]=0;							\
comp[depth][2]=0;

#define CLLR_XX_BITMAPS							\
uint32_t temp0, temp1;							\
int ss=32-s;								\
temp0 = (list[depth][0]) << ss; list[depth][0] = (list[depth][0] >> s) | (newbit << ss);	\
temp1 = (list[depth][1]) << ss; list[depth][1] = (list[depth][1] >> s) | temp0;			\
/*temp0 = (list[depth][2]) << ss;*/ list[depth][2] = (list[depth][2] >> s) | temp1;		\
temp0 = (comp[depth][2]) >> ss; comp[depth][2] = (comp[depth][2] << s)/* | temp1*/;		\
temp1 = (comp[depth][1]) >> ss; comp[depth][1] = (comp[depth][1] << s) | temp0;			\
comp[depth][0] = (comp[depth][0] << s) | temp1;


#define CLLR_32_BITMAPS							\
comp[depth][0] = comp[depth][1];					\
comp[depth][1] = comp[depth][2];					\
comp[depth][2] = 0;							\
list[depth][2] = list[depth][1];					\
list[depth][1] = list[depth][0];					\
list[depth][0] = newbit;						\
newbit = 0;

#define DEEPER_BITMAPS							\
/*#define depthp1 depth+1*/						\
int depthp1 = depth+1;							\
list[depthp1][0] = list[depth][0];					\
list[depthp1][1] = list[depth][1];					\
list[depthp1][2] = list[depth][2];					\
dist[depthp1][0] = list[depthp1][0] | dist[depth][0];			\
dist[depthp1][1] = list[depthp1][1] | dist[depth][1];			\
dist[depthp1][2] = list[depthp1][2] | dist[depth][2];			\
comp[depthp1][0] = dist[depthp1][0] | comp[depth][0];			\
comp[depthp1][1] = dist[depthp1][1] | comp[depth][1];			\
comp[depthp1][2] = dist[depthp1][2] | comp[depth][2];			\
newbit=1;

#define DEEPER_BITMAPS_FIRST_WORD					\
list[depth][0] = list[depthm1][0];					\
dist[depth][0] = list[depth][0] | dist[depthm1][0];			\
comp[depth][0] = dist[depth][0] | comp[depthm1][0];

#define DEEPER_BITMAPS_REST						\
list[depth][1] = list[depthm1][1];					\
list[depth][2] = list[depthm1][2];					\
dist[depth][1] = list[depth][1] | dist[depthm1][1];			\
dist[depth][2] = list[depth][2] | dist[depthm1][2];			\
comp[depth][1] = dist[depth][1] | comp[depthm1][1];			\
comp[depth][2] = dist[depth][2] | comp[depthm1][2];			\
newbit=1;

#else

#error no bitmaps defined in bitmaps32.h for your configuration of NUMWORDS

#endif

