summaryrefslogtreecommitdiff
path: root/src/static_libs/freetype/sw_ft_math.c
blob: 268c875c2824029e722cf52f9eb0974c52bd2ee1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/***************************************************************************/
/*                                                                         */
/*  fttrigon.c                                                             */
/*                                                                         */
/*    FreeType trigonometric functions (body).                             */
/*                                                                         */
/*  Copyright 2001-2005, 2012-2013 by                                      */
/*  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      */
/*  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.                                        */
/*                                                                         */
/***************************************************************************/

#include <math.h>
#include "sw_ft_math.h"


#define SW_FT_MSB( x )  ( 31 - __builtin_clz( x ) )

#define SW_FT_PAD_FLOOR( x, n )  ( (x) & ~((n)-1) )
#define SW_FT_PAD_ROUND( x, n )  SW_FT_PAD_FLOOR( (x) + ((n)/2), n )
#define SW_FT_PAD_CEIL( x, n )   SW_FT_PAD_FLOOR( (x) + ((n)-1), n )


#define SW_FT_BEGIN_STMNT  do {
#define SW_FT_END_STMNT    } while ( 0 )
/* transfer sign leaving a positive number */
#define SW_FT_MOVE_SIGN( x, s ) \
SW_FT_BEGIN_STMNT             \
  if ( x < 0 )             \
  {                        \
    x = -x;                \
    s = -s;                \
  }                        \
SW_FT_END_STMNT




SW_FT_Long
SW_FT_MulFix( SW_FT_Long  a,
           SW_FT_Long  b )
{
  SW_FT_Int   s = 1;
  SW_FT_Long  c;


  SW_FT_MOVE_SIGN( a, s );
  SW_FT_MOVE_SIGN( b, s );

  c = (SW_FT_Long)( ( (SW_FT_Int64)a * b + 0x8000L ) >> 16 );

  return ( s > 0 ) ? c : -c;
}

SW_FT_Long
SW_FT_MulDiv( SW_FT_Long  a,
           SW_FT_Long  b,
           SW_FT_Long  c )
{
  SW_FT_Int   s = 1;
  SW_FT_Long  d;


  SW_FT_MOVE_SIGN( a, s );
  SW_FT_MOVE_SIGN( b, s );
  SW_FT_MOVE_SIGN( c, s );

  d = (SW_FT_Long)( c > 0 ? ( (SW_FT_Int64)a * b + ( c >> 1 ) ) / c
                       : 0x7FFFFFFFL );

  return ( s > 0 ) ? d : -d;
}

SW_FT_Long
SW_FT_DivFix( SW_FT_Long  a,
           SW_FT_Long  b )
{
  SW_FT_Int   s = 1;
  SW_FT_Long  q;


  SW_FT_MOVE_SIGN( a, s );
  SW_FT_MOVE_SIGN( b, s );

  q = (SW_FT_Long)( b > 0 ? ( ( (SW_FT_UInt64)a << 16 ) + ( b >> 1 ) ) / b
                       : 0x7FFFFFFFL );

  return ( s < 0 ? -q : q );
}


/*************************************************************************/
/*                                                                       */
/* This is a fixed-point CORDIC implementation of trigonometric          */
/* functions as well as transformations between Cartesian and polar      */
/* coordinates.  The angles are represented as 16.16 fixed-point values  */
/* in degrees, i.e., the angular resolution is 2^-16 degrees.  Note that */
/* only vectors longer than 2^16*180/pi (or at least 22 bits) on a       */
/* discrete Cartesian grid can have the same or better angular           */
/* resolution.  Therefore, to maintain this precision, some functions    */
/* require an interim upscaling of the vectors, whereas others operate   */
/* with 24-bit long vectors directly.                                    */
/*                                                                       */
/*************************************************************************/

  /* the Cordic shrink factor 0.858785336480436 * 2^32 */
#define SW_FT_TRIG_SCALE      0xDBD95B16UL

  /* the highest bit in overflow-safe vector components, */
  /* MSB of 0.858785336480436 * sqrt(0.5) * 2^30         */
#define SW_FT_TRIG_SAFE_MSB   29

  /* this table was generated for SW_FT_PI = 180L << 16, i.e. degrees */
#define SW_FT_TRIG_MAX_ITERS  23

  static const SW_FT_Fixed
  ft_trig_arctan_table[] =
  {
    1740967L, 919879L, 466945L, 234379L, 117304L, 58666L, 29335L,
    14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
    57L, 29L, 14L, 7L, 4L, 2L, 1L
  };

  /* multiply a given value by the CORDIC shrink factor */
  static SW_FT_Fixed
  ft_trig_downscale( SW_FT_Fixed  val )
  {
    SW_FT_Fixed  s;
    SW_FT_Int64  v;


    s   = val;
    val = SW_FT_ABS( val );

    v   = ( val * (SW_FT_Int64)SW_FT_TRIG_SCALE ) + 0x100000000UL;
    val = (SW_FT_Fixed)( v >> 32 );

    return ( s >= 0 ) ? val : -val;
  }



  /* undefined and never called for zero vector */
  static SW_FT_Int
  ft_trig_prenorm( SW_FT_Vector*  vec )
  {
    SW_FT_Pos  x, y;
    SW_FT_Int  shift;


    x = vec->x;
    y = vec->y;

    shift = SW_FT_MSB( SW_FT_ABS( x ) | SW_FT_ABS( y ) );

    if ( shift <= SW_FT_TRIG_SAFE_MSB )
    {
      shift  = SW_FT_TRIG_SAFE_MSB - shift;
      vec->x = (SW_FT_Pos)( (SW_FT_ULong)x << shift );
      vec->y = (SW_FT_Pos)( (SW_FT_ULong)y << shift );
    }
    else
    {
      shift -= SW_FT_TRIG_SAFE_MSB;
      vec->x = x >> shift;
      vec->y = y >> shift;
      shift  = -shift;
    }

    return shift;
  }


  static void
  ft_trig_pseudo_rotate( SW_FT_Vector*  vec,
                         SW_FT_Angle    theta )
  {
    SW_FT_Int           i;
    SW_FT_Fixed         x, y, xtemp, b;
    const SW_FT_Fixed  *arctanptr;


    x = vec->x;
    y = vec->y;

    /* Rotate inside [-PI/4,PI/4] sector */
    while ( theta < -SW_FT_ANGLE_PI4 )
    {
      xtemp  =  y;
      y      = -x;
      x      =  xtemp;
      theta +=  SW_FT_ANGLE_PI2;
    }

    while ( theta > SW_FT_ANGLE_PI4 )
    {
      xtemp  = -y;
      y      =  x;
      x      =  xtemp;
      theta -=  SW_FT_ANGLE_PI2;
    }

    arctanptr = ft_trig_arctan_table;

    /* Pseudorotations, with right shifts */
    for ( i = 1, b = 1; i < SW_FT_TRIG_MAX_ITERS; b <<= 1, i++ )
    {
      if ( theta < 0 )
      {
        xtemp  = x + ( ( y + b ) >> i );
        y      = y - ( ( x + b ) >> i );
        x      = xtemp;
        theta += *arctanptr++;
      }
      else
      {
        xtemp  = x - ( ( y + b ) >> i );
        y      = y + ( ( x + b ) >> i );
        x      = xtemp;
        theta -= *arctanptr++;
      }
    }

    vec->x = x;
    vec->y = y;
  }


  static void
  ft_trig_pseudo_polarize( SW_FT_Vector*  vec )
  {
    SW_FT_Angle         theta;
    SW_FT_Int           i;
    SW_FT_Fixed         x, y, xtemp, b;
    const SW_FT_Fixed  *arctanptr;


    x = vec->x;
    y = vec->y;

    /* Get the vector into [-PI/4,PI/4] sector */
    if ( y > x )
    {
      if ( y > -x )
      {
        theta =  SW_FT_ANGLE_PI2;
        xtemp =  y;
        y     = -x;
        x     =  xtemp;
      }
      else
      {
        theta =  y > 0 ? SW_FT_ANGLE_PI : -SW_FT_ANGLE_PI;
        x     = -x;
        y     = -y;
      }
    }
    else
    {
      if ( y < -x )
      {
        theta = -SW_FT_ANGLE_PI2;
        xtemp = -y;
        y     =  x;
        x     =  xtemp;
      }
      else
      {
        theta = 0;
      }
    }

    arctanptr = ft_trig_arctan_table;

    /* Pseudorotations, with right shifts */
    for ( i = 1, b = 1; i < SW_FT_TRIG_MAX_ITERS; b <<= 1, i++ )
    {
      if ( y > 0 )
      {
        xtemp  = x + ( ( y + b ) >> i );
        y      = y - ( ( x + b ) >> i );
        x      = xtemp;
        theta += *arctanptr++;
      }
      else
      {
        xtemp  = x - ( ( y + b ) >> i );
        y      = y + ( ( x + b ) >> i );
        x      = xtemp;
        theta -= *arctanptr++;
      }
    }

    /* round theta */
    if ( theta >= 0 )
      theta = SW_FT_PAD_ROUND( theta, 32 );
    else
      theta = -SW_FT_PAD_ROUND( -theta, 32 );

    vec->x = x;
    vec->y = theta;
  }


  /* documentation is in fttrigon.h */

  SW_FT_Fixed
  SW_FT_Cos( SW_FT_Angle  angle )
  {
    SW_FT_Vector  v;


    v.x = SW_FT_TRIG_SCALE >> 8;
    v.y = 0;
    ft_trig_pseudo_rotate( &v, angle );

    return ( v.x + 0x80L ) >> 8;
  }


  /* documentation is in fttrigon.h */

  SW_FT_Fixed
  SW_FT_Sin( SW_FT_Angle  angle )
  {
    return SW_FT_Cos( SW_FT_ANGLE_PI2 - angle );
  }


  /* documentation is in fttrigon.h */

  SW_FT_Fixed
  SW_FT_Tan( SW_FT_Angle  angle )
  {
    SW_FT_Vector  v;


    v.x = SW_FT_TRIG_SCALE >> 8;
    v.y = 0;
    ft_trig_pseudo_rotate( &v, angle );

    return SW_FT_DivFix( v.y, v.x );
  }


  /* documentation is in fttrigon.h */

  SW_FT_Angle
  SW_FT_Atan2( SW_FT_Fixed  dx,
            SW_FT_Fixed  dy )
  {
    SW_FT_Vector  v;


    if ( dx == 0 && dy == 0 )
      return 0;

    v.x = dx;
    v.y = dy;
    ft_trig_prenorm( &v );
    ft_trig_pseudo_polarize( &v );

    return v.y;
  }


  /* documentation is in fttrigon.h */

  void
  SW_FT_Vector_Unit( SW_FT_Vector*  vec,
                  SW_FT_Angle    angle )
  {
    vec->x = SW_FT_TRIG_SCALE >> 8;
    vec->y = 0;
    ft_trig_pseudo_rotate( vec, angle );
    vec->x = ( vec->x + 0x80L ) >> 8;
    vec->y = ( vec->y + 0x80L ) >> 8;
  }


  /* these macros return 0 for positive numbers,
     and -1 for negative ones */
#define SW_FT_SIGN_LONG( x )   ( (x) >> ( SW_FT_SIZEOF_LONG * 8 - 1 ) )
#define SW_FT_SIGN_INT( x )    ( (x) >> ( SW_FT_SIZEOF_INT * 8 - 1 ) )
#define SW_FT_SIGN_INT32( x )  ( (x) >> 31 )
#define SW_FT_SIGN_INT16( x )  ( (x) >> 15 )


  /* documentation is in fttrigon.h */

  void
  SW_FT_Vector_Rotate( SW_FT_Vector*  vec,
                    SW_FT_Angle    angle )
  {
    SW_FT_Int     shift;
    SW_FT_Vector  v;


    v.x   = vec->x;
    v.y   = vec->y;

    if ( angle && ( v.x != 0 || v.y != 0 ) )
    {
      shift = ft_trig_prenorm( &v );
      ft_trig_pseudo_rotate( &v, angle );
      v.x = ft_trig_downscale( v.x );
      v.y = ft_trig_downscale( v.y );

      if ( shift > 0 )
      {
        SW_FT_Int32  half = (SW_FT_Int32)1L << ( shift - 1 );


        vec->x = ( v.x + half + SW_FT_SIGN_LONG( v.x ) ) >> shift;
        vec->y = ( v.y + half + SW_FT_SIGN_LONG( v.y ) ) >> shift;
      }
      else
      {
        shift  = -shift;
        vec->x = (SW_FT_Pos)( (SW_FT_ULong)v.x << shift );
        vec->y = (SW_FT_Pos)( (SW_FT_ULong)v.y << shift );
      }
    }
  }


  /* documentation is in fttrigon.h */

  SW_FT_Fixed
  SW_FT_Vector_Length( SW_FT_Vector*  vec )
  {
    SW_FT_Int     shift;
    SW_FT_Vector  v;


    v = *vec;

    /* handle trivial cases */
    if ( v.x == 0 )
    {
      return SW_FT_ABS( v.y );
    }
    else if ( v.y == 0 )
    {
      return SW_FT_ABS( v.x );
    }

    /* general case */
    shift = ft_trig_prenorm( &v );
    ft_trig_pseudo_polarize( &v );

    v.x = ft_trig_downscale( v.x );

    if ( shift > 0 )
      return ( v.x + ( 1 << ( shift - 1 ) ) ) >> shift;

    return (SW_FT_Fixed)( (SW_FT_UInt32)v.x << -shift );
  }


  /* documentation is in fttrigon.h */

  void
  SW_FT_Vector_Polarize( SW_FT_Vector*  vec,
                      SW_FT_Fixed   *length,
                      SW_FT_Angle   *angle )
  {
    SW_FT_Int     shift;
    SW_FT_Vector  v;


    v = *vec;

    if ( v.x == 0 && v.y == 0 )
      return;

    shift = ft_trig_prenorm( &v );
    ft_trig_pseudo_polarize( &v );

    v.x = ft_trig_downscale( v.x );

    *length = ( shift >= 0 ) ?                      ( v.x >>  shift )
                             : (SW_FT_Fixed)( (SW_FT_UInt32)v.x << -shift );
    *angle  = v.y;
  }


  /* documentation is in fttrigon.h */

  void
  SW_FT_Vector_From_Polar( SW_FT_Vector*  vec,
                        SW_FT_Fixed    length,
                        SW_FT_Angle    angle )
  {
    vec->x = length;
    vec->y = 0;

    SW_FT_Vector_Rotate( vec, angle );
  }


  /* documentation is in fttrigon.h */

  SW_FT_Angle
  SW_FT_Angle_Diff( SW_FT_Angle  angle1,
                 SW_FT_Angle  angle2 )
  {
    SW_FT_Angle  delta = angle2 - angle1;


    delta %= SW_FT_ANGLE_2PI;
    if ( delta < 0 )
      delta += SW_FT_ANGLE_2PI;

    if ( delta > SW_FT_ANGLE_PI )
      delta -= SW_FT_ANGLE_2PI;

    return delta;
  }


/* END */