:: commit 926477ccd29dc36846d7b7e7250aa9ae9af0ecb5

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-05-13 08:53

parents: f6ed0ca2de

inline the AC

diff --git a/src/cm.c b/src/cm.c
index 386da4d..1d1afd4 100644
--- a/src/cm.c
+++ b/src/cm.c
@@ -30,44 +30,6 @@
 #define write_out(s, c) (s)->out_queue[(s)->output_ptr++] = (c)
 #define read_in(s) ((s)->input_ptr < (s)->input_max ? (s)->in_queue[(s)->input_ptr++] : -1)
 
-// Encode a zero bit with given probability.
-static inline void encodebit0(state * s, u32 p) {
-    s->low += (((u64)(s->high - s->low) * p) >> 18) + 1;
-
-    // Write identical bits.
-    while ((s->low ^ s->high) < (1 << 24)) {
-        write_out(s, s->low >> 24);  // Same as s->high >> 24
-        s->low <<= 8;
-        s->high = (s->high << 8) | 0xFF;
-    }
-}
-
-// Encode an one bit with given probability.
-static inline void encodebit1(state * s, u32 p) {
-    s->high = s->low + (((u64)(s->high - s->low) * p) >> 18);
-    while ((s->low ^ s->high) < (1 << 24)) {
-        write_out(s, s->low >> 24);
-        s->low <<= 8;
-        s->high = (s->high << 8) + 255;
-    }
-}
-
-static inline u8 decodebit(state * s, u32 p) {
-    // Split the range.
-    const u32 mid = s->low + (((u64)(s->high - s->low) * p) >> 18);
-    const u8 bit = s->code <= mid;
-    if (bit)
-        s->high = mid;
-    else
-        s->low = mid + 1;
-    while ((s->low ^ s->high) < (1 << 24)) {
-        s->low <<= 8;
-        s->high = (s->high << 8) + 255;
-        s->code = (s->code << 8) + read_in(s);
-    }
-    return bit;
-}
-
 void flush(state * s) {
     write_out(s, s->low >> 24);
     s->low <<= 8;
@@ -129,14 +91,29 @@ void encode_bytes(state * s, u8 * buf, s32 size) {
             const int ssep = x1 + (((x2 - x1) * (p & 4095)) >> 12);
 
             if (c & 128) {
-                encodebit1(s, ssep * 3 + p);
+                s->high = s->low + (((u64)(s->high - s->low) * (ssep * 3 + p)) >> 18);
+
+                while ((s->low ^ s->high) < (1 << 24)) {
+                    write_out(s, s->low >> 24);
+                    s->low <<= 8;
+                    s->high = (s->high << 8) + 0xFF;
+                }
+
                 s->C0[ctx] = update1(s->C0[ctx], 2);
                 s->C1[s->c1][ctx] = update1(s->C1[s->c1][ctx], 4);
                 s->C2[2 * ctx + f][j] = update1(s->C2[2 * ctx + f][j], 6);
                 s->C2[2 * ctx + f][j + 1] = update1(s->C2[2 * ctx + f][j + 1], 6);
                 ctx += ctx + 1;
             } else {
-                encodebit0(s, ssep * 3 + p);
+                s->low += (((u64)(s->high - s->low) * (ssep * 3 + p)) >> 18) + 1;
+
+                // Write identical bits.
+                while ((s->low ^ s->high) < (1 << 24)) {
+                    write_out(s, s->low >> 24);  // Same as s->high >> 24
+                    s->low <<= 8;
+                    s->high = (s->high << 8) + 0xFF;
+                }
+
                 s->C0[ctx] = update0(s->C0[ctx], 2);
                 s->C1[s->c1][ctx] = update0(s->C1[s->c1][ctx], 4);
                 s->C2[2 * ctx + f][j] = update0(s->C2[2 * ctx + f][j], 6);
@@ -174,7 +151,17 @@ void decode_bytes(state * s, u8 * c, s32 size) {
             const int x2 = s->C2[2 * ctx + f][j + 1];
             const int ssep = x1 + (((x2 - x1) * (p & 4095)) >> 12);
 
-            const int bit = decodebit(s, ssep * 3 + p);
+            const u32 mid = s->low + (((u64)(s->high - s->low) * (ssep * 3 + p)) >> 18);
+            const u8 bit = s->code <= mid;
+            if (bit)
+                s->high = mid;
+            else
+                s->low = mid + 1;
+            while ((s->low ^ s->high) < (1 << 24)) {
+                s->low <<= 8;
+                s->high = (s->high << 8) + 255;
+                s->code = (s->code << 8) + read_in(s);
+            }
 
             if (bit) {
                 s->C0[ctx] = update1(s->C0[ctx], 2);
tab: 248 wrap: offon