1 /***************************************************************************/
5 /* FreeType trigonometric functions (body). */
7 /* Copyright 2001-2005, 2012-2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
18 /*************************************************************************/
20 /* This is a fixed-point CORDIC implementation of trigonometric */
21 /* functions as well as transformations between Cartesian and polar */
22 /* coordinates. The angles are represented as 16.16 fixed-point values */
23 /* in degrees, i.e., the angular resolution is 2^-16 degrees. Note that */
24 /* only vectors longer than 2^16*180/pi (or at least 22 bits) on a */
25 /* discrete Cartesian grid can have the same or better angular */
26 /* resolution. Therefore, to maintain this precision, some functions */
27 /* require an interim upscaling of the vectors, whereas others operate */
28 /* with 24-bit long vectors directly. */
30 /*************************************************************************/
33 #include FT_INTERNAL_OBJECTS_H
34 #include FT_INTERNAL_CALC_H
35 #include FT_TRIGONOMETRY_H
38 /* the Cordic shrink factor 0.858785336480436 * 2^32 */
39 #define FT_TRIG_SCALE 0xDBD95B16UL
41 /* the highest bit in overflow-safe vector components, */
42 /* MSB of 0.858785336480436 * sqrt(0.5) * 2^30 */
43 #define FT_TRIG_SAFE_MSB 29
45 /* this table was generated for FT_PI = 180L << 16, i.e. degrees */
46 #define FT_TRIG_MAX_ITERS 23
49 ft_trig_arctan_table[] =
51 1740967L, 919879L, 466945L, 234379L, 117304L, 58666L, 29335L,
52 14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
53 57L, 29L, 14L, 7L, 4L, 2L, 1L
59 /* multiply a given value by the CORDIC shrink factor */
61 ft_trig_downscale( FT_Fixed val )
70 v = ( val * (FT_Int64)FT_TRIG_SCALE ) + 0x100000000UL;
71 val = (FT_Fixed)( v >> 32 );
73 return ( s >= 0 ) ? val : -val;
76 #else /* !FT_LONG64 */
78 /* multiply a given value by the CORDIC shrink factor */
80 ft_trig_downscale( FT_Fixed val )
83 FT_UInt32 v1, v2, k1, k2, hi, lo1, lo2, lo3;
89 v1 = (FT_UInt32)val >> 16;
90 v2 = (FT_UInt32)( val & 0xFFFFL );
92 k1 = (FT_UInt32)FT_TRIG_SCALE >> 16; /* constant */
93 k2 = (FT_UInt32)( FT_TRIG_SCALE & 0xFFFFL ); /* constant */
96 lo1 = k1 * v2 + k2 * v1; /* can't overflow */
98 lo2 = ( k2 * v2 ) >> 16;
99 lo3 = FT_MAX( lo1, lo2 );
104 hi += (FT_UInt32)0x10000UL;
108 return ( s >= 0 ) ? val : -val;
111 #endif /* !FT_LONG64 */
115 ft_trig_prenorm( FT_Vector* vec )
124 shift = FT_MSB( FT_ABS( x ) | FT_ABS( y ) );
126 if ( shift <= FT_TRIG_SAFE_MSB )
128 shift = FT_TRIG_SAFE_MSB - shift;
129 vec->x = (FT_Pos)( (FT_ULong)x << shift );
130 vec->y = (FT_Pos)( (FT_ULong)y << shift );
134 shift -= FT_TRIG_SAFE_MSB;
145 ft_trig_pseudo_rotate( FT_Vector* vec,
149 FT_Fixed x, y, xtemp, b;
150 const FT_Fixed *arctanptr;
156 /* Rotate inside [-PI/4,PI/4] sector */
157 while ( theta < -FT_ANGLE_PI4 )
162 theta += FT_ANGLE_PI2;
165 while ( theta > FT_ANGLE_PI4 )
170 theta -= FT_ANGLE_PI2;
173 arctanptr = ft_trig_arctan_table;
175 /* Pseudorotations, with right shifts */
176 for ( i = 1, b = 1; i < FT_TRIG_MAX_ITERS; b <<= 1, i++ )
180 xtemp = x + ( ( y + b ) >> i );
181 y = y - ( ( x + b ) >> i );
183 theta += *arctanptr++;
187 xtemp = x - ( ( y + b ) >> i );
188 y = y + ( ( x + b ) >> i );
190 theta -= *arctanptr++;
200 ft_trig_pseudo_polarize( FT_Vector* vec )
204 FT_Fixed x, y, xtemp, b;
205 const FT_Fixed *arctanptr;
211 /* Get the vector into [-PI/4,PI/4] sector */
216 theta = FT_ANGLE_PI2;
223 theta = y > 0 ? FT_ANGLE_PI : -FT_ANGLE_PI;
232 theta = -FT_ANGLE_PI2;
243 arctanptr = ft_trig_arctan_table;
245 /* Pseudorotations, with right shifts */
246 for ( i = 1, b = 1; i < FT_TRIG_MAX_ITERS; b <<= 1, i++ )
250 xtemp = x + ( ( y + b ) >> i );
251 y = y - ( ( x + b ) >> i );
253 theta += *arctanptr++;
257 xtemp = x - ( ( y + b ) >> i );
258 y = y + ( ( x + b ) >> i );
260 theta -= *arctanptr++;
266 theta = FT_PAD_ROUND( theta, 32 );
268 theta = -FT_PAD_ROUND( -theta, 32 );
275 /* documentation is in fttrigon.h */
277 FT_EXPORT_DEF( FT_Fixed )
278 FT_Cos( FT_Angle angle )
283 v.x = FT_TRIG_SCALE >> 8;
285 ft_trig_pseudo_rotate( &v, angle );
287 return ( v.x + 0x80L ) >> 8;
291 /* documentation is in fttrigon.h */
293 FT_EXPORT_DEF( FT_Fixed )
294 FT_Sin( FT_Angle angle )
296 return FT_Cos( FT_ANGLE_PI2 - angle );
300 /* documentation is in fttrigon.h */
302 FT_EXPORT_DEF( FT_Fixed )
303 FT_Tan( FT_Angle angle )
308 v.x = FT_TRIG_SCALE >> 8;
310 ft_trig_pseudo_rotate( &v, angle );
312 return FT_DivFix( v.y, v.x );
316 /* documentation is in fttrigon.h */
318 FT_EXPORT_DEF( FT_Angle )
319 FT_Atan2( FT_Fixed dx,
325 if ( dx == 0 && dy == 0 )
330 ft_trig_prenorm( &v );
331 ft_trig_pseudo_polarize( &v );
337 /* documentation is in fttrigon.h */
339 FT_EXPORT_DEF( void )
340 FT_Vector_Unit( FT_Vector* vec,
343 vec->x = FT_TRIG_SCALE >> 8;
345 ft_trig_pseudo_rotate( vec, angle );
346 vec->x = ( vec->x + 0x80L ) >> 8;
347 vec->y = ( vec->y + 0x80L ) >> 8;
351 /* these macros return 0 for positive numbers,
352 and -1 for negative ones */
353 #define FT_SIGN_LONG( x ) ( (x) >> ( FT_SIZEOF_LONG * 8 - 1 ) )
354 #define FT_SIGN_INT( x ) ( (x) >> ( FT_SIZEOF_INT * 8 - 1 ) )
355 #define FT_SIGN_INT32( x ) ( (x) >> 31 )
356 #define FT_SIGN_INT16( x ) ( (x) >> 15 )
359 /* documentation is in fttrigon.h */
361 FT_EXPORT_DEF( void )
362 FT_Vector_Rotate( FT_Vector* vec,
372 if ( angle && ( v.x != 0 || v.y != 0 ) )
374 shift = ft_trig_prenorm( &v );
375 ft_trig_pseudo_rotate( &v, angle );
376 v.x = ft_trig_downscale( v.x );
377 v.y = ft_trig_downscale( v.y );
381 FT_Int32 half = (FT_Int32)1L << ( shift - 1 );
384 vec->x = ( v.x + half + FT_SIGN_LONG( v.x ) ) >> shift;
385 vec->y = ( v.y + half + FT_SIGN_LONG( v.y ) ) >> shift;
390 vec->x = (FT_Pos)( (FT_ULong)v.x << shift );
391 vec->y = (FT_Pos)( (FT_ULong)v.y << shift );
397 /* documentation is in fttrigon.h */
399 FT_EXPORT_DEF( FT_Fixed )
400 FT_Vector_Length( FT_Vector* vec )
408 /* handle trivial cases */
411 return FT_ABS( v.y );
415 return FT_ABS( v.x );
419 shift = ft_trig_prenorm( &v );
420 ft_trig_pseudo_polarize( &v );
422 v.x = ft_trig_downscale( v.x );
425 return ( v.x + ( 1 << ( shift - 1 ) ) ) >> shift;
427 return (FT_Fixed)( (FT_UInt32)v.x << -shift );
431 /* documentation is in fttrigon.h */
433 FT_EXPORT_DEF( void )
434 FT_Vector_Polarize( FT_Vector* vec,
444 if ( v.x == 0 && v.y == 0 )
447 shift = ft_trig_prenorm( &v );
448 ft_trig_pseudo_polarize( &v );
450 v.x = ft_trig_downscale( v.x );
452 *length = ( shift >= 0 ) ? ( v.x >> shift )
453 : (FT_Fixed)( (FT_UInt32)v.x << -shift );
458 /* documentation is in fttrigon.h */
460 FT_EXPORT_DEF( void )
461 FT_Vector_From_Polar( FT_Vector* vec,
468 FT_Vector_Rotate( vec, angle );
472 /* documentation is in fttrigon.h */
474 FT_EXPORT_DEF( FT_Angle )
475 FT_Angle_Diff( FT_Angle angle1,
478 FT_Angle delta = angle2 - angle1;
481 delta %= FT_ANGLE_2PI;
483 delta += FT_ANGLE_2PI;
485 if ( delta > FT_ANGLE_PI )
486 delta -= FT_ANGLE_2PI;