]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/BrotliCompress/enc/write_bits.h
BaseTools: Copy Brotli algorithm 3rd party source code for tool
[mirror_edk2.git] / BaseTools / Source / C / BrotliCompress / enc / write_bits.h
diff --git a/BaseTools/Source/C/BrotliCompress/enc/write_bits.h b/BaseTools/Source/C/BrotliCompress/enc/write_bits.h
new file mode 100644 (file)
index 0000000..3d5cad6
--- /dev/null
@@ -0,0 +1,90 @@
+/* Copyright 2010 Google Inc. All Rights Reserved.\r
+\r
+   Distributed under MIT license.\r
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
+*/\r
+\r
+/* Write bits into a byte array. */\r
+\r
+#ifndef BROTLI_ENC_WRITE_BITS_H_\r
+#define BROTLI_ENC_WRITE_BITS_H_\r
+\r
+#include <assert.h>\r
+#include <stdio.h>  /* printf */\r
+\r
+#include "../common/types.h"\r
+#include "./port.h"\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+extern "C" {\r
+#endif\r
+\r
+/*#define BIT_WRITER_DEBUG */\r
+\r
+/* This function writes bits into bytes in increasing addresses, and within\r
+   a byte least-significant-bit first.\r
+\r
+   The function can write up to 56 bits in one go with WriteBits\r
+   Example: let's assume that 3 bits (Rs below) have been written already:\r
+\r
+   BYTE-0     BYTE+1       BYTE+2\r
+\r
+   0000 0RRR    0000 0000    0000 0000\r
+\r
+   Now, we could write 5 or less bits in MSB by just sifting by 3\r
+   and OR'ing to BYTE-0.\r
+\r
+   For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,\r
+   and locate the rest in BYTE+1, BYTE+2, etc. */\r
+static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,\r
+                                          uint64_t bits,\r
+                                          size_t * BROTLI_RESTRICT pos,\r
+                                          uint8_t * BROTLI_RESTRICT array) {\r
+#ifdef IS_LITTLE_ENDIAN\r
+  /* This branch of the code can write up to 56 bits at a time,\r
+     7 bits are lost by being perhaps already in *p and at least\r
+     1 bit is needed to initialize the bit-stream ahead (i.e. if 7\r
+     bits are in *p and we write 57 bits, then the next write will\r
+     access a byte that was never initialized). */\r
+  uint8_t *p = &array[*pos >> 3];\r
+  uint64_t v = *p;\r
+#ifdef BIT_WRITER_DEBUG\r
+  printf("WriteBits  %2d  0x%016llx  %10d\n", n_bits, bits, *pos);\r
+#endif\r
+  assert((bits >> n_bits) == 0);\r
+  assert(n_bits <= 56);\r
+  v |= bits << (*pos & 7);\r
+  BROTLI_UNALIGNED_STORE64(p, v);  /* Set some bits. */\r
+  *pos += n_bits;\r
+#else\r
+  /* implicit & 0xff is assumed for uint8_t arithmetics */\r
+  uint8_t *array_pos = &array[*pos >> 3];\r
+  const size_t bits_reserved_in_first_byte = (*pos & 7);\r
+  size_t bits_left_to_write;\r
+  bits <<= bits_reserved_in_first_byte;\r
+  *array_pos++ |= (uint8_t)bits;\r
+  for (bits_left_to_write = n_bits + bits_reserved_in_first_byte;\r
+       bits_left_to_write >= 9;\r
+       bits_left_to_write -= 8) {\r
+    bits >>= 8;\r
+    *array_pos++ = (uint8_t)bits;\r
+  }\r
+  *array_pos = 0;\r
+  *pos += n_bits;\r
+#endif\r
+}\r
+\r
+static BROTLI_INLINE void BrotliWriteBitsPrepareStorage(\r
+    size_t pos, uint8_t *array) {\r
+#ifdef BIT_WRITER_DEBUG\r
+  printf("WriteBitsPrepareStorage            %10d\n", pos);\r
+#endif\r
+  assert((pos & 7) == 0);\r
+  array[pos >> 3] = 0;\r
+}\r
+\r
+#if defined(__cplusplus) || defined(c_plusplus)\r
+}  /* extern "C" */\r
+#endif\r
+\r
+#endif  /* BROTLI_ENC_WRITE_BITS_H_ */\r