 }
 
 
+__device__ static void
+gpujpeg_huffman_gpu_encode_value(int & out_nbits, int & out_cword, const int preceding_zero_count, const int value,
+                                 const struct gpujpeg_table_huffman_encoder * const d_table) {
+    out_cword = value;
+    int absolute = value;
+    if ( value < 0 ) {
+        // valu eis now absolute value of input
+        absolute = -absolute;
+        // For a negative input, want temp2 = bitwise complement of abs(input)
+        // This code assumes we are on a two's complement machine
+        out_cword--;
+    }
+
+    // Find the number of bits needed for the magnitude of the coefficient
+    out_nbits = 0;
+    while ( absolute ) {
+        out_nbits++;
+        absolute >>= 1;
+    }
+    
+    // trim remaining bits
+    out_cword &= (1 << out_nbits) - 1;
+    
+    // find prefix of the codeword and size of the prefix
+    const int prefix_idx = preceding_zero_count * 16 + out_nbits;
+    out_cword |= d_table->code[prefix_idx] << out_nbits;
+    out_nbits += d_table->size[prefix_idx];
+}
 
 
 /**

    const unsigned int follow_mask = ~nonzero_mask;
    const bool nonzero_follows = follow_mask & ((even_nonzero_bitmap >> 1) | odd_nonzero_bitmap);
    
    
    
//     // save value into shared memory  TODO: remove later!!!
//     s_in[load_idx] = in_even;
//     s_in[load_idx + 1] = in_odd;
//     
//     // TODO: is this needed?
//     __threadfence_block();
//     
//     // compute count of consecutive zeros before even value
//     // TODO: reimplement after getting it all to work
//     int ref_zeros_before_even = 0; // TODO implement anyhow (NOTE: first DC coefficient is treated as nonzero)
//     for(int i = load_idx; --i > 0; ) {
//         if(s_in[i]) {
//             break;
//         }
//         ref_zeros_before_even++;
//     }
//     assert(zeros_before_even == ref_zeros_before_even);
//     
//     // TODO: set to true if any nonzero value follows thread's even value
//     bool ref_nonzero_follows = false;
//     for(int i = load_idx + 1; i < 64; i++) {
//         if(s_in[i]) {
//             ref_nonzero_follows = true;
//             break;
//         }
//     }
//     
//     assert(ref_nonzero_follows == nonzero_follows);
    
    // count of consecutive zeros before odd value (either one more than 
    // even if even is zero or none if even value itself is nonzero)
    const int zeros_before_odd = in_even || !tid ? 0 : zeros_before_even + 1;

            break;
        }
        
        gpujpeg_huffman_gpu_encode_value(cword_size, cword_value, r, value, d_table_ac);
        gpujpeg_huffman_gpu_encoder_emit_bits(out_bits, data_compressed, cword_size, cword_value);
    }

    return 0;
