     }
     
     // each thread gets codeword for its two pixels
-    int even_code_size = 0, even_code_value = 0, odd_code_size = 0, odd_code_value = 0;
+    unsigned int even_code_size = 0, even_code_value = 0, odd_code_size = 0, odd_code_value = 0;
     if(nonzero_follows || !tid) {
         gpujpeg_huffman_gpu_encode_value(even_code_size, even_code_value, zeros_before_even & 0xf, in_even, d_table_even);
         gpujpeg_huffman_gpu_encode_value(odd_code_size, odd_code_value, zeros_before_odd & 0xf, in_odd, d_table_ac);

        const uint4 cwords = *(d_src_codewords++);
        
        // encode all 4 codewords
        gpujpeg_huffman_gpu_encoder_emit_bits(remaining_bits, byte_count, bit_count, (uint8_t*)s_temp, cwords.x & 31, cwords.x >> 5);
        gpujpeg_huffman_gpu_encoder_emit_bits(remaining_bits, byte_count, bit_count, (uint8_t*)s_temp, cwords.y & 31, cwords.y >> 5);
        gpujpeg_huffman_gpu_encoder_emit_bits(remaining_bits, byte_count, bit_count, (uint8_t*)s_temp, cwords.z & 31, cwords.z >> 5);
        gpujpeg_huffman_gpu_encoder_emit_bits(remaining_bits, byte_count, bit_count, (uint8_t*)s_temp, cwords.w & 31, cwords.w >> 5);
        
        // possibly flush output if have at least 16 bytes
        if(byte_count > 16) {


/**
 * Adds up to 32 bits at once.
 * Codeword value must be aligned to left (most significant bits).
 */
__device__ inline void 
gpujpeg_huffman_gpu_encoder_emit_bits(unsigned int & remaining_bits, int & byte_count, int & bit_count, uint8_t * const out_ptr, const unsigned int packed_code_word) {
    // decompose packed codeword into the msb-aligned value and bit-length of the value
    const unsigned int code_word = packed_code_word & ~31;
    const unsigned int code_bit_size = packed_code_word & 31;
    
    // concatenate with remaining bits
    remaining_bits |= code_word >> bit_count;
