ported quantization code