Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / freetype2 / base / fttrigon.c
blob155fc5ff26cf64ca5ea6cbe54479e169a4fb84f5
1 /***************************************************************************/
2 /* */
3 /* fttrigon.c */
4 /* */
5 /* FreeType trigonometric functions (body). */
6 /* */
7 /* Copyright 2001 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_TRIGONOMETRY_H
23 /* the following is 0.2715717684432231 * 2^30 */
24 #define FT_TRIG_COSCALE 0x11616E8EUL
26 /* this table was generated for FT_PI = 180L << 16, i.e. degrees */
27 #define FT_TRIG_MAX_ITERS 23
29 static const FT_Fixed
30 ft_trig_arctan_table[24] =
32 4157273L, 2949120L, 1740967L, 919879L, 466945L, 234379L, 117304L,
33 58666L, 29335L, 14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
34 57L, 29L, 14L, 7L, 4L, 2L, 1L
37 /* the Cordic shrink factor, multiplied by 2^32 */
38 #define FT_TRIG_SCALE 1166391785UL /* 0x4585BA38UL */
41 #ifdef FT_CONFIG_HAS_INT64
43 /* multiply a given value by the CORDIC shrink factor */
44 static FT_Fixed
45 ft_trig_downscale( FT_Fixed val )
47 FT_Fixed s;
48 FT_Int64 v;
51 s = val;
52 val = ( val >= 0 ) ? val : -val;
54 v = ( val * (FT_Int64)FT_TRIG_SCALE ) + 0x100000000UL;
55 val = (FT_Fixed)( v >> 32 );
57 return ( s >= 0 ) ? val : -val;
60 #else /* !FT_CONFIG_HAS_INT64 */
62 /* multiply a given value by the CORDIC shrink factor */
63 static FT_Fixed
64 ft_trig_downscale( FT_Fixed val )
66 FT_Fixed s;
67 FT_UInt32 v1, v2, k1, k2, hi, lo1, lo2, lo3;
70 s = val;
71 val = ( val >= 0 ) ? val : -val;
73 v1 = (FT_UInt32)val >> 16;
74 v2 = (FT_UInt32)val & 0xFFFF;
76 k1 = FT_TRIG_SCALE >> 16; /* constant */
77 k2 = FT_TRIG_SCALE & 0xFFFF; /* constant */
79 hi = k1 * v1;
80 lo1 = k1 * v2 + k2 * v1; /* can't overflow */
82 lo2 = ( k2 * v2 ) >> 16;
83 lo3 = ( lo1 >= lo2 ) ? lo1 : lo2;
84 lo1 += lo2;
86 hi += lo1 >> 16;
87 if ( lo1 < lo3 )
88 hi += 0x10000UL;
90 val = (FT_Fixed)hi;
92 return ( s >= 0 ) ? val : -val;
95 #endif /* !FT_CONFIG_HAS_INT64 */
98 static FT_Int
99 ft_trig_prenorm( FT_Vector* vec )
101 FT_Fixed x, y, z;
102 FT_Int shift;
105 x = vec->x;
106 y = vec->y;
108 z = ( ( x >= 0 ) ? x : - x ) | ( (y >= 0) ? y : -y );
109 shift = 0;
111 if ( z < ( 1L << 27 ) )
115 shift++;
116 z <<= 1;
117 } while ( z < ( 1L << 27 ) );
119 vec->x = x << shift;
120 vec->y = y << shift;
122 else if ( z > ( 1L << 28 ) )
126 shift++;
127 z >>= 1;
128 } while ( z > ( 1L << 28 ) );
130 vec->x = x >> shift;
131 vec->y = y >> shift;
132 shift = -shift;
134 return shift;
138 static void
139 ft_trig_pseudo_rotate( FT_Vector* vec,
140 FT_Angle theta )
142 FT_Int i;
143 FT_Fixed x, y, xtemp;
144 const FT_Fixed *arctanptr;
147 x = vec->x;
148 y = vec->y;
150 /* Get angle between -90 and 90 degrees */
151 while ( theta <= -FT_ANGLE_PI2 )
153 x = -x;
154 y = -y;
155 theta += FT_ANGLE_PI;
158 while ( theta > FT_ANGLE_PI2 )
160 x = -x;
161 y = -y;
162 theta -= FT_ANGLE_PI;
165 /* Initial pseudorotation, with left shift */
166 arctanptr = ft_trig_arctan_table;
168 if ( theta < 0 )
170 xtemp = x + ( y << 1 );
171 y = y - ( x << 1 );
172 x = xtemp;
173 theta += *arctanptr++;
175 else
177 xtemp = x - ( y << 1 );
178 y = y + ( x << 1 );
179 x = xtemp;
180 theta -= *arctanptr++;
183 /* Subsequent pseudorotations, with right shifts */
184 i = 0;
187 if ( theta < 0 )
189 xtemp = x + ( y >> i );
190 y = y - ( x >> i );
191 x = xtemp;
192 theta += *arctanptr++;
194 else
196 xtemp = x - ( y >> i );
197 y = y + ( x >> i );
198 x = xtemp;
199 theta -= *arctanptr++;
201 } while ( ++i < FT_TRIG_MAX_ITERS );
203 vec->x = x;
204 vec->y = y;
208 static void
209 ft_trig_pseudo_polarize( FT_Vector* vec )
211 FT_Fixed theta;
212 FT_Fixed yi, i;
213 FT_Fixed x, y;
214 const FT_Fixed *arctanptr;
217 x = vec->x;
218 y = vec->y;
220 /* Get the vector into the right half plane */
221 theta = 0;
222 if ( x < 0 )
224 x = -x;
225 y = -y;
226 theta = 2 * FT_ANGLE_PI2;
229 if ( y > 0 )
230 theta = - theta;
232 arctanptr = ft_trig_arctan_table;
234 if ( y < 0 )
236 /* Rotate positive */
237 yi = y + ( x << 1 );
238 x = x - ( y << 1 );
239 y = yi;
240 theta -= *arctanptr++; /* Subtract angle */
242 else
244 /* Rotate negative */
245 yi = y - ( x << 1 );
246 x = x + ( y << 1 );
247 y = yi;
248 theta += *arctanptr++; /* Add angle */
251 i = 0;
254 if ( y < 0 )
256 /* Rotate positive */
257 yi = y + ( x >> i );
258 x = x - ( y >> i );
259 y = yi;
260 theta -= *arctanptr++;
262 else
264 /* Rotate negative */
265 yi = y - ( x >> i );
266 x = x + ( y >> i );
267 y = yi;
268 theta += *arctanptr++;
270 } while ( ++i < FT_TRIG_MAX_ITERS );
272 /* round theta */
273 if ( theta >= 0 )
274 theta = ( theta + 16 ) & -32;
275 else
276 theta = - (( -theta + 16 ) & -32);
278 vec->x = x;
279 vec->y = theta;
283 /* documentation is in fttrigon.h */
285 FT_EXPORT_DEF( FT_Fixed )
286 FT_Cos( FT_Angle angle )
288 FT_Vector v;
291 v.x = FT_TRIG_COSCALE >> 2;
292 v.y = 0;
293 ft_trig_pseudo_rotate( &v, angle );
295 return v.x / ( 1 << 12 );
299 /* documentation is in fttrigon.h */
301 FT_EXPORT_DEF( FT_Fixed )
302 FT_Sin( FT_Angle angle )
304 return FT_Cos( FT_ANGLE_PI2 - angle );
308 /* documentation is in fttrigon.h */
310 FT_EXPORT_DEF( FT_Fixed )
311 FT_Tan( FT_Angle angle )
313 FT_Vector v;
316 v.x = FT_TRIG_COSCALE >> 2;
317 v.y = 0;
318 ft_trig_pseudo_rotate( &v, angle );
320 return FT_DivFix( v.y, v.x );
324 /* documentation is in fttrigon.h */
326 FT_EXPORT_DEF( FT_Angle )
327 FT_Atan2( FT_Fixed dx,
328 FT_Fixed dy )
330 FT_Vector v;
333 if ( dx == 0 && dy == 0 )
334 return 0;
336 v.x = dx;
337 v.y = dy;
338 ft_trig_prenorm( &v );
339 ft_trig_pseudo_polarize( &v );
341 return v.y;
345 /* documentation is in fttrigon.h */
347 FT_EXPORT_DEF( void )
348 FT_Vector_Unit( FT_Vector* vec,
349 FT_Angle angle )
351 vec->x = FT_TRIG_COSCALE >> 2;
352 vec->y = 0;
353 ft_trig_pseudo_rotate( vec, angle );
354 vec->x >>= 12;
355 vec->y >>= 12;
359 /* documentation is in fttrigon.h */
361 FT_EXPORT_DEF( void )
362 FT_Vector_Rotate( FT_Vector* vec,
363 FT_Angle angle )
365 FT_Int shift;
366 FT_Vector v;
369 v.x = vec->x;
370 v.y = vec->y;
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 );
379 if ( shift >= 0 )
381 vec->x = v.x >> shift;
382 vec->y = v.y >> shift;
384 else
386 shift = -shift;
387 vec->x = v.x << shift;
388 vec->y = v.y << shift;
394 /* documentation is in fttrigon.h */
396 FT_EXPORT_DEF( FT_Fixed )
397 FT_Vector_Length( FT_Vector* vec )
399 FT_Int shift;
400 FT_Vector v;
403 v = *vec;
405 /* handle trivial cases */
406 if ( v.x == 0 )
408 return ( v.y >= 0 ) ? v.y : -v.y;
410 else if ( v.y == 0 )
412 return ( v.x >= 0 ) ? v.x : -v.x;
415 /* general case */
416 shift = ft_trig_prenorm( &v );
417 ft_trig_pseudo_polarize( &v );
419 v.x = ft_trig_downscale( v.x );
421 if ( shift > 0 )
422 return ( v.x + ( 1 << ( shift - 1 ) ) ) >> shift;
424 return v.x << -shift;
428 /* documentation is in fttrigon.h */
430 FT_EXPORT_DEF( void )
431 FT_Vector_Polarize( FT_Vector* vec,
432 FT_Fixed *length,
433 FT_Angle *angle )
435 FT_Int shift;
436 FT_Vector v;
439 v = *vec;
441 if ( v.x == 0 && v.y == 0 )
442 return;
444 shift = ft_trig_prenorm( &v );
445 ft_trig_pseudo_polarize( &v );
447 v.x = ft_trig_downscale( v.x );
449 *length = ( shift >= 0 ) ? ( v.x >> shift ) : ( v.x << -shift );
450 *angle = v.y;
454 /* END */