1999-12-17 00:11:37 +01:00
|
|
|
/***************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* ftcalc.c */
|
|
|
|
/* */
|
|
|
|
/* Arithmetic computations (body). */
|
|
|
|
/* */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* Copyright 1996-2000 by */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
|
|
|
|
/* */
|
2000-06-05 07:26:15 +02:00
|
|
|
/* This file is part of the FreeType project, and may only be used, */
|
|
|
|
/* modified, and distributed under the terms of the FreeType project */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
|
|
|
|
/* this file you indicate that you have read the license and */
|
|
|
|
/* understand and accept it fully. */
|
|
|
|
/* */
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Support for 1-complement arithmetic has been totally dropped in this */
|
|
|
|
/* release. You can still write your own code if you need it. */
|
|
|
|
/* */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* Implementing basic computation routines. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* FT_MulDiv() and FT_MulFix() are declared in freetype.h. */
|
|
|
|
/* */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
|
2000-05-11 20:23:52 +02:00
|
|
|
#include <freetype/internal/ftcalc.h>
|
|
|
|
#include <freetype/internal/ftdebug.h>
|
|
|
|
#include <freetype/internal/ftobjs.h> /* for ABS() */
|
1999-12-17 00:11:37 +01:00
|
|
|
|
|
|
|
|
2000-06-03 08:03:11 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
|
|
|
|
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
|
|
|
|
/* messages during execution. */
|
|
|
|
/* */
|
|
|
|
#undef FT_COMPONENT
|
|
|
|
#define FT_COMPONENT trace_calc
|
|
|
|
|
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
#ifdef FT_CONFIG_OPTION_OLD_CALCS
|
|
|
|
|
|
|
|
static const FT_Long ft_square_roots[63] =
|
|
|
|
{
|
|
|
|
1, 1, 2, 3, 4, 5, 8, 11,
|
|
|
|
16, 22, 32, 45, 64, 90, 128, 181,
|
|
|
|
256, 362, 512, 724, 1024, 1448, 2048, 2896,
|
|
|
|
4096, 5892, 8192, 11585, 16384, 23170, 32768, 46340,
|
|
|
|
|
|
|
|
65536, 92681, 131072, 185363, 262144, 370727,
|
|
|
|
524288, 741455, 1048576, 1482910, 2097152, 2965820,
|
|
|
|
4194304, 5931641, 8388608, 11863283, 16777216, 23726566,
|
|
|
|
|
|
|
|
33554432, 47453132, 67108864, 94906265,
|
|
|
|
134217728, 189812531, 268435456, 379625062,
|
|
|
|
536870912, 759250125, 1073741824, 1518500250,
|
|
|
|
2147483647
|
|
|
|
};
|
2000-05-29 22:46:12 +02:00
|
|
|
|
2000-05-17 01:44:38 +02:00
|
|
|
#else
|
2000-05-29 22:46:12 +02:00
|
|
|
|
2000-01-07 16:02:05 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_Sqrt32 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* Computes the square root of an Int32 integer (which will be */
|
2000-05-30 00:40:57 +02:00
|
|
|
/* handled as an unsigned long value). */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* x :: The value to compute the root for. */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `sqrt(x)'. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt32( FT_Int32 x )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_ULong val, root, newroot, mask;
|
2000-01-02 10:41:30 +01:00
|
|
|
|
2000-01-10 18:19:45 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
root = 0;
|
2000-05-29 22:46:12 +02:00
|
|
|
mask = 0x40000000L;
|
1999-12-17 00:11:37 +01:00
|
|
|
val = (FT_ULong)x;
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
do
|
|
|
|
{
|
2000-01-02 10:41:30 +01:00
|
|
|
newroot = root + mask;
|
2000-01-07 16:02:05 +01:00
|
|
|
if ( newroot <= val )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
val -= newroot;
|
2000-01-07 16:02:05 +01:00
|
|
|
root = newroot + mask;
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
root >>= 1;
|
|
|
|
mask >>= 2;
|
|
|
|
}
|
2000-01-02 10:41:30 +01:00
|
|
|
while ( mask != 0 );
|
2000-01-10 18:19:45 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
return root;
|
|
|
|
}
|
2000-05-29 22:46:12 +02:00
|
|
|
|
2000-06-12 21:36:41 +02:00
|
|
|
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
|
2000-01-10 18:19:45 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
#ifdef LONG64
|
1999-12-17 00:11:37 +01:00
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_MulDiv */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* A very simple function used to perform the computation `(a*b)/c' */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* with maximal accuracy (it uses a 64-bit intermediate integer */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* whenever necessary). */
|
|
|
|
/* */
|
|
|
|
/* This function isn't necessarily as fast as some processor specific */
|
|
|
|
/* operations, but is at least completely portable. */
|
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. */
|
|
|
|
/* c :: The divisor. */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*b)/c'. This function never traps when trying to */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* on the signs of `a' and `b'. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_MulDiv( FT_Long a,
|
|
|
|
FT_Long b,
|
|
|
|
FT_Long c )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int s;
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
s = 1;
|
|
|
|
if ( a < 0 ) { a = -a; s = -s; }
|
|
|
|
if ( b < 0 ) { b = -b; s = -s; }
|
|
|
|
if ( c < 0 ) { c = -c; s = -s; }
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
return s * ( c > 0 ? ( (FT_Int64)a * b + ( c >> 1 ) ) / c
|
|
|
|
: 0x7FFFFFFFL );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_MulFix */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* A very simple function used to perform the computation */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* `(a*b)/0x10000' with maximal accuracy. Most of the time this is */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* used to multiply a given value by a 16.16 fixed float factor. */
|
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. Use a 16.16 factor here whenever */
|
|
|
|
/* possible (see note below). */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*b)/0x10000'. */
|
|
|
|
/* */
|
|
|
|
/* <Note> */
|
|
|
|
/* This function has been optimized for the case where the absolute */
|
|
|
|
/* value of `a' is less than 2048, and `b' is a 16.16 scaling factor. */
|
|
|
|
/* As this happens mainly when scaling from notional units to */
|
|
|
|
/* fractional pixels in FreeType, it resulted in noticeable speed */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* improvements between versions 2.x and 1.x. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* As a conclusion, always try to place a 16.16 factor as the */
|
|
|
|
/* _second_ argument of this function; this can make a great */
|
|
|
|
/* difference. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_MulFix( FT_Long a,
|
|
|
|
FT_Long b )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
2000-05-30 00:40:57 +02:00
|
|
|
FT_Int s;
|
1999-12-17 00:11:37 +01:00
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
s = 1;
|
|
|
|
if ( a < 0 ) { a = -a; s = -s; }
|
|
|
|
if ( b < 0 ) { b = -b; s = -s; }
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
return s * (FT_Long)( ( (FT_Int64)a * b + 0x8000 ) >> 16 );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_DivFix */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* A very simple function used to perform the computation */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* `(a*0x10000)/b' with maximal accuracy. Most of the time, this is */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* used to divide a given value by a 16.16 fixed float factor. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. Use a 16.16 factor here whenever */
|
|
|
|
/* possible (see note below). */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*0x10000)/b'. */
|
|
|
|
/* */
|
|
|
|
/* <Note> */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* The optimization for FT_DivFix() is simple: If (a << 16) fits in */
|
|
|
|
/* 32 bits, then the division is computed directly. Otherwise, we */
|
|
|
|
/* use a specialized version of the old FT_MulDiv64(). */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_DivFix( FT_Long a,
|
|
|
|
FT_Long b )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int32 s;
|
|
|
|
FT_Word32 q;
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
s = a; a = ABS(a);
|
|
|
|
s ^= b; b = ABS(b);
|
|
|
|
|
|
|
|
if ( b == 0 )
|
2000-05-30 00:40:57 +02:00
|
|
|
/* check for division by 0 */
|
2000-05-29 22:46:12 +02:00
|
|
|
q = 0x7FFFFFFFL;
|
2000-01-10 18:19:45 +01:00
|
|
|
else
|
1999-12-17 00:11:37 +01:00
|
|
|
/* compute result directly */
|
2000-05-30 00:40:57 +02:00
|
|
|
q = ( (FT_Int64)a << 16 ) / b;
|
1999-12-17 00:11:37 +01:00
|
|
|
|
|
|
|
return (FT_Int32)( s < 0 ? -q : q );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
#ifdef FT_CONFIG_OPTION_OLD_CALCS
|
2000-05-29 22:46:12 +02:00
|
|
|
|
|
|
|
/* a helper function for FT_Sqrt64() */
|
|
|
|
|
|
|
|
static
|
|
|
|
int ft_order64( FT_Int64 z )
|
|
|
|
{
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while ( z )
|
|
|
|
{
|
|
|
|
z = (unsigned INT64)z >> 1;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return j - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_Sqrt64 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* Computes the square root of a 64-bit value. That sounds stupid, */
|
|
|
|
/* but it is needed to obtain maximal accuracy in the TrueType */
|
|
|
|
/* bytecode interpreter. */
|
2000-05-02 12:52:28 +02:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
2000-05-30 18:49:14 +02:00
|
|
|
/* l :: A 64-bit integer. */
|
2000-05-02 12:52:28 +02:00
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The 32-bit square-root. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt64( FT_Int64 l )
|
2000-05-02 12:52:28 +02:00
|
|
|
{
|
|
|
|
FT_Int64 r, s;
|
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
if ( l <= 0 ) return 0;
|
|
|
|
if ( l == 1 ) return 1;
|
|
|
|
|
|
|
|
r = ft_square_roots[ft_order64( l )];
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
s = r;
|
2000-05-30 00:40:57 +02:00
|
|
|
r = ( r + l / r ) >> 1;
|
2000-05-02 12:52:28 +02:00
|
|
|
}
|
2000-05-30 00:40:57 +02:00
|
|
|
while ( r > s || r * r > l );
|
2000-05-02 12:52:28 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2000-05-29 22:46:12 +02:00
|
|
|
|
|
|
|
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
|
2000-05-02 12:52:28 +02:00
|
|
|
|
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
#else /* LONG64 */
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_MulDiv */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* A very simple function used to perform the computation `(a*b)/c' */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* with maximal accuracy (it uses a 64-bit intermediate integer */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* whenever necessary). */
|
|
|
|
/* */
|
|
|
|
/* This function isn't necessarily as fast as some processor specific */
|
|
|
|
/* operations, but is at least completely portable. */
|
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. */
|
|
|
|
/* c :: The divisor. */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*b)/c'. This function never traps when trying to */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* divide by zero; it simply returns `MaxInt' or `MinInt' depending */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* on the signs of `a' and `b'. */
|
|
|
|
/* */
|
|
|
|
/* <Note> */
|
|
|
|
/* The FT_MulDiv() function has been optimized thanks to ideas from */
|
|
|
|
/* Graham Asher. The trick is to optimize computation if everything */
|
|
|
|
/* fits within 32 bits (a rather common case). */
|
|
|
|
/* */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* We compute `a*b+c/2', then divide it by `c' (positive values). */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* 46340 is FLOOR(SQRT(2^31-1)). */
|
|
|
|
/* */
|
|
|
|
/* if ( a <= 46340 && b <= 46340 ) then ( a*b <= 0x7FFEA810 ) */
|
|
|
|
/* */
|
|
|
|
/* 0x7FFFFFFF - 0x7FFEA810 = 0x157F0 */
|
|
|
|
/* */
|
|
|
|
/* if ( c < 0x157F0*2 ) then ( a*b+c/2 <= 0x7FFFFFFF ) */
|
|
|
|
/* */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* and 2*0x157F0 = 176096. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_MulDiv( FT_Long a,
|
|
|
|
FT_Long b,
|
|
|
|
FT_Long c )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
long s;
|
|
|
|
|
|
|
|
|
|
|
|
if ( a == 0 || b == c )
|
|
|
|
return a;
|
|
|
|
|
|
|
|
s = a; a = ABS( a );
|
|
|
|
s ^= b; b = ABS( b );
|
|
|
|
s ^= c; c = ABS( c );
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
if ( a <= 46340 && b <= 46340 && c <= 176095L && c > 0 )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
2000-05-30 00:40:57 +02:00
|
|
|
a = ( a * b + ( c >> 1 ) ) / c;
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
2000-05-30 00:40:57 +02:00
|
|
|
else if ( c > 0 )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int64 temp, temp2;
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
FT_MulTo64( a, b, &temp );
|
2000-05-30 00:40:57 +02:00
|
|
|
temp2.hi = (FT_Int32)( c >> 31 );
|
|
|
|
temp2.lo = (FT_Word32)( c / 2 );
|
1999-12-17 00:11:37 +01:00
|
|
|
FT_Add64( &temp, &temp2, &temp );
|
|
|
|
a = FT_Div64by32( &temp, c );
|
|
|
|
}
|
2000-05-29 22:55:13 +02:00
|
|
|
else
|
2000-05-30 00:40:57 +02:00
|
|
|
a = 0x7FFFFFFFL;
|
1999-12-17 00:11:37 +01:00
|
|
|
|
2000-06-07 02:00:08 +02:00
|
|
|
return ( s < 0 ? -a : a );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_MulFix */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* A very simple function used to perform the computation */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* `(a*b)/0x10000' with maximal accuracy. Most of the time, this is */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* used to multiply a given value by a 16.16 fixed float factor. */
|
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. Use a 16.16 factor here whenever */
|
|
|
|
/* possible (see note below). */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*b)/0x10000'. */
|
|
|
|
/* */
|
|
|
|
/* <Note> */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* The optimization for FT_MulFix() is different. We could simply be */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* happy by applying the same principles as with FT_MulDiv(), because */
|
|
|
|
/* */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* c = 0x10000 < 176096 */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* However, in most cases, we have a `b' with a value around 0x10000 */
|
|
|
|
/* which is greater than 46340. */
|
|
|
|
/* */
|
|
|
|
/* According to some testing, most cases have `a' < 2048, so a good */
|
|
|
|
/* idea is to use bounds like 2048 and 1048576 (=floor((2^31-1)/2048) */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* for `a' and `b', respectively. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_MulFix( FT_Long a,
|
|
|
|
FT_Long b )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Long s;
|
2000-03-28 13:17:58 +02:00
|
|
|
FT_ULong ua, ub;
|
2000-01-02 10:41:30 +01:00
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
if ( a == 0 || b == 0x10000L )
|
|
|
|
return a;
|
|
|
|
|
|
|
|
s = a; a = ABS(a);
|
|
|
|
s ^= b; b = ABS(b);
|
|
|
|
|
2000-03-28 13:17:58 +02:00
|
|
|
ua = (FT_ULong)a;
|
|
|
|
ub = (FT_ULong)b;
|
|
|
|
|
|
|
|
if ( ua <= 2048 && ub <= 1048576L )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
2000-05-30 00:40:57 +02:00
|
|
|
ua = ( ua * ub + 0x8000 ) >> 16;
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-03-28 13:17:58 +02:00
|
|
|
FT_ULong al = ua & 0xFFFF;
|
1999-12-17 00:11:37 +01:00
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
ua = ( ua >> 16 ) * ub +
|
|
|
|
al * ( ub >> 16 ) +
|
|
|
|
( al * ( ub & 0xFFFF ) >> 16 );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
2000-03-28 13:17:58 +02:00
|
|
|
return ( s < 0 ? -(FT_Long)ua : ua );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_DivFix */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* A very simple function used to perform the computation */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* `(a*0x10000)/b' with maximal accuracy. Most of the time, this is */
|
|
|
|
/* used to divide a given value by a 16.16 fixed float factor. */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* a :: The first multiplier. */
|
|
|
|
/* b :: The second multiplier. Use a 16.16 factor here whenever */
|
|
|
|
/* possible (see note below). */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `(a*0x10000)/b'. */
|
|
|
|
/* */
|
|
|
|
/* <Note> */
|
2000-05-30 00:40:57 +02:00
|
|
|
/* The optimization for FT_DivFix() is simple: If (a << 16) fits into */
|
2000-01-02 10:41:30 +01:00
|
|
|
/* 32 bits, then the division is computed directly. Otherwise, we */
|
|
|
|
/* use a specialized version of the old FT_MulDiv64(). */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Long ) FT_DivFix( FT_Long a,
|
|
|
|
FT_Long b )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int32 s;
|
|
|
|
FT_Word32 q;
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
s = a; a = ABS(a);
|
|
|
|
s ^= b; b = ABS(b);
|
|
|
|
|
|
|
|
if ( b == 0 )
|
2000-05-30 00:40:57 +02:00
|
|
|
{
|
|
|
|
/* check for division by 0 */
|
2000-05-29 22:46:12 +02:00
|
|
|
q = 0x7FFFFFFFL;
|
2000-05-30 00:40:57 +02:00
|
|
|
}
|
|
|
|
else if ( ( a >> 16 ) == 0 )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
/* compute result directly */
|
2000-05-30 00:40:57 +02:00
|
|
|
q = (FT_Word32)( a << 16 ) / (FT_Word32)b;
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we need more bits, we'll have to do it by hand */
|
|
|
|
FT_Word32 c;
|
|
|
|
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
q = ( a / b ) << 16;
|
|
|
|
c = a % b;
|
|
|
|
|
|
|
|
/* we must compute C*0x10000/B; we simply shift C and B so */
|
1999-12-17 00:11:37 +01:00
|
|
|
/* C becomes smaller than 16 bits */
|
2000-01-02 10:41:30 +01:00
|
|
|
while ( c >> 16 )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
c >>= 1;
|
|
|
|
b <<= 1;
|
|
|
|
}
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
q += ( c << 16 ) / b;
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-07 16:02:05 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_Add64 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* Add two Int64 values. */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* x :: A pointer to the first value to be added. */
|
|
|
|
/* y :: A pointer to the second value to be added. */
|
|
|
|
/* */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* <Output> */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* z :: A pointer to the result of `x + y'. */
|
|
|
|
/* */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* <Note> */
|
|
|
|
/* Will be wrapped by the ADD_64() macro. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( void ) FT_Add64( FT_Int64* x,
|
|
|
|
FT_Int64* y,
|
|
|
|
FT_Int64* z )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
register FT_Word32 lo, hi;
|
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
lo = x->lo + y->lo;
|
|
|
|
hi = x->hi + y->hi + ( lo < x->lo );
|
|
|
|
|
|
|
|
z->lo = lo;
|
|
|
|
z->hi = hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-07 16:02:05 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_MulTo64 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* Multiplies two Int32 integers. Returns an Int64 integer. */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* x :: The first multiplier. */
|
|
|
|
/* y :: The second multiplier. */
|
|
|
|
/* */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* <Output> */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* z :: A pointer to the result of `x * y'. */
|
|
|
|
/* */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* <Note> */
|
|
|
|
/* Will be wrapped by the MUL_64() macro. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( void ) FT_MulTo64( FT_Int32 x,
|
|
|
|
FT_Int32 y,
|
|
|
|
FT_Int64* z )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int32 s;
|
|
|
|
|
|
|
|
|
|
|
|
s = x; x = ABS( x );
|
|
|
|
s ^= y; y = ABS( y );
|
|
|
|
|
|
|
|
{
|
|
|
|
FT_Word32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
|
2000-01-02 10:41:30 +01:00
|
|
|
|
2000-01-10 18:19:45 +01:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
lo1 = x & 0x0000FFFF; hi1 = x >> 16;
|
|
|
|
lo2 = y & 0x0000FFFF; hi2 = y >> 16;
|
|
|
|
|
|
|
|
lo = lo1 * lo2;
|
|
|
|
i1 = lo1 * hi2;
|
|
|
|
i2 = lo2 * hi1;
|
|
|
|
hi = hi1 * hi2;
|
|
|
|
|
|
|
|
/* Check carry overflow of i1 + i2 */
|
|
|
|
i1 += i2;
|
|
|
|
if ( i1 < i2 )
|
|
|
|
hi += 1L << 16;
|
|
|
|
|
2000-01-02 10:41:30 +01:00
|
|
|
hi += i1 >> 16;
|
1999-12-17 00:11:37 +01:00
|
|
|
i1 = i1 << 16;
|
|
|
|
|
|
|
|
/* Check carry overflow of i1 + lo */
|
|
|
|
lo += i1;
|
2000-05-30 00:40:57 +02:00
|
|
|
hi += ( lo < i1 );
|
1999-12-17 00:11:37 +01:00
|
|
|
|
|
|
|
z->lo = lo;
|
|
|
|
z->hi = hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( s < 0 )
|
|
|
|
{
|
|
|
|
z->lo = (FT_Word32)-(FT_Int32)z->lo;
|
|
|
|
z->hi = ~z->hi + !(z->lo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-07 16:02:05 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_Div64by32 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
|
|
|
/* Divides an Int64 value by an Int32 value. Returns an Int32 */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* integer. */
|
2000-01-07 16:02:05 +01:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
|
|
|
/* x :: A pointer to the dividend. */
|
|
|
|
/* y :: The divisor. */
|
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The result of `x / y'. */
|
|
|
|
/* */
|
2000-01-08 18:10:33 +01:00
|
|
|
/* <Note> */
|
|
|
|
/* Will be wrapped by the DIV_64() macro. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Int32 ) FT_Div64by32( FT_Int64* x,
|
|
|
|
FT_Int32 y )
|
1999-12-17 00:11:37 +01:00
|
|
|
{
|
|
|
|
FT_Int32 s;
|
|
|
|
FT_Word32 q, r, i, lo;
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
s = x->hi;
|
|
|
|
if ( s < 0 )
|
|
|
|
{
|
|
|
|
x->lo = (FT_Word32)-(FT_Int32)x->lo;
|
|
|
|
x->hi = ~x->hi + !(x->lo);
|
|
|
|
}
|
|
|
|
s ^= y; y = ABS( y );
|
|
|
|
|
|
|
|
/* Shortcut */
|
|
|
|
if ( x->hi == 0 )
|
|
|
|
{
|
2000-05-30 00:40:57 +02:00
|
|
|
if ( y > 0 )
|
|
|
|
q = x->lo / y;
|
|
|
|
else
|
|
|
|
q = 0x7FFFFFFFL;
|
2000-05-29 22:55:13 +02:00
|
|
|
|
2000-06-07 02:00:08 +02:00
|
|
|
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r = x->hi;
|
|
|
|
lo = x->lo;
|
|
|
|
|
|
|
|
if ( r >= (FT_Word32)y ) /* we know y is to be treated as unsigned here */
|
2000-06-07 02:00:08 +02:00
|
|
|
return ( s < 0 ? 0x80000001UL : 0x7FFFFFFFUL );
|
2000-05-30 00:40:57 +02:00
|
|
|
/* Return Max/Min Int32 if division overflow. */
|
|
|
|
/* This includes division by zero! */
|
1999-12-17 00:11:37 +01:00
|
|
|
q = 0;
|
|
|
|
for ( i = 0; i < 32; i++ )
|
|
|
|
{
|
|
|
|
r <<= 1;
|
|
|
|
q <<= 1;
|
|
|
|
r |= lo >> 31;
|
|
|
|
|
|
|
|
if ( r >= (FT_Word32)y )
|
|
|
|
{
|
|
|
|
r -= y;
|
|
|
|
q |= 1;
|
|
|
|
}
|
|
|
|
lo <<= 1;
|
|
|
|
}
|
|
|
|
|
2000-06-07 02:00:08 +02:00
|
|
|
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
|
1999-12-17 00:11:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
#ifdef FT_CONFIG_OPTION_OLD_CALCS
|
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
|
|
|
|
/* two helper functions for FT_Sqrt64() */
|
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
static
|
2000-05-29 22:46:12 +02:00
|
|
|
void FT_Sub64( FT_Int64* x,
|
|
|
|
FT_Int64* y,
|
|
|
|
FT_Int64* z )
|
2000-05-02 12:52:28 +02:00
|
|
|
{
|
|
|
|
register FT_Word32 lo, hi;
|
|
|
|
|
|
|
|
|
|
|
|
lo = x->lo - y->lo;
|
|
|
|
hi = x->hi - y->hi - ( (FT_Int32)lo < 0 );
|
|
|
|
|
|
|
|
z->lo = lo;
|
|
|
|
z->hi = hi;
|
|
|
|
}
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
|
2000-05-29 22:46:12 +02:00
|
|
|
static
|
|
|
|
int ft_order64( FT_Int64* z )
|
|
|
|
{
|
|
|
|
FT_Word32 i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
|
|
|
|
i = z->lo;
|
|
|
|
j = 0;
|
|
|
|
if ( z->hi )
|
|
|
|
{
|
|
|
|
i = z->hi;
|
|
|
|
j = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( i > 0 )
|
|
|
|
{
|
|
|
|
i >>= 1;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return j - 1;
|
|
|
|
}
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
|
2000-05-02 12:52:28 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* <Function> */
|
|
|
|
/* FT_Sqrt64 */
|
|
|
|
/* */
|
|
|
|
/* <Description> */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* Computes the square root of a 64-bits value. That sounds stupid, */
|
|
|
|
/* but it is needed to obtain maximal accuracy in the TrueType */
|
|
|
|
/* bytecode interpreter. */
|
2000-05-02 12:52:28 +02:00
|
|
|
/* */
|
|
|
|
/* <Input> */
|
2000-05-29 22:46:12 +02:00
|
|
|
/* z :: A pointer to a 64-bit integer. */
|
2000-05-02 12:52:28 +02:00
|
|
|
/* */
|
|
|
|
/* <Return> */
|
|
|
|
/* The 32-bit square-root. */
|
|
|
|
/* */
|
2000-05-29 22:46:12 +02:00
|
|
|
FT_EXPORT_FUNC( FT_Int32 ) FT_Sqrt64( FT_Int64* l )
|
2000-05-02 12:52:28 +02:00
|
|
|
{
|
|
|
|
FT_Int64 l2;
|
|
|
|
FT_Int32 r, s;
|
|
|
|
|
|
|
|
|
2000-05-30 00:40:57 +02:00
|
|
|
if ( (FT_Int32)l->hi < 0 ||
|
|
|
|
( l->hi == 0 && l->lo == 0 ) )
|
2000-05-29 22:46:12 +02:00
|
|
|
return 0;
|
2000-05-02 12:52:28 +02:00
|
|
|
|
|
|
|
s = ft_order64( l );
|
2000-05-30 00:40:57 +02:00
|
|
|
if ( s == 0 )
|
|
|
|
return 1;
|
2000-05-02 12:52:28 +02:00
|
|
|
|
|
|
|
r = ft_square_roots[s];
|
|
|
|
do
|
|
|
|
{
|
|
|
|
s = r;
|
2000-05-30 00:40:57 +02:00
|
|
|
r = ( r + FT_Div64by32( l, r ) ) >> 1;
|
2000-05-02 12:52:28 +02:00
|
|
|
FT_MulTo64( r, r, &l2 );
|
|
|
|
FT_Sub64 ( l, &l2, &l2 );
|
|
|
|
}
|
|
|
|
while ( r > s || (FT_Int32)l2.hi < 0 );
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
|
|
|
|
|
1999-12-17 00:11:37 +01:00
|
|
|
#endif /* LONG64 */
|
|
|
|
|
|
|
|
|
|
|
|
/* END */
|