1 /***************************************************************************/
5 /* FreeType trigonometric functions (body). */
7 /* Copyright 2001, 2002, 2003, 2004, 2005 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 /***************************************************************************/
20 #include FT_INTERNAL_OBJECTS_H
21 #include FT_TRIGONOMETRY_H
24 /* the following is 0.2715717684432231 * 2^30 */
25 #define FT_TRIG_COSCALE 0x11616E8EUL
27 /* this table was generated for FT_PI = 180L << 16, i.e. degrees */
28 #define FT_TRIG_MAX_ITERS 23
31 ft_trig_arctan_table
[24] =
33 4157273L, 2949120L, 1740967L, 919879L, 466945L, 234379L, 117304L,
34 58666L, 29335L, 14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
35 57L, 29L, 14L, 7L, 4L, 2L, 1L
38 /* the Cordic shrink factor, multiplied by 2^32 */
39 #define FT_TRIG_SCALE 1166391785UL /* 0x4585BA38UL */
42 #ifdef FT_CONFIG_HAS_INT64
44 /* multiply a given value by the CORDIC shrink factor */
46 ft_trig_downscale( FT_Fixed val
)
53 val
= ( val
>= 0 ) ? val
: -val
;
55 v
= ( val
* (FT_Int64
)FT_TRIG_SCALE
) + 0x100000000UL
;
56 val
= (FT_Fixed
)( v
>> 32 );
58 return ( s
>= 0 ) ? val
: -val
;
61 #else /* !FT_CONFIG_HAS_INT64 */
63 /* multiply a given value by the CORDIC shrink factor */
65 ft_trig_downscale( FT_Fixed val
)
68 FT_UInt32 v1
, v2
, k1
, k2
, hi
, lo1
, lo2
, lo3
;
72 val
= ( val
>= 0 ) ? val
: -val
;
74 v1
= (FT_UInt32
)val
>> 16;
75 v2
= (FT_UInt32
)(val
& 0xFFFFL
);
77 k1
= (FT_UInt32
)FT_TRIG_SCALE
>> 16; /* constant */
78 k2
= (FT_UInt32
)(FT_TRIG_SCALE
& 0xFFFFL
); /* constant */
81 lo1
= k1
* v2
+ k2
* v1
; /* can't overflow */
83 lo2
= ( k2
* v2
) >> 16;
84 lo3
= ( lo1
>= lo2
) ? lo1
: lo2
;
89 hi
+= (FT_UInt32
)0x10000UL
;
93 return ( s
>= 0 ) ? val
: -val
;
96 #endif /* !FT_CONFIG_HAS_INT64 */
100 ft_trig_prenorm( FT_Vector
* vec
)
109 z
= ( ( x
>= 0 ) ? x
: - x
) | ( (y
>= 0) ? y
: -y
);
113 /* determine msb bit index in `shift' */
114 if ( z
>= ( 1L << 16 ) )
119 if ( z
>= ( 1L << 8 ) )
124 if ( z
>= ( 1L << 4 ) )
129 if ( z
>= ( 1L << 2 ) )
134 if ( z
>= ( 1L << 1 ) )
156 if ( z
< ( 1L << 27 ) )
162 } while ( z
< ( 1L << 27 ) );
166 else if ( z
> ( 1L << 28 ) )
172 } while ( z
> ( 1L << 28 ) );
186 ft_trig_pseudo_rotate( FT_Vector
* vec
,
190 FT_Fixed x
, y
, xtemp
;
191 const FT_Fixed
*arctanptr
;
197 /* Get angle between -90 and 90 degrees */
198 while ( theta
<= -FT_ANGLE_PI2
)
202 theta
+= FT_ANGLE_PI
;
205 while ( theta
> FT_ANGLE_PI2
)
209 theta
-= FT_ANGLE_PI
;
212 /* Initial pseudorotation, with left shift */
213 arctanptr
= ft_trig_arctan_table
;
217 xtemp
= x
+ ( y
<< 1 );
220 theta
+= *arctanptr
++;
224 xtemp
= x
- ( y
<< 1 );
227 theta
-= *arctanptr
++;
230 /* Subsequent pseudorotations, with right shifts */
236 xtemp
= x
+ ( y
>> i
);
239 theta
+= *arctanptr
++;
243 xtemp
= x
- ( y
>> i
);
246 theta
-= *arctanptr
++;
248 } while ( ++i
< FT_TRIG_MAX_ITERS
);
256 ft_trig_pseudo_polarize( FT_Vector
* vec
)
261 const FT_Fixed
*arctanptr
;
267 /* Get the vector into the right half plane */
273 theta
= 2 * FT_ANGLE_PI2
;
279 arctanptr
= ft_trig_arctan_table
;
283 /* Rotate positive */
287 theta
-= *arctanptr
++; /* Subtract angle */
291 /* Rotate negative */
295 theta
+= *arctanptr
++; /* Add angle */
303 /* Rotate positive */
307 theta
-= *arctanptr
++;
311 /* Rotate negative */
315 theta
+= *arctanptr
++;
317 } while ( ++i
< FT_TRIG_MAX_ITERS
);
321 theta
= FT_PAD_ROUND( theta
, 32 );
323 theta
= -FT_PAD_ROUND( -theta
, 32 );
330 /* documentation is in fttrigon.h */
332 FT_EXPORT_DEF( FT_Fixed
)
333 FT_Cos( FT_Angle angle
)
338 v
.x
= FT_TRIG_COSCALE
>> 2;
340 ft_trig_pseudo_rotate( &v
, angle
);
342 return v
.x
/ ( 1 << 12 );
346 /* documentation is in fttrigon.h */
348 FT_EXPORT_DEF( FT_Fixed
)
349 FT_Sin( FT_Angle angle
)
351 return FT_Cos( FT_ANGLE_PI2
- angle
);
355 /* documentation is in fttrigon.h */
357 FT_EXPORT_DEF( FT_Fixed
)
358 FT_Tan( FT_Angle angle
)
363 v
.x
= FT_TRIG_COSCALE
>> 2;
365 ft_trig_pseudo_rotate( &v
, angle
);
367 return FT_DivFix( v
.y
, v
.x
);
371 /* documentation is in fttrigon.h */
373 FT_EXPORT_DEF( FT_Angle
)
374 FT_Atan2( FT_Fixed dx
,
380 if ( dx
== 0 && dy
== 0 )
385 ft_trig_prenorm( &v
);
386 ft_trig_pseudo_polarize( &v
);
392 /* documentation is in fttrigon.h */
394 FT_EXPORT_DEF( void )
395 FT_Vector_Unit( FT_Vector
* vec
,
398 vec
->x
= FT_TRIG_COSCALE
>> 2;
400 ft_trig_pseudo_rotate( vec
, angle
);
406 /* these macros return 0 for positive numbers,
407 and -1 for negative ones */
408 #define FT_SIGN_LONG( x ) ( (x) >> ( FT_SIZEOF_LONG * 8 - 1 ) )
409 #define FT_SIGN_INT( x ) ( (x) >> ( FT_SIZEOF_INT * 8 - 1 ) )
410 #define FT_SIGN_INT32( x ) ( (x) >> 31 )
411 #define FT_SIGN_INT16( x ) ( (x) >> 15 )
414 /* documentation is in fttrigon.h */
416 FT_EXPORT_DEF( void )
417 FT_Vector_Rotate( FT_Vector
* vec
,
427 if ( angle
&& ( v
.x
!= 0 || v
.y
!= 0 ) )
429 shift
= ft_trig_prenorm( &v
);
430 ft_trig_pseudo_rotate( &v
, angle
);
431 v
.x
= ft_trig_downscale( v
.x
);
432 v
.y
= ft_trig_downscale( v
.y
);
436 FT_Int32 half
= (FT_Int32
)1L << ( shift
- 1 );
439 vec
->x
= ( v
.x
+ half
+ FT_SIGN_LONG( v
.x
) ) >> shift
;
440 vec
->y
= ( v
.y
+ half
+ FT_SIGN_LONG( v
.y
) ) >> shift
;
445 vec
->x
= v
.x
<< shift
;
446 vec
->y
= v
.y
<< shift
;
452 /* documentation is in fttrigon.h */
454 FT_EXPORT_DEF( FT_Fixed
)
455 FT_Vector_Length( FT_Vector
* vec
)
463 /* handle trivial cases */
466 return ( v
.y
>= 0 ) ? v
.y
: -v
.y
;
470 return ( v
.x
>= 0 ) ? v
.x
: -v
.x
;
474 shift
= ft_trig_prenorm( &v
);
475 ft_trig_pseudo_polarize( &v
);
477 v
.x
= ft_trig_downscale( v
.x
);
480 return ( v
.x
+ ( 1 << ( shift
- 1 ) ) ) >> shift
;
482 return v
.x
<< -shift
;
486 /* documentation is in fttrigon.h */
488 FT_EXPORT_DEF( void )
489 FT_Vector_Polarize( FT_Vector
* vec
,
499 if ( v
.x
== 0 && v
.y
== 0 )
502 shift
= ft_trig_prenorm( &v
);
503 ft_trig_pseudo_polarize( &v
);
505 v
.x
= ft_trig_downscale( v
.x
);
507 *length
= ( shift
>= 0 ) ? ( v
.x
>> shift
) : ( v
.x
<< -shift
);
512 /* documentation is in fttrigon.h */
514 FT_EXPORT_DEF( void )
515 FT_Vector_From_Polar( FT_Vector
* vec
,
522 FT_Vector_Rotate( vec
, angle
);
526 /* documentation is in fttrigon.h */
528 FT_EXPORT_DEF( FT_Angle
)
529 FT_Angle_Diff( FT_Angle angle1
,
532 FT_Angle delta
= angle2
- angle1
;
535 delta
%= FT_ANGLE_2PI
;
537 delta
+= FT_ANGLE_2PI
;
539 if ( delta
> FT_ANGLE_PI
)
540 delta
-= FT_ANGLE_2PI
;