summaryrefslogtreecommitdiffstats
path: root/predictor.c
blob: ff220ef03370184897d40f11d867bae00b1a6d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "predictor.h"
/*
 * SECONDARY SYMBOL ESTIMATION
 *
 * Maps the probability of the next bit being 1 and a context 
 * into a new probability that the next bit will be 1.
 *
 * After each guess, the state is updated to improve future
 * guesses.
 *
 * The input probability (p1) is stretched and divided into 32
 * segments to combine with other contexts. The output is
 * interpolated between two adjacent quantized values of
 * stretch(p1).
 */

/* Determines the learning rate for updates (smaller = faster, default=8) */
#define APM_RATE 8 

struct apm_t {
        int index;
        int n;
        uint16_t *t;
};

/**
 * apm_learn()
 * ```````````
 * Compute adjusted probability in context @context (0 to n-1) 
 *
 * @apm    : Reference to APM structure
 * @p      : Probability that the next bit will be 1
 * @context: Context in which the prediction was made (0 to n-1)
 * @bit    : Actual observed bit.
 * Return  : Adjusted probability that next bit will be 1 scaled to (0-65535)
 */
void apm_learn(struct apm_t *apm, int bit)
{
        int g;

        g = (bit<<16) + (bit<<APM_RATE) - bit - bit;
    
        apm->t[apm->index]   += g - apm->t[apm->index]   >> APM_RATE;
        apm->t[apm->index+1] += g - apm->t[apm->index+1] >> APM_RATE;
}

/**
 * apm_prob()
 * ``````````
 * Compute adjusted probability in context @context (0 to n-1) 
 *
 * @apm    : Reference to APM structure
 * @p      : Probability that the next bit will be 1
 * @context: Context in which the prediction was made (0 to n-1)
 * Return  : Adjusted probability that next bit will be 1 scaled to (0-65535)
 */
int apm_prob(struct apm_t *apm, int p, int context)
{
        int w; /* Interpolation weight (33 points) */

        p = stretch(p);

        w = p & 127;
    
        apm->index = (p+2048>>7)+context*33;

        return apm->t[apm->index]*(128-w) + apm->t[apm->index+1]*w >> 11;
}

/**
 * apm_init()
 * ``````````
 * Initialize an APM structure
 *
 * @apm  : Reference to APM structure
 * @n    : Number of contexts to accomodate
 * Return: Nothing
 */
void apm_init(struct apm_t *apm, int n)
{
        int i;
        int j;

        apm->index = 0;

        if (!apm->t) {
                apm->t = malloc(n*33*sizeof(uint16_t));
        }

        for (i=0; i<n; i++) {
                for (j=0; j<33; j++) {
                        if (i == 0) {
                                apm->t[i*33+j] = squash((j-16)*128)*16;
                        } else {
                                apm->t[i*33+j] = apm->t[j];
                        }
                }
        }
}

/**
 * h2()
 * ````
 * Hash two 32-bit integers into one 32-bit integer
 *
 * @a    : 32-bit integer
 * @b    : 32-bit integer
 * Return: Hash of @a and @b
 */
uint32_t h2(uint32_t a, uint32_t b)
{
        uint32_t c;
        uint32_t d;
        uint32_t e;
        uint32_t h;

        c = 0xFFFFFFFF;
        d = 0xFFFFFFFF;
        e = 0xFFFFFFFF;
        
        h = a*200002979U+b*30005491U+c*50004239U+d*70004807U+e*110002499U;
  
        return h^h>>9^a>>2^b>>3^c>>4^d>>5^e>>6;
}

/**
 * h3()
 * ````
 * Hash three 32-bit integers into one 32-bit integer
 *
 * @a    : 32-bit integer
 * @b    : 32-bit integer
 * @c    : 32-bit integer
 * Return: Hash of @a, @b, and @c
 */
uint32_t h3(uint32_t a, uint32_t b, uint32_t c)
{
        uint32_t d;
        uint32_t e;
        uint32_t h;

        d = 0xFFFFFFFF;
        e = 0xFFFFFFFF;
        
        h = a*200002979U+b*30005491U+c*50004239u+d*70004807U+e*110002499U;
  
        return h^h>>9^a>>2^b>>3^c>>4^d>>5^e>>6;
}


static struct apm_t A1;
static struct apm_t A2;
static struct apm_t A3;
static struct apm_t A4;

static int APM_is_active = 0;


int apm_adjust(int pr, uint32_t cx, uint8_t part, int bit)
{
        uint32_t hash0;
        uint32_t hash1;
        uint32_t hash2;

        int pr2;
        int pr3;
        int pr4;

        /* Initialize these static folks the first time */
        if (APM_is_active == 0) {
                apm_init(&A1, 256);
                apm_init(&A2, 0x10000);
                apm_init(&A3, 0x10000);
                apm_init(&A4, 0x10000);

                /* Prevent this branch from happening again */ 
                APM_is_active = 1;
        }

        /* Train the APMs from the last time */
        apm_learn(&A1, bit);
        apm_learn(&A2, bit);
        apm_learn(&A3, bit);
        apm_learn(&A4, bit);

        /* Hash the order 0, order 1, and order 2 contexts into indices */
        hash0 = part+256*(cx&0x000000FF);
        hash1 = part ^ h2(cx&0x000000FF, cx&0x0000FF00>>8);
        hash2 = part ^ h3(cx&0x000000FF, cx&0x0000FF00>>8, cx&0x00FF0000>>16);

        /*
         * There are 2 APM stages in series:
         *
         *      p1 := (p1 + 3 APM(order 0, p1)) / 4.
         *      p1 := (APM(order 1, p1) + 2 APM(order 2, p1) + APM(order 3, p1)) / 4.
         */
        pr  = (apm_prob(&A1, pr, part) * 3 + pr) >> 2;

        pr2 = apm_prob(&A2, pr, hash0 & 0xFFFF);
        pr3 = apm_prob(&A3, pr, hash1 & 0xFFFF);
        pr4 = apm_prob(&A4, pr, hash2 & 0xFFFF);

        pr = (pr2 + pr3*2 + pr4+2) >> 2;

        return pr;
}

#define USE_SSE

int SMOOTH(int p, uint32_t context, uint8_t part, int bit)
{
        #ifdef USE_SSE
        return apm_adjust(p, context, part, bit);
        #else
        return p;
        #endif
}