1 /****************************************************************************
5 * Arithmetic computations (body).
7 * Copyright (C) 1996-2020 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.
18 /**************************************************************************
20 * Support for 1-complement arithmetic has been totally dropped in this
21 * release. You can still write your own code if you need it.
25 /**************************************************************************
27 * Implementing basic computation routines.
29 * FT_MulDiv(), FT_MulFix(), FT_DivFix(), FT_RoundFix(), FT_CeilFix(),
30 * and FT_FloorFix() are declared in freetype.h.
35 #include <freetype/ftglyph.h>
36 #include <freetype/fttrigon.h>
37 #include <freetype/internal/ftcalc.h>
38 #include <freetype/internal/ftdebug.h>
39 #include <freetype/internal/ftobjs.h>
42 #ifdef FT_MULFIX_ASSEMBLER
46 /* we need to emulate a 64-bit data type if a real one isn't available */
50 typedef struct FT_Int64_
57 #endif /* !FT_LONG64 */
60 /**************************************************************************
62 * The macro FT_COMPONENT is used in trace mode. It is an implicit
63 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
64 * messages during execution.
67 #define FT_COMPONENT calc
70 /* transfer sign, leaving a positive number; */
71 /* we need an unsigned value to safely negate INT_MIN (or LONG_MIN) */
72 #define FT_MOVE_SIGN( x, x_unsigned, s ) \
76 x_unsigned = 0U - (x_unsigned); \
81 /* The following three functions are available regardless of whether */
82 /* FT_LONG64 is defined. */
84 /* documentation is in freetype.h */
86 FT_EXPORT_DEF( FT_Fixed )
87 FT_RoundFix( FT_Fixed a )
89 return ( ADD_LONG( a, 0x8000L - ( a < 0 ) ) ) & ~0xFFFFL;
93 /* documentation is in freetype.h */
95 FT_EXPORT_DEF( FT_Fixed )
96 FT_CeilFix( FT_Fixed a )
98 return ( ADD_LONG( a, 0xFFFFL ) ) & ~0xFFFFL;
102 /* documentation is in freetype.h */
104 FT_EXPORT_DEF( FT_Fixed )
105 FT_FloorFix( FT_Fixed a )
112 FT_BASE_DEF ( FT_Int )
113 FT_MSB( FT_UInt32 z )
118 /* determine msb bit index in `shift' */
119 if ( z & 0xFFFF0000UL )
124 if ( z & 0x0000FF00UL )
129 if ( z & 0x000000F0UL )
134 if ( z & 0x0000000CUL )
139 if ( z & 0x00000002UL )
151 /* documentation is in ftcalc.h */
153 FT_BASE_DEF( FT_Fixed )
154 FT_Hypot( FT_Fixed x,
163 return FT_Vector_Length( &v );
170 /* documentation is in freetype.h */
172 FT_EXPORT_DEF( FT_Long )
173 FT_MulDiv( FT_Long a_,
178 FT_UInt64 a, b, c, d;
186 FT_MOVE_SIGN( a_, a, s );
187 FT_MOVE_SIGN( b_, b, s );
188 FT_MOVE_SIGN( c_, c, s );
190 d = c > 0 ? ( a * b + ( c >> 1 ) ) / c
195 return s < 0 ? NEG_LONG( d_ ) : d_;
199 /* documentation is in ftcalc.h */
201 FT_BASE_DEF( FT_Long )
202 FT_MulDiv_No_Round( FT_Long a_,
207 FT_UInt64 a, b, c, d;
215 FT_MOVE_SIGN( a_, a, s );
216 FT_MOVE_SIGN( b_, b, s );
217 FT_MOVE_SIGN( c_, c, s );
219 d = c > 0 ? a * b / c
224 return s < 0 ? NEG_LONG( d_ ) : d_;
228 /* documentation is in freetype.h */
230 FT_EXPORT_DEF( FT_Long )
231 FT_MulFix( FT_Long a_,
234 #ifdef FT_MULFIX_ASSEMBLER
236 return FT_MULFIX_ASSEMBLER( (FT_Int32)a_, (FT_Int32)b_ );
240 FT_Int64 ab = (FT_Int64)a_ * (FT_Int64)b_;
242 /* this requires arithmetic right shift of signed numbers */
243 return (FT_Long)( ( ab + 0x8000L - ( ab < 0 ) ) >> 16 );
245 #endif /* FT_MULFIX_ASSEMBLER */
249 /* documentation is in freetype.h */
251 FT_EXPORT_DEF( FT_Long )
252 FT_DivFix( FT_Long a_,
263 FT_MOVE_SIGN( a_, a, s );
264 FT_MOVE_SIGN( b_, b, s );
266 q = b > 0 ? ( ( a << 16 ) + ( b >> 1 ) ) / b
271 return s < 0 ? NEG_LONG( q_ ) : q_;
275 #else /* !FT_LONG64 */
279 ft_multo64( FT_UInt32 x,
283 FT_UInt32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
286 lo1 = x & 0x0000FFFFU; hi1 = x >> 16;
287 lo2 = y & 0x0000FFFFU; hi2 = y >> 16;
294 /* Check carry overflow of i1 + i2 */
296 hi += (FT_UInt32)( i1 < i2 ) << 16;
301 /* Check carry overflow of i1 + lo */
311 ft_div64by32( FT_UInt32 hi,
320 return (FT_UInt32)0x7FFFFFFFL;
322 /* We shift as many bits as we can into the high register, perform */
323 /* 32-bit division with modulo there, then work through the remaining */
324 /* bits with long division. This optimization is especially noticeable */
325 /* for smaller dividends that barely use the high register. */
327 i = 31 - FT_MSB( hi );
328 r = ( hi << i ) | ( lo >> ( 32 - i ) ); lo <<= i; /* left 64-bit shift */
330 r -= q * y; /* remainder */
332 i = 32 - i; /* bits remaining in low register */
336 r = ( r << 1 ) | ( lo >> 31 ); lo <<= 1;
350 FT_Add64( FT_Int64* x,
358 hi = x->hi + y->hi + ( lo < x->lo );
365 /* The FT_MulDiv function has been optimized thanks to ideas from */
366 /* Graham Asher and Alexei Podtelezhnikov. The trick is to optimize */
367 /* a rather common case when everything fits within 32-bits. */
369 /* We compute 'a*b+c/2', then divide it by 'c' (all positive values). */
371 /* The product of two positive numbers never exceeds the square of */
372 /* its mean values. Therefore, we always avoid the overflow by */
375 /* (a + b) / 2 <= sqrt(X - c/2) , */
377 /* where X = 2^32 - 1, the maximum unsigned 32-bit value, and using */
378 /* unsigned arithmetic. Now we replace `sqrt' with a linear function */
379 /* that is smaller or equal for all values of c in the interval */
380 /* [0;X/2]; it should be equal to sqrt(X) and sqrt(3X/4) at the */
381 /* endpoints. Substituting the linear solution and explicit numbers */
384 /* a + b <= 131071.99 - c / 122291.84 . */
386 /* In practice, we should use a faster and even stronger inequality */
388 /* a + b <= 131071 - (c >> 16) */
390 /* or, alternatively, */
392 /* a + b <= 129894 - (c >> 17) . */
394 /* FT_MulFix, on the other hand, is optimized for a small value of */
395 /* the first argument, when the second argument can be much larger. */
396 /* This can be achieved by scaling the second argument and the limit */
397 /* in the above inequalities. For example, */
399 /* a + (b >> 8) <= (131071 >> 4) */
401 /* covers the practical range of use. The actual test below is a bit */
402 /* tighter to avoid the border case overflows. */
404 /* In the case of FT_DivFix, the exact overflow check */
406 /* a << 16 <= X - c/2 */
408 /* is scaled down by 2^16 and we use */
410 /* a <= 65535 - (c >> 17) . */
412 /* documentation is in freetype.h */
414 FT_EXPORT_DEF( FT_Long )
415 FT_MulDiv( FT_Long a_,
423 /* XXX: this function does not allow 64-bit arguments */
429 FT_MOVE_SIGN( a_, a, s );
430 FT_MOVE_SIGN( b_, b, s );
431 FT_MOVE_SIGN( c_, c, s );
436 else if ( a + b <= 129894UL - ( c >> 17 ) )
437 a = ( a * b + ( c >> 1 ) ) / c;
441 FT_Int64 temp, temp2;
444 ft_multo64( a, b, &temp );
449 FT_Add64( &temp, &temp2, &temp );
451 /* last attempt to ditch long division */
452 a = ( temp.hi == 0 ) ? temp.lo / c
453 : ft_div64by32( temp.hi, temp.lo, c );
458 return s < 0 ? NEG_LONG( a_ ) : a_;
462 FT_BASE_DEF( FT_Long )
463 FT_MulDiv_No_Round( FT_Long a_,
471 /* XXX: this function does not allow 64-bit arguments */
477 FT_MOVE_SIGN( a_, a, s );
478 FT_MOVE_SIGN( b_, b, s );
479 FT_MOVE_SIGN( c_, c, s );
484 else if ( a + b <= 131071UL )
492 ft_multo64( a, b, &temp );
494 /* last attempt to ditch long division */
495 a = ( temp.hi == 0 ) ? temp.lo / c
496 : ft_div64by32( temp.hi, temp.lo, c );
501 return s < 0 ? NEG_LONG( a_ ) : a_;
505 /* documentation is in freetype.h */
507 FT_EXPORT_DEF( FT_Long )
508 FT_MulFix( FT_Long a_,
511 #ifdef FT_MULFIX_ASSEMBLER
513 return FT_MULFIX_ASSEMBLER( a_, b_ );
518 * This code is nonportable. See comment below.
520 * However, on a platform where right-shift of a signed quantity fills
521 * the leftmost bits by copying the sign bit, it might be faster.
529 * This is a clever way of converting a signed number `a' into its
530 * absolute value (stored back into `a') and its sign. The sign is
531 * stored in `sa'; 0 means `a' was positive or zero, and -1 means `a'
532 * was negative. (Similarly for `b' and `sb').
534 * Unfortunately, it doesn't work (at least not portably).
536 * It makes the assumption that right-shift on a negative signed value
537 * fills the leftmost bits by copying the sign bit. This is wrong.
538 * According to K&R 2nd ed, section `A7.8 Shift Operators' on page 206,
539 * the result of right-shift of a negative signed value is
540 * implementation-defined. At least one implementation fills the
541 * leftmost bits with 0s (i.e., it is exactly the same as an unsigned
542 * right shift). This means that when `a' is negative, `sa' ends up
543 * with the value 1 rather than -1. After that, everything else goes
546 sa = ( a_ >> ( sizeof ( a_ ) * 8 - 1 ) );
547 a = ( a_ ^ sa ) - sa;
548 sb = ( b_ >> ( sizeof ( b_ ) * 8 - 1 ) );
549 b = ( b_ ^ sb ) - sb;
554 if ( a + ( b >> 8 ) <= 8190UL )
555 a = ( a * b + 0x8000U ) >> 16;
558 FT_UInt32 al = a & 0xFFFFUL;
561 a = ( a >> 16 ) * b + al * ( b >> 16 ) +
562 ( ( al * ( b & 0xFFFFUL ) + 0x8000UL ) >> 16 );
576 /* XXX: this function does not allow 64-bit arguments */
581 FT_MOVE_SIGN( a_, a, s );
582 FT_MOVE_SIGN( b_, b, s );
584 if ( a + ( b >> 8 ) <= 8190UL )
585 a = ( a * b + 0x8000UL ) >> 16;
588 FT_UInt32 al = a & 0xFFFFUL;
591 a = ( a >> 16 ) * b + al * ( b >> 16 ) +
592 ( ( al * ( b & 0xFFFFUL ) + 0x8000UL ) >> 16 );
597 return s < 0 ? NEG_LONG( a_ ) : a_;
604 /* documentation is in freetype.h */
606 FT_EXPORT_DEF( FT_Long )
607 FT_DivFix( FT_Long a_,
615 /* XXX: this function does not allow 64-bit arguments */
620 FT_MOVE_SIGN( a_, a, s );
621 FT_MOVE_SIGN( b_, b, s );
625 /* check for division by 0 */
628 else if ( a <= 65535UL - ( b >> 17 ) )
630 /* compute result directly */
631 q = ( ( a << 16 ) + ( b >> 1 ) ) / b;
635 /* we need more bits; we have to do it by hand */
636 FT_Int64 temp, temp2;
644 FT_Add64( &temp, &temp2, &temp );
645 q = ft_div64by32( temp.hi, temp.lo, b );
650 return s < 0 ? NEG_LONG( q_ ) : q_;
654 #endif /* !FT_LONG64 */
657 /* documentation is in ftglyph.h */
659 FT_EXPORT_DEF( void )
660 FT_Matrix_Multiply( const FT_Matrix* a,
663 FT_Fixed xx, xy, yx, yy;
669 xx = ADD_LONG( FT_MulFix( a->xx, b->xx ),
670 FT_MulFix( a->xy, b->yx ) );
671 xy = ADD_LONG( FT_MulFix( a->xx, b->xy ),
672 FT_MulFix( a->xy, b->yy ) );
673 yx = ADD_LONG( FT_MulFix( a->yx, b->xx ),
674 FT_MulFix( a->yy, b->yx ) );
675 yy = ADD_LONG( FT_MulFix( a->yx, b->xy ),
676 FT_MulFix( a->yy, b->yy ) );
685 /* documentation is in ftglyph.h */
687 FT_EXPORT_DEF( FT_Error )
688 FT_Matrix_Invert( FT_Matrix* matrix )
690 FT_Pos delta, xx, yy;
694 return FT_THROW( Invalid_Argument );
696 /* compute discriminant */
697 delta = FT_MulFix( matrix->xx, matrix->yy ) -
698 FT_MulFix( matrix->xy, matrix->yx );
701 return FT_THROW( Invalid_Argument ); /* matrix can't be inverted */
703 matrix->xy = -FT_DivFix( matrix->xy, delta );
704 matrix->yx = -FT_DivFix( matrix->yx, delta );
709 matrix->xx = FT_DivFix( yy, delta );
710 matrix->yy = FT_DivFix( xx, delta );
716 /* documentation is in ftcalc.h */
719 FT_Matrix_Multiply_Scaled( const FT_Matrix* a,
723 FT_Fixed xx, xy, yx, yy;
725 FT_Long val = 0x10000L * scaling;
731 xx = ADD_LONG( FT_MulDiv( a->xx, b->xx, val ),
732 FT_MulDiv( a->xy, b->yx, val ) );
733 xy = ADD_LONG( FT_MulDiv( a->xx, b->xy, val ),
734 FT_MulDiv( a->xy, b->yy, val ) );
735 yx = ADD_LONG( FT_MulDiv( a->yx, b->xx, val ),
736 FT_MulDiv( a->yy, b->yx, val ) );
737 yy = ADD_LONG( FT_MulDiv( a->yx, b->xy, val ),
738 FT_MulDiv( a->yy, b->yy, val ) );
747 /* documentation is in ftcalc.h */
749 FT_BASE_DEF( FT_Bool )
750 FT_Matrix_Check( const FT_Matrix* matrix )
754 FT_Fixed nonzero_minval, maxval;
755 FT_Fixed temp1, temp2;
762 val[0] = FT_ABS( matrix->xx );
763 val[1] = FT_ABS( matrix->xy );
764 val[2] = FT_ABS( matrix->yx );
765 val[3] = FT_ABS( matrix->yy );
768 * To avoid overflow, we ensure that each value is not larger than
770 * int(sqrt(2^31 / 4)) = 23170 ;
772 * we also check that no value becomes zero if we have to scale.
776 nonzero_minval = FT_LONG_MAX;
778 for ( i = 0; i < 4; i++ )
780 if ( val[i] > maxval )
782 if ( val[i] && val[i] < nonzero_minval )
783 nonzero_minval = val[i];
786 /* we only handle 32bit values */
787 if ( maxval > 0x7FFFFFFFL )
790 if ( maxval > 23170 )
792 FT_Fixed scale = FT_DivFix( maxval, 23170 );
795 if ( !FT_DivFix( nonzero_minval, scale ) )
796 return 0; /* value range too large */
798 m.xx = FT_DivFix( matrix->xx, scale );
799 m.xy = FT_DivFix( matrix->xy, scale );
800 m.yx = FT_DivFix( matrix->yx, scale );
801 m.yy = FT_DivFix( matrix->yy, scale );
806 temp1 = FT_ABS( m.xx * m.yy - m.xy * m.yx );
807 temp2 = m.xx * m.xx + m.xy * m.xy + m.yx * m.yx + m.yy * m.yy;
817 /* documentation is in ftcalc.h */
820 FT_Vector_Transform_Scaled( FT_Vector* vector,
821 const FT_Matrix* matrix,
826 FT_Long val = 0x10000L * scaling;
829 if ( !vector || !matrix )
832 xz = ADD_LONG( FT_MulDiv( vector->x, matrix->xx, val ),
833 FT_MulDiv( vector->y, matrix->xy, val ) );
834 yz = ADD_LONG( FT_MulDiv( vector->x, matrix->yx, val ),
835 FT_MulDiv( vector->y, matrix->yy, val ) );
842 /* documentation is in ftcalc.h */
844 FT_BASE_DEF( FT_UInt32 )
845 FT_Vector_NormLen( FT_Vector* vector )
847 FT_Int32 x_ = vector->x;
848 FT_Int32 y_ = vector->y;
850 FT_UInt32 x, y, u, v, l;
851 FT_Int sx = 1, sy = 1, shift;
857 FT_MOVE_SIGN( x_, x, sx );
858 FT_MOVE_SIGN( y_, y, sy );
864 vector->y = sy * 0x10000;
870 vector->x = sx * 0x10000;
874 /* Estimate length and prenormalize by shifting so that */
875 /* the new approximate length is between 2/3 and 4/3. */
876 /* The magic constant 0xAAAAAAAAUL (2/3 of 2^32) helps */
877 /* achieve this in 16.16 fixed-point representation. */
878 l = x > y ? x + ( y >> 1 )
881 shift = 31 - FT_MSB( l );
882 shift -= 15 + ( l >= ( 0xAAAAAAAAUL >> shift ) );
889 /* re-estimate length for tiny vectors */
890 l = x > y ? x + ( y >> 1 )
900 /* lower linear approximation for reciprocal length minus one */
901 b = 0x10000 - (FT_Int32)l;
906 /* Newton's iterations */
909 u = (FT_UInt32)( x_ + ( x_ * b >> 16 ) );
910 v = (FT_UInt32)( y_ + ( y_ * b >> 16 ) );
912 /* Normalized squared length in the parentheses approaches 2^32. */
913 /* On two's complement systems, converting to signed gives the */
914 /* difference with 2^32 even if the expression wraps around. */
915 z = -(FT_Int32)( u * u + v * v ) / 0x200;
916 z = z * ( ( 0x10000 + b ) >> 8 ) / 0x10000;
922 vector->x = sx < 0 ? -(FT_Pos)u : (FT_Pos)u;
923 vector->y = sy < 0 ? -(FT_Pos)v : (FT_Pos)v;
925 /* Conversion to signed helps to recover from likely wrap around */
926 /* in calculating the prenormalized length, because it gives the */
927 /* correct difference with 2^32 on two's complement systems. */
928 l = (FT_UInt32)( 0x10000 + (FT_Int32)( u * x + v * y ) / 0x10000 );
930 l = ( l + ( 1 << ( shift - 1 ) ) ) >> shift;
940 /* documentation is in ftcalc.h */
942 FT_BASE_DEF( FT_Int32 )
943 FT_SqrtFixed( FT_Int32 x )
945 FT_UInt32 root, rem_hi, rem_lo, test_div;
954 rem_lo = (FT_UInt32)x;
958 rem_hi = ( rem_hi << 2 ) | ( rem_lo >> 30 );
961 test_div = ( root << 1 ) + 1;
963 if ( rem_hi >= test_div )
971 return (FT_Int32)root;
977 /* documentation is in ftcalc.h */
979 FT_BASE_DEF( FT_Int )
980 ft_corner_orientation( FT_Pos in_x,
985 /* we silently ignore overflow errors since such large values */
986 /* lead to even more (harmless) rendering errors later on */
990 FT_Int64 delta = SUB_INT64( MUL_INT64( in_x, out_y ),
991 MUL_INT64( in_y, out_x ) );
994 return ( delta > 0 ) - ( delta < 0 );
1001 if ( ADD_LONG( FT_ABS( in_x ), FT_ABS( out_y ) ) <= 131071L &&
1002 ADD_LONG( FT_ABS( in_y ), FT_ABS( out_x ) ) <= 131071L )
1004 FT_Long z1 = MUL_LONG( in_x, out_y );
1005 FT_Long z2 = MUL_LONG( in_y, out_x );
1015 else /* products might overflow 32 bits */
1020 /* XXX: this function does not allow 64-bit arguments */
1021 ft_multo64( (FT_UInt32)in_x, (FT_UInt32)out_y, &z1 );
1022 ft_multo64( (FT_UInt32)in_y, (FT_UInt32)out_x, &z2 );
1024 if ( z1.hi > z2.hi )
1026 else if ( z1.hi < z2.hi )
1028 else if ( z1.lo > z2.lo )
1030 else if ( z1.lo < z2.lo )
1036 /* XXX: only the sign of return value, +1/0/-1 must be used */
1043 /* documentation is in ftcalc.h */
1045 FT_BASE_DEF( FT_Int )
1046 ft_corner_is_flat( FT_Pos in_x,
1051 FT_Pos ax = in_x + out_x;
1052 FT_Pos ay = in_y + out_y;
1054 FT_Pos d_in, d_out, d_hypot;
1057 /* The idea of this function is to compare the length of the */
1058 /* hypotenuse with the `in' and `out' length. The `corner' */
1059 /* represented by `in' and `out' is flat if the hypotenuse's */
1060 /* length isn't too large. */
1062 /* This approach has the advantage that the angle between */
1063 /* `in' and `out' is not checked. In case one of the two */
1064 /* vectors is `dominant', this is, much larger than the */
1065 /* other vector, we thus always have a flat corner. */
1068 /* x---------------------------x */
1076 d_in = FT_HYPOT( in_x, in_y );
1077 d_out = FT_HYPOT( out_x, out_y );
1078 d_hypot = FT_HYPOT( ax, ay );
1080 /* now do a simple length comparison: */
1082 /* d_in + d_out < 17/16 d_hypot */
1084 return ( d_in + d_out - d_hypot ) < ( d_hypot >> 4 );