freetype2/src/base/ftcalc.c

634 lines
14 KiB
C
Raw Normal View History

1999-12-17 00:11:37 +01:00
/***************************************************************************/
/* */
/* ftcalc.c */
/* */
/* Arithmetic computations (body). */
/* */
/* Copyright 1996-2000 by */
1999-12-17 00:11:37 +01:00
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* 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. */
/* */
/*************************************************************************/
/*************************************************************************/
/* */
/* Implementing basic computation routines. */
1999-12-17 00:11:37 +01:00
/* */
/* FT_MulDiv(), FT_MulFix(), FT_DivFix(), FT_RoundFix(), FT_CeilFix(), */
/* and FT_FloorFix() are declared in freetype.h. */
1999-12-17 00:11:37 +01:00
/* */
/*************************************************************************/
2000-12-08 17:17:16 +01:00
#include <ft2build.h>
#include FT_INTERNAL_CALC_H
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_OBJECTS_H
1999-12-17 00:11:37 +01:00
/* we need to define a 64-bits data type here */
#ifndef FT_CONFIG_OPTION_OLD_CALCS
# ifdef FT_LONG64
typedef FT_INT64 FT_Int64;
# else
typedef struct FT_Int64_
{
FT_UInt32 lo;
FT_UInt32 hi;
} FT_Int64;
# endif
#endif
/*************************************************************************/
/* */
/* 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
/* The following three functions are available regardless of whether */
/* FT_LONG64 or FT_CONFIG_OPTION_OLD_CALCS is defined. */
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed ) FT_RoundFix( FT_Fixed a )
{
return ( a >= 0 ) ? ( a + 0x8000L ) & -0x10000L
: -((-a + 0x8000L ) & -0x10000L );
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed ) FT_CeilFix( FT_Fixed a )
{
return ( a >= 0 ) ? ( a + 0xFFFFL ) & -0x10000L
: -((-a + 0xFFFFL ) & -0x10000L );
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Fixed ) FT_FloorFix( FT_Fixed a )
{
return ( a >= 0 ) ? a & -0x10000L
: -((-a) & -0x10000L );
}
#ifdef FT_CONFIG_OPTION_OLD_CALCS
static const FT_Long ft_square_roots[63] =
{
1L, 1L, 2L, 3L, 4L, 5L, 8L, 11L,
16L, 22L, 32L, 45L, 64L, 90L, 128L, 181L,
256L, 362L, 512L, 724L, 1024L, 1448L, 2048L, 2896L,
4096L, 5892L, 8192L, 11585L, 16384L, 23170L, 32768L, 46340L,
65536L, 92681L, 131072L, 185363L, 262144L, 370727L,
524288L, 741455L, 1048576L, 1482910L, 2097152L, 2965820L,
4194304L, 5931641L, 8388608L, 11863283L, 16777216L, 23726566L,
33554432L, 47453132L, 67108864L, 94906265L,
134217728L, 189812531L, 268435456L, 379625062L,
536870912L, 759250125L, 1073741824L, 1518500250L,
2147483647L
};
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
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( FT_Int32 ) FT_Sqrt32( FT_Int32 x )
1999-12-17 00:11:37 +01:00
{
FT_ULong val, root, newroot, mask;
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;
1999-12-17 00:11:37 +01:00
do
{
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
}
1999-12-17 00:11:37 +01:00
root >>= 1;
mask >>= 2;
} while ( mask != 0 );
1999-12-17 00:11:37 +01:00
return root;
}
2000-05-29 22:46:12 +02:00
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
1999-12-17 00:11:37 +01:00
#ifdef FT_LONG64
1999-12-17 00:11:37 +01:00
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long ) FT_MulDiv( FT_Long a,
FT_Long b,
FT_Long c )
1999-12-17 00:11:37 +01:00
{
FT_Int s;
FT_Long d;
2000-05-30 00:40:57 +02:00
2001-05-08 15:52:13 +02:00
1999-12-17 00:11:37 +01:00
s = 1;
if ( a < 0 ) { a = -a; s = -1; }
1999-12-17 00:11:37 +01:00
if ( b < 0 ) { b = -b; s = -s; }
if ( c < 0 ) { c = -c; s = -s; }
d = (FT_Long)( c > 0 ? ( (FT_Int64)a * b + ( c >> 1 ) ) / c
: 0x7FFFFFFFL );
2001-05-08 15:52:13 +02:00
return ( s > 0 ) ? d : -d;
1999-12-17 00:11:37 +01:00
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long ) FT_MulFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
FT_Int s = 1;
2001-05-08 15:52:13 +02:00
FT_Long c;
1999-12-17 00:11:37 +01:00
if ( a < 0 ) { a = -a; s = -1; }
1999-12-17 00:11:37 +01:00
if ( b < 0 ) { b = -b; s = -s; }
c = (FT_Long)( ( (FT_Int64)a * b + 0x8000 ) >> 16 );
2001-05-08 15:52:13 +02:00
return ( s > 0 ) ? c : -c ;
1999-12-17 00:11:37 +01:00
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long ) FT_DivFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
FT_UInt32 q;
1999-12-17 00:11:37 +01:00
s = 1;
if ( a < 0 ) { a = -a; s = -1; }
if ( b < 0 ) { b = -b; s = -s; }
1999-12-17 00:11:37 +01:00
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;
else
1999-12-17 00:11:37 +01:00
/* compute result directly */
q = (FT_UInt32)( ( ( (FT_Int64)a << 16 ) + ( b >> 1 ) ) / b );
1999-12-17 00:11:37 +01:00
return ( s < 0 ? -(FT_Long)q : (FT_Long)q );
1999-12-17 00:11:37 +01: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 FT_INT64)z >> 1;
2000-05-29 22:46:12 +02:00
j++;
}
return j - 1;
}
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( FT_Int32 ) FT_Sqrt64( FT_Int64 l )
{
FT_Int64 r, s;
2000-05-29 22:46:12 +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;
} while ( r > s || r * r > l );
return (FT_Int32)r;
}
2000-05-29 22:46:12 +02:00
2001-04-26 02:21:48 +02:00
#endif /* FT_CONFIG_OPTION_OLD_CALCS */
#else /* FT_LONG64 */
2001-04-26 02:21:48 +02:00
static void
ft_multo64( FT_UInt32 x,
FT_UInt32 y,
FT_Int64 *z )
{
FT_UInt32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
2001-04-26 02:21:48 +02:00
2000-10-31 21:42:18 +01:00
lo1 = x & 0x0000FFFFU; hi1 = x >> 16;
lo2 = y & 0x0000FFFFU; hi2 = y >> 16;
lo = lo1 * lo2;
i1 = lo1 * hi2;
i2 = lo2 * hi1;
hi = hi1 * hi2;
2001-04-26 02:21:48 +02:00
/* Check carry overflow of i1 + i2 */
i1 += i2;
hi += (FT_UInt32)( i1 < i2 ) << 16;
2001-04-26 02:21:48 +02:00
hi += i1 >> 16;
i1 = i1 << 16;
2001-04-26 02:21:48 +02:00
/* Check carry overflow of i1 + lo */
lo += i1;
hi += ( lo < i1 );
2001-04-26 02:21:48 +02:00
z->lo = lo;
z->hi = hi;
}
2001-04-26 02:21:48 +02:00
static FT_UInt32
ft_div64by32( FT_UInt32 hi,
FT_UInt32 lo,
FT_UInt32 y )
2001-04-26 02:21:48 +02:00
{
FT_UInt r, q;
FT_Int i;
2001-04-26 02:21:48 +02:00
q = 0;
r = hi;
if ( r >= y )
return (FT_UInt32)0x7FFFFFFFL;
i = 32;
2001-04-26 02:21:48 +02:00
do
{
r <<= 1;
q <<= 1;
r |= lo >> 31;
if ( r >= (FT_UInt32)y )
{
r -= y;
q |= 1;
}
lo <<= 1;
} while ( --i );
return q;
2001-04-26 02:21:48 +02:00
}
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( void ) FT_Add64( FT_Int64* x,
FT_Int64* y,
FT_Int64 *z )
{
register FT_UInt32 lo, hi, max;
2001-04-26 02:21:48 +02:00
max = x->lo > y->lo ? x->lo : y->lo;
lo = x->lo + y->lo;
hi = x->hi + y->hi + ( lo < max );
z->lo = lo;
z->hi = hi;
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long ) FT_MulDiv( FT_Long a,
FT_Long b,
FT_Long c )
1999-12-17 00:11:37 +01:00
{
long s;
1999-12-17 00:11:37 +01:00
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
2001-04-26 02:21:48 +02:00
ft_multo64( a, b, &temp );
temp2.hi = 0;
temp2.lo = (FT_UInt32)(c >> 1);
FT_Add64( &temp, &temp2, &temp );
2001-04-26 02:21:48 +02:00
a = ft_div64by32( temp.hi, temp.lo, b );
1999-12-17 00:11:37 +01:00
}
else
2000-05-30 00:40:57 +02:00
a = 0x7FFFFFFFL;
1999-12-17 00:11:37 +01:00
return ( s < 0 ? -a : a );
1999-12-17 00:11:37 +01:00
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( 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-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
ua = ( ua >> 16 ) * ub + al * ( ub >> 16 ) +
( ( al * ( ub & 0xFFFF ) + 0x8000 ) >> 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
}
/* documentation is in freetype.h */
FT_EXPORT_DEF( FT_Long ) FT_DivFix( FT_Long a,
FT_Long b )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
FT_UInt32 q;
1999-12-17 00:11:37 +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 */
2001-04-26 02:21:48 +02:00
q = (FT_UInt32)( (a << 16) + (b >> 1) ) / (FT_UInt32)b;
1999-12-17 00:11:37 +01:00
}
else
{
/* we need more bits; we have to do it by hand */
FT_Int64 temp, temp2;
1999-12-17 00:11:37 +01:00
temp.hi = (FT_Int32) (a >> 16);
temp.lo = (FT_UInt32)(a << 16);
2001-04-26 02:21:48 +02:00
temp2.hi = 0;
temp2.lo = (FT_UInt32)( b >> 1 );
FT_Add64( &temp, &temp2, &temp );
2001-04-26 02:21:48 +02:00
q = ft_div64by32( temp.hi, temp.lo, b );
1999-12-17 00:11:37 +01:00
}
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
}
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( void ) FT_MulTo64( FT_Int32 x,
FT_Int32 y,
FT_Int64 *z )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
1999-12-17 00:11:37 +01:00
s = x; x = ABS( x );
s ^= y; y = ABS( y );
2001-04-26 02:21:48 +02:00
ft_multo64( x, y, z );
1999-12-17 00:11:37 +01:00
if ( s < 0 )
{
z->lo = (FT_UInt32)-(FT_Int32)z->lo;
z->hi = ~z->hi + !( z->lo );
1999-12-17 00:11:37 +01:00
}
}
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( FT_Int32 ) FT_Div64by32( FT_Int64* x,
FT_Int32 y )
1999-12-17 00:11:37 +01:00
{
FT_Int32 s;
2001-04-26 02:21:48 +02:00
FT_UInt32 q;
1999-12-17 00:11:37 +01:00
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_UInt32)-(FT_Int32)x->lo;
x->hi = ~x->hi + !( x->lo );
1999-12-17 00:11:37 +01:00
}
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 >> 1 ) ) / y;
2000-05-30 00:40:57 +02:00
else
q = 0x7FFFFFFFL;
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
1999-12-17 00:11:37 +01:00
}
2001-04-26 02:21:48 +02:00
q = ft_div64by32( x->hi, x->lo, y );
return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
1999-12-17 00:11:37 +01:00
}
#ifdef FT_CONFIG_OPTION_OLD_CALCS
2000-05-29 22:46:12 +02:00
/* two helper functions for FT_Sqrt64() */
static
2000-05-29 22:46:12 +02:00
void FT_Sub64( FT_Int64* x,
FT_Int64* y,
FT_Int64* z )
{
register FT_UInt32 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_UInt32 i;
2000-05-29 22:46:12 +02:00
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
/* documentation is in ftcalc.h */
FT_EXPORT_DEF( FT_Int32 ) FT_Sqrt64( FT_Int64* l )
{
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;
s = ft_order64( l );
2000-05-30 00:40:57 +02:00
if ( s == 0 )
return 1;
r = ft_square_roots[s];
do
{
s = r;
2000-05-30 00:40:57 +02:00
r = ( r + FT_Div64by32( l, r ) ) >> 1;
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 */
#endif /* FT_LONG64 */
/* a not-so-fast but working 16.16 fixed point square root function */
FT_EXPORT_DEF( FT_Int32 ) FT_SqrtFixed( FT_Int32 x )
{
FT_UInt32 root, rem_hi, rem_lo, test_div;
FT_Int count;
2000-10-31 21:42:18 +01:00
root = 0;
if ( x > 0 )
{
rem_hi = 0;
rem_lo = x;
count = 24;
do
{
rem_hi = ( rem_hi << 2 ) | ( rem_lo >> 30 );
rem_lo <<= 2;
root <<= 1;
test_div = ( root << 1 ) + 1;
if ( rem_hi >= test_div )
{
rem_hi -= test_div;
root += 1;
}
} while ( --count );
}
2000-09-12 00:50:13 +02:00
return (FT_Int32)root;
}
1999-12-17 00:11:37 +01:00
/* END */