| 155 | } // namespace |
| 156 | |
| 157 | static inline char* EmitLiteral(char* op, |
| 158 | const char* literal, |
| 159 | int len, |
| 160 | bool allow_fast_path) { |
| 161 | int n = len - 1; // Zero-length literals are disallowed |
| 162 | if (n < 60) { |
| 163 | // Fits in tag byte |
| 164 | *op++ = LITERAL | (n << 2); |
| 165 | |
| 166 | // The vast majority of copies are below 16 bytes, for which a |
| 167 | // call to memcpy is overkill. This fast path can sometimes |
| 168 | // copy up to 15 bytes too much, but that is okay in the |
| 169 | // main loop, since we have a bit to go on for both sides: |
| 170 | // |
| 171 | // - The input will always have kInputMarginBytes = 15 extra |
| 172 | // available bytes, as long as we're in the main loop, and |
| 173 | // if not, allow_fast_path = false. |
| 174 | // - The output will always have 32 spare bytes (see |
| 175 | // MaxCompressedLength). |
| 176 | if (allow_fast_path && len <= 16) { |
| 177 | UnalignedCopy64(literal, op); |
| 178 | UnalignedCopy64(literal + 8, op + 8); |
| 179 | return op + len; |
| 180 | } |
| 181 | } else { |
| 182 | // Encode in upcoming bytes |
| 183 | char* base = op; |
| 184 | int count = 0; |
| 185 | op++; |
| 186 | while (n > 0) { |
| 187 | *op++ = n & 0xff; |
| 188 | n >>= 8; |
| 189 | count++; |
| 190 | } |
| 191 | assert(count >= 1); |
| 192 | assert(count <= 4); |
| 193 | *base = LITERAL | ((59+count) << 2); |
| 194 | } |
| 195 | memcpy(op, literal, len); |
| 196 | return op + len; |
| 197 | } |
| 198 | |
| 199 | static inline char* EmitCopyLessThan64(char* op, size_t offset, int len) { |
| 200 | assert(len <= 64); |
no test coverage detected