aboutsummaryrefslogtreecommitdiff
path: root/c/enc/command.h
diff options
context:
space:
mode:
Diffstat (limited to 'c/enc/command.h')
-rw-r--r--c/enc/command.h32
1 files changed, 19 insertions, 13 deletions
diff --git a/c/enc/command.h b/c/enc/command.h
index 0526815..6075dfe 100644
--- a/c/enc/command.h
+++ b/c/enc/command.h
@@ -13,6 +13,7 @@
#include "../common/platform.h"
#include <brotli/types.h>
#include "./fast_log.h"
+#include "./params.h"
#include "./prefix.h"
#if defined(__cplusplus) || defined(c_plusplus)
@@ -116,7 +117,8 @@ typedef struct Command {
} Command;
/* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */
-static BROTLI_INLINE void InitCommand(Command* self, size_t insertlen,
+static BROTLI_INLINE void InitCommand(Command* self,
+ const BrotliDistanceParams* dist, size_t insertlen,
size_t copylen, int copylen_code_delta, size_t distance_code) {
/* Don't rely on signed int representation, use honest casts. */
uint32_t delta = (uint8_t)((int8_t)copylen_code_delta);
@@ -126,7 +128,8 @@ static BROTLI_INLINE void InitCommand(Command* self, size_t insertlen,
npostfix and ndirect were 0, they are only recomputed later after the
clustering if needed. */
PrefixEncodeCopyDistance(
- distance_code, 0, 0, &self->dist_prefix_, &self->dist_extra_);
+ distance_code, dist->num_direct_distance_codes,
+ dist->distance_postfix_bits, &self->dist_prefix_, &self->dist_extra_);
GetLengthCode(
insertlen, (size_t)((int)copylen + copylen_code_delta),
TO_BROTLI_BOOL((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_);
@@ -140,21 +143,24 @@ static BROTLI_INLINE void InitInsertCommand(Command* self, size_t insertlen) {
GetLengthCode(insertlen, 4, BROTLI_FALSE, &self->cmd_prefix_);
}
-static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(const Command* self) {
- if ((self->dist_prefix_ & 0x3FF) < BROTLI_NUM_DISTANCE_SHORT_CODES) {
+static BROTLI_INLINE uint32_t CommandRestoreDistanceCode(
+ const Command* self, const BrotliDistanceParams* dist) {
+ if ((self->dist_prefix_ & 0x3FF) <
+ BROTLI_NUM_DISTANCE_SHORT_CODES + dist->num_direct_distance_codes) {
return self->dist_prefix_ & 0x3FF;
} else {
+ uint32_t dcode = self->dist_prefix_ & 0x3FF;
uint32_t nbits = self->dist_prefix_ >> 10;
uint32_t extra = self->dist_extra_;
- /* It is assumed that the distance was first encoded with NPOSTFIX = 0 and
- NDIRECT = 0, so the code itself is of this form:
- BROTLI_NUM_DISTANCE_SHORT_CODES + 2 * (nbits - 1) + prefix_bit
- Therefore, the following expression results in (2 + prefix_bit). */
- uint32_t prefix = (self->dist_prefix_ & 0x3FF) + 4u -
- BROTLI_NUM_DISTANCE_SHORT_CODES - 2u * nbits;
- /* Subtract 4 for offset (Chapter 4.) and
- increase by BROTLI_NUM_DISTANCE_SHORT_CODES - 1 */
- return (prefix << nbits) + extra + BROTLI_NUM_DISTANCE_SHORT_CODES - 4u;
+ uint32_t postfix_mask = (1U << dist->distance_postfix_bits) - 1U;
+ uint32_t hcode = (dcode - dist->num_direct_distance_codes -
+ BROTLI_NUM_DISTANCE_SHORT_CODES) >>
+ dist->distance_postfix_bits;
+ uint32_t lcode = (dcode - dist->num_direct_distance_codes -
+ BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask;
+ uint32_t offset = ((2U + (hcode & 1U)) << nbits) - 4U;
+ return ((offset + extra) << dist->distance_postfix_bits) + lcode +
+ dist->num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES;
}
}