1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: ttcalc.c 8250 2007-09-25 13:31:24Z giles $ */
16
/* Changes after FreeType: cut out the TrueType instruction interpreter. */
18
/*******************************************************************
22
* Arithmetic Computations (body).
24
* Copyright 1996-1998 by
25
* David Turner, Robert Wilhelm, and Werner Lemberg.
27
* This file is part of the FreeType project, and may only be used
28
* modified and distributed under the terms of the FreeType project
29
* license, LICENSE.TXT. By continuing to use, modify, or distribute
30
* this file you indicate that you have read the license and
31
* understand and accept it fully.
33
******************************************************************/
39
/* support for 1-complement arithmetic has been totally dropped in this */
40
/* release. You can still write your own code if you need it.. */
42
static const long Roots[63] =
44
1, 1, 2, 3, 4, 5, 8, 11,
45
16, 22, 32, 45, 64, 90, 128, 181,
46
256, 362, 512, 724, 1024, 1448, 2048, 2896,
47
4096, 5892, 8192, 11585, 16384, 23170, 32768, 46340,
49
65536, 92681, 131072, 185363, 262144, 370727,
50
524288, 741455, 1048576, 1482910, 2097152, 2965820,
51
4194304, 5931641, 8388608, 11863283, 16777216, 23726566,
53
33554432, 47453132, 67108864, 94906265,
54
134217728, 189812531, 268435456, 379625062,
55
536870912, 759250125, 1073741824, 1518500250,
62
Int32 MulDiv( Int32 a, Int32 b, Int32 c )
71
return ((s < 0) ? -a : a);
75
Int32 MulDiv_Round( Int32 a, Int32 b, Int32 c )
83
a = ((Int64)a * b + c/2) / c;
84
return ((s < 0) ? -a : a);
88
static Int Order64( Int64 z )
93
z = (unsigned INT64)z >> 1;
100
Int32 Sqrt64( Int64 l )
104
if ( l <= 0 ) return 0;
105
if ( l == 1 ) return 1;
107
r = Roots[Order64( l )];
112
r = ( r + l/r ) >> 1;
114
while ( r > s || r*r > l );
121
Int32 MulDiv( Int32 a, Int32 b, Int32 c )
130
MulTo64( a, b, &temp );
131
a = Div64by32( &temp, c );
133
return ((s < 0) ? -a : a);
137
Int32 MulDiv_Round( Int32 a, Int32 b, Int32 c )
146
MulTo64( a, b, &temp );
147
temp2.hi = (Int32)(c >> 31);
148
temp2.lo = (Word32)(c / 2);
149
Add64( &temp, &temp2, &temp );
150
a = Div64by32( &temp, c );
152
return ((s < 0) ? -a : a);
156
static void Neg64__( Int64* x )
158
/* Remember that -(0x80000000) == 0x80000000 with 2-complement! */
159
/* We take care of that here. */
168
if ( (Int32)x->hi == 0x80000000 ) /* Check -MaxInt32 - 1 */
171
x->hi--; /* We return 0x7FFFFFFF! */
177
void Add64( Int64* x, Int64* y, Int64* z )
179
register Word32 lo, hi;
185
if ( (Word32)x->lo >= (Word32)(-y->lo) ) hi++;
192
void Sub64( Int64* x, Int64* y, Int64* z )
194
register Word32 lo, hi;
199
if ( x->lo < y->lo ) hi--;
206
void MulTo64( Int32 x, Int32 y, Int64* z )
209
Word32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
214
lo1 = x & 0x0000FFFF; hi1 = x >> 16;
215
lo2 = y & 0x0000FFFF; hi2 = y >> 16;
222
/* Check carry overflow of i1 + i2 */
226
if ( i1 >= (Word32)-i2 ) hi += 1 << 16;
233
/* Check carry overflow of i1 + lo */
236
if ( lo >= (Word32)-i1 ) hi++;
245
if (s < 0) Neg64__( z );
249
Int32 Div64by32( Int64* x, Int32 y )
254
s = x->hi; if (s<0) Neg64__(x);
261
return ((s<0) ? -q : q);
267
if ( r >= (Word32)y ) /* we know y is to be treated as unsigned here */
268
return ( (s<0) ? 0x80000001 : 0x7FFFFFFF );
269
/* Return Max/Min Int32 if divide overflow */
270
/* This includes division by zero! */
272
for ( i = 0; i < 32; i++ )
278
if ( r >= (Word32)y )
286
return ( (s<0) ? -q : q );
290
Int Order64( Int64* z )
315
Int32 Sqrt64( Int64* l )
320
if ( (Int32)l->hi < 0 ||
321
(l->hi == 0 && l->lo == 0) ) return 0;
324
if ( s == 0 ) return 1;
330
r = ( r + Div64by32(l,r) ) >> 1;
331
MulTo64( r, r, &l2 );
332
Sub64 ( l, &l2, &l2 );
334
while ( r > s || (Int32)l2.hi < 0 );
341
#if 0 /* unused by the rest of the library */
343
Int Order32( Int32 z )
357
Int32 Sqrt32( Int32 l )
361
if ( l <= 0 ) return 0;
362
if ( l == 1 ) return 1;
364
r = Roots[Order32( l )];
368
r = ( r + l/r ) >> 1;
370
while ( r > s || r*r > l );