require '_h2ph_pre.ph';

no warnings 'redefine';

unless(defined(&_BITSTRING_H_)) {
    eval 'sub _BITSTRING_H_ () {1;}' unless defined(&_BITSTRING_H_);
    eval 'sub _bit_byte {
        my($bit) = @_;
	    eval q((($bit) >> 3));
    }' unless defined(&_bit_byte);
    eval 'sub _bit_mask {
        my($bit) = @_;
	    eval q((1<< (($bit)&0x7)));
    }' unless defined(&_bit_mask);
    eval 'sub bitstr_size {
        my($nbits) = @_;
	    eval q(((($nbits) + 7) >> 3));
    }' unless defined(&bitstr_size);
    eval 'sub bit_alloc {
        my($nbits) = @_;
	    eval q(( &bitstr_t *) &calloc( &bitstr_size($nbits), $sizeof{ &bitstr_t}));
    }' unless defined(&bit_alloc);
    eval 'sub bit_decl {
        my($name, $nbits) = @_;
	    eval q((($name)[ &bitstr_size($nbits)]));
    }' unless defined(&bit_decl);
    eval 'sub bit_test {
        my($name, $bit) = @_;
	    eval q((($name)[ &_bit_byte($bit)] &  &_bit_mask($bit)));
    }' unless defined(&bit_test);
    eval 'sub bit_set {
        my($name, $bit) = @_;
	    eval q((($name)[ &_bit_byte($bit)] |=  &_bit_mask($bit)));
    }' unless defined(&bit_set);
    eval 'sub bit_clear {
        my($name, $bit) = @_;
	    eval q((($name)[ &_bit_byte($bit)] &= ~ &_bit_mask($bit)));
    }' unless defined(&bit_clear);
    eval 'sub bit_nclear {
        my($name, $start, $stop) = @_;
	    eval q( &do {  &register  &bitstr_t * &_name = $name;  &register \'int\'  &_start = $start,  &_stop = $stop;  &while ( &_start <=  &_stop) {  &bit_clear( &_name,  &_start);  &_start++; } }  &while(0));
    }' unless defined(&bit_nclear);
    eval 'sub bit_nset {
        my($name, $start, $stop) = @_;
	    eval q( &do {  &register  &bitstr_t * &_name = $name;  &register \'int\'  &_start = $start,  &_stop = $stop;  &while ( &_start <=  &_stop) {  &bit_set( &_name,  &_start);  &_start++; } }  &while(0));
    }' unless defined(&bit_nset);
    eval 'sub bit_ffc {
        my($name, $nbits, $value) = @_;
	    eval q( &do {  &register  &bitstr_t * &_name = $name;  &register \'int\'  &_bit,  &_nbits = $nbits,  &_value = -1;  &for ( &_bit = 0;  &_bit <  &_nbits; ++ &_bit)  &if (! &bit_test( &_name,  &_bit)) {  &_value =  &_bit;  &break; } *($value) =  &_value; }  &while(0));
    }' unless defined(&bit_ffc);
    eval 'sub bit_ffs {
        my($name, $nbits, $value) = @_;
	    eval q( &do {  &register  &bitstr_t * &_name = $name;  &register \'int\'  &_bit,  &_nbits = $nbits,  &_value = -1;  &for ( &_bit = 0;  &_bit <  &_nbits; ++ &_bit)  &if ( &bit_test( &_name,  &_bit)) {  &_value =  &_bit;  &break; } *($value) =  &_value; }  &while(0));
    }' unless defined(&bit_ffs);
}
1;
