summaryrefslogtreecommitdiffstats
path: root/libpixelflinger/fixed.cpp
blob: 5b920628bad1e6d5a9c6a871e6e81df906d0b8a6 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* libs/pixelflinger/fixed.cpp
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

#include <stdio.h>

#include <private/pixelflinger/ggl_context.h>
#include <private/pixelflinger/ggl_fixed.h>


// ------------------------------------------------------------------------

int32_t gglRecipQNormalized(int32_t x, int* exponent)
{
    const int32_t s = x>>31;
    uint32_t a = s ? -x : x;

    // the result will overflow, so just set it to the biggest/inf value
    if (ggl_unlikely(a <= 2LU)) {
        *exponent = 0;
        return s ? FIXED_MIN : FIXED_MAX;
    }

    // Newton-Raphson iteration:
    // x = r*(2 - a*r)

    const int32_t lz = gglClz(a);
    a <<= lz;  // 0.32
    uint32_t r = a;
    // note: if a == 0x80000000, this means x was a power-of-2, in this
    // case we don't need to compute anything. We get the reciprocal for
    // (almost) free.
    if (a != 0x80000000) {
        r = (0x2E800 << (30-16)) - (r>>(2-1)); // 2.30, r = 2.90625 - 2*a
        // 0.32 + 2.30 = 2.62 -> 2.30
        // 2.30 + 2.30 = 4.60 -> 2.30
        r = (((2LU<<30) - uint32_t((uint64_t(a)*r) >> 32)) * uint64_t(r)) >> 30;
        r = (((2LU<<30) - uint32_t((uint64_t(a)*r) >> 32)) * uint64_t(r)) >> 30;
    }

    // shift right 1-bit to make room for the sign bit
    *exponent = 30-lz-1;
    r >>= 1;
    return s ? -r : r;
}

int32_t gglRecipQ(GGLfixed x, int q)
{
    int shift;
    x = gglRecipQNormalized(x, &shift);
    shift += 16-q;
    x += 1L << (shift-1);   // rounding
    x >>= shift;
    return x;
}    

// ------------------------------------------------------------------------

GGLfixed gglFastDivx(GGLfixed n, GGLfixed d)
{
    if ((d>>24) && ((d>>24)+1)) {
        n >>= 8;
        d >>= 8;
    }
    return gglMulx(n, gglRecip(d));
}

// ------------------------------------------------------------------------

static const GGLfixed ggl_sqrt_reciproc_approx_tab[8] = {
    // 1/sqrt(x) with x = 1-N/16, N=[8...1]
    0x16A09, 0x15555, 0x143D1, 0x134BF, 0x1279A, 0x11C01, 0x111AC, 0x10865
};

GGLfixed gglSqrtRecipx(GGLfixed x)
{
    if (x == 0)         return FIXED_MAX;
    if (x == FIXED_ONE) return x;
    const GGLfixed a = x;
    const int32_t lz = gglClz(x);
    x = ggl_sqrt_reciproc_approx_tab[(a>>(28-lz))&0x7];
    const int32_t exp = lz - 16;
    if (exp <= 0)   x >>= -exp>>1;
    else            x <<= (exp>>1) + (exp & 1);        
    if (exp & 1) {
        x = gglMulx(x, ggl_sqrt_reciproc_approx_tab[0])>>1;
    }
    // 2 Newton-Raphson iterations: x = x/2*(3-(a*x)*x)
    x = gglMulx((x>>1),(0x30000 - gglMulx(gglMulx(a,x),x)));
    x = gglMulx((x>>1),(0x30000 - gglMulx(gglMulx(a,x),x)));
    return x;
}

GGLfixed gglSqrtx(GGLfixed a)
{
    // Compute a full precision square-root (24 bits accuracy)
    GGLfixed r = 0;
    GGLfixed bit = 0x800000;
    int32_t bshift = 15;
    do {
        GGLfixed temp = bit + (r<<1);
        if (bshift >= 8)    temp <<= (bshift-8);
        else                temp >>= (8-bshift);
        if (a >= temp) {
            r += bit;
            a -= temp;
        }
        bshift--;
    } while (bit>>=1);
    return r;
}

// ------------------------------------------------------------------------

static const GGLfixed ggl_log_approx_tab[] = {
    // -ln(x)/ln(2) with x = N/16, N=[8...16]
    0xFFFF, 0xd47f, 0xad96, 0x8a62, 0x6a3f, 0x4caf, 0x3151, 0x17d6, 0x0000
};

static const GGLfixed ggl_alog_approx_tab[] = { // domain [0 - 1.0]
	0xffff, 0xeac0, 0xd744, 0xc567, 0xb504, 0xa5fe, 0x9837, 0x8b95, 0x8000
};

GGLfixed gglPowx(GGLfixed x, GGLfixed y)
{
    // prerequisite: 0 <= x <= 1, and y >=0

    // pow(x,y) = 2^(y*log2(x))
    // =  2^(y*log2(x*(2^exp)*(2^-exp))))
    // =  2^(y*(log2(X)-exp))
    // =  2^(log2(X)*y - y*exp)
    // =  2^( - (-log2(X)*y + y*exp) )
    
    int32_t exp = gglClz(x) - 16;
    GGLfixed f = x << exp;
    x = (f & 0x0FFF)<<4;
    f = (f >> 12) & 0x7;
    GGLfixed p = gglMulAddx(
            ggl_log_approx_tab[f+1] - ggl_log_approx_tab[f], x,
            ggl_log_approx_tab[f]);
    p = gglMulAddx(p, y, y*exp);
    exp = gglFixedToIntFloor(p);
    if (exp < 31) {
        p = gglFracx(p);
        x = (p & 0x1FFF)<<3;
        p >>= 13;    
        p = gglMulAddx(
                ggl_alog_approx_tab[p+1] - ggl_alog_approx_tab[p], x,
                ggl_alog_approx_tab[p]);
        p >>= exp;
    } else {
        p = 0;
    }
    return p;
        // ( powf((a*65536.0f), (b*65536.0f)) ) * 65536.0f;
}

// ------------------------------------------------------------------------

int32_t gglDivQ(GGLfixed n, GGLfixed d, int32_t i)
{
    //int32_t r =int32_t((int64_t(n)<<i)/d);
    const int32_t ds = n^d;
    if (n<0) n = -n;
    if (d<0) d = -d;
    int nd = gglClz(d) - gglClz(n);
    i += nd + 1;
    if (nd > 0) d <<= nd;
    else        n <<= -nd;
    uint32_t q = 0;

    int j = i & 7;
    i >>= 3;

    // gcc deals with the code below pretty well.
    // we get 3.75 cycles per bit in the main loop
    // and 8 cycles per bit in the termination loop
    if (ggl_likely(i)) {
        n -= d;
        do {
            q <<= 8;
            if (n>=0)   q |= 128;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 64;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 32;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 16;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 8;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 4;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 2;
            else        n += d;
            n = n*2 - d;
            if (n>=0)   q |= 1;
            else        n += d;
            
            if (--i == 0)
                goto finish;

            n = n*2 - d;
        } while(true);
        do {
            q <<= 1;
            n = n*2 - d;
            if (n>=0)   q |= 1;
            else        n += d;
        finish: ;
        } while (j--);
        return (ds<0) ? -q : q;
    }

    n -= d;
    if (n>=0)   q |= 1;
    else        n += d;
    j--;
    goto finish;
}

// ------------------------------------------------------------------------

// assumes that the int32_t values of a, b, and c are all positive
// use when both a and b are larger than c

template <typename T>
static inline void swap(T& a, T& b) {
    T t(a);
    a = b;
    b = t;
}

static __attribute__((noinline))
int32_t slow_muldiv(uint32_t a, uint32_t b, uint32_t c)
{
	// first we compute a*b as a 64-bit integer
    // (GCC generates umull with the code below)
    uint64_t ab = uint64_t(a)*b;
    uint32_t hi = ab>>32;
    uint32_t lo = ab;
    uint32_t result;

	// now perform the division
	if (hi >= c) {
	overflow:
		result = 0x7fffffff;  // basic overflow
	} else if (hi == 0) {
		result = lo/c;  // note: c can't be 0
		if ((result >> 31) != 0)  // result must fit in 31 bits
			goto overflow;
	} else {
		uint32_t r = hi;
		int bits = 31;
	    result = 0;
		do {
			r = (r << 1) | (lo >> 31);
			lo <<= 1;
			result <<= 1;
			if (r >= c) {
				r -= c;
				result |= 1;
			}
		} while (bits--);
	}
	return int32_t(result);
}

// assumes a >= 0 and c >= b >= 0
static inline
int32_t quick_muldiv(int32_t a, int32_t b, int32_t c)
{
    int32_t r = 0, q = 0, i;
    int leading = gglClz(a);
    i = 32 - leading;
    a <<= leading;
    do {
        r <<= 1;
        if (a < 0)
            r += b;
        a <<= 1;
        q <<= 1;
        if (r >= c) {
            r -= c;
            q++;
        }
        asm(""::); // gcc generates better code this way
        if (r >= c) {
            r -= c;
            q++;
        }
    }
    while (--i);
    return q;
}

// this function computes a*b/c with 64-bit intermediate accuracy
// overflows (e.g. division by 0) are handled and return INT_MAX

int32_t gglMulDivi(int32_t a, int32_t b, int32_t c)
{
	int32_t result;
	int32_t sign = a^b^c;

	if (a < 0) a = -a;
	if (b < 0) b = -b;
	if (c < 0) c = -c;

    if (a < b) {
        swap(a, b);
    }
    
	if (b <= c) result = quick_muldiv(a, b, c);
	else        result = slow_muldiv((uint32_t)a, (uint32_t)b, (uint32_t)c);
	
	if (sign < 0)
		result = -result;
	  
    return result;
}