1771eb107985566d2e458e3f06f2c7bd1b21c2dcaEugene Klyuchnikov/* Copyright 2013 Google Inc. All Rights Reserved. 2771eb107985566d2e458e3f06f2c7bd1b21c2dcaEugene Klyuchnikov 324ffa78414663b545b66be392caff7eb5574a62cEugene Klyuchnikov Distributed under MIT license. 4771eb107985566d2e458e3f06f2c7bd1b21c2dcaEugene Klyuchnikov See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5771eb107985566d2e458e3f06f2c7bd1b21c2dcaEugene Klyuchnikov*/ 6771eb107985566d2e458e3f06f2c7bd1b21c2dcaEugene Klyuchnikov 7352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov/* Functions for encoding of integers into prefix codes the amount of extra 8352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov bits, and the actual values of the extra bits. */ 9c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka 10c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka#ifndef BROTLI_ENC_PREFIX_H_ 11c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka#define BROTLI_ENC_PREFIX_H_ 12c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka 13b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#include "../common/constants.h" 140a63f99db9eeb8a15dc3941b96802141b1993912Eugene Kliuchnikov#include <brotli/port.h> 1581480011581d1bb40e2ed26566a95d060f2767b3Eugene Kliuchnikov#include <brotli/types.h> 16b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka#include "./fast_log.h" 17c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka 18b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#if defined(__cplusplus) || defined(c_plusplus) 19b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikovextern "C" { 20b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#endif 21b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov 220a63f99db9eeb8a15dc3941b96802141b1993912Eugene Kliuchnikov/* Here distance_code is an intermediate code, i.e. one of the special codes or 230a63f99db9eeb8a15dc3941b96802141b1993912Eugene Kliuchnikov the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */ 24b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikovstatic BROTLI_INLINE void PrefixEncodeCopyDistance(size_t distance_code, 25b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t num_direct_codes, 26b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t postfix_bits, 27b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov uint16_t* code, 28b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov uint32_t* extra_bits) { 29b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov if (distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes) { 30b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov *code = (uint16_t)distance_code; 31b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka *extra_bits = 0; 32b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka return; 33b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov } else { 3440101bb9bff62e81fd17324bf1af131b4b138275eustas size_t dist = ((size_t)1 << (postfix_bits + 2u)) + 35b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes); 36b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t bucket = Log2FloorNonZero(dist) - 1; 37b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t postfix_mask = (1u << postfix_bits) - 1; 38b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t postfix = dist & postfix_mask; 39b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t prefix = (dist >> bucket) & 1; 40b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t offset = (2 + prefix) << bucket; 41b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov size_t nbits = bucket - postfix_bits; 42b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov *code = (uint16_t)( 43b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes + 44b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov ((2 * (nbits - 1) + prefix) << postfix_bits) + postfix)); 45b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov *extra_bits = (uint32_t)( 46b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov (nbits << 24) | ((dist - offset) >> postfix_bits)); 47b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka } 48b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka} 49b4f39bf540c7755909105b6a96c7ac89b79364adZoltan Szabadka 50b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#if defined(__cplusplus) || defined(c_plusplus) 51b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov} /* extern "C" */ 52b972c67780f03256a3fbf81dc3350a4bf00aa4adEugene Kliuchnikov#endif 53c66e4e3e4fc3ba36ca36a43eee3b704f7b989c60Zoltan Szabadka 54352b0b2836504f59c4915b20eee11a770b415a47Eugene Kliuchnikov#endif /* BROTLI_ENC_PREFIX_H_ */ 55