src/java.desktop/share/native/libfreetype/include/freetype/internal/ftcalc.h
changeset 54876 da3834261f0c
parent 49234 3375a8039fde
equal deleted inserted replaced
54875:bcfedddcf4ce 54876:da3834261f0c
     1 /***************************************************************************/
     1 /****************************************************************************
     2 /*                                                                         */
     2  *
     3 /*  ftcalc.h                                                               */
     3  * ftcalc.h
     4 /*                                                                         */
     4  *
     5 /*    Arithmetic computations (specification).                             */
     5  *   Arithmetic computations (specification).
     6 /*                                                                         */
     6  *
     7 /*  Copyright 1996-2018 by                                                 */
     7  * Copyright (C) 1996-2019 by
     8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
     8  * David Turner, Robert Wilhelm, and Werner Lemberg.
     9 /*                                                                         */
     9  *
    10 /*  This file is part of the FreeType project, and may only be used,       */
    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      */
    11  * modified, and distributed under the terms of the FreeType project
    12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
    12  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
    13 /*  this file you indicate that you have read the license and              */
    13  * this file you indicate that you have read the license and
    14 /*  understand and accept it fully.                                        */
    14  * understand and accept it fully.
    15 /*                                                                         */
    15  *
    16 /***************************************************************************/
    16  */
    17 
    17 
    18 
    18 
    19 #ifndef FTCALC_H_
    19 #ifndef FTCALC_H_
    20 #define FTCALC_H_
    20 #define FTCALC_H_
    21 
    21 
    25 
    25 
    26 
    26 
    27 FT_BEGIN_HEADER
    27 FT_BEGIN_HEADER
    28 
    28 
    29 
    29 
    30   /*************************************************************************/
    30   /**************************************************************************
    31   /*                                                                       */
    31    *
    32   /* FT_MulDiv() and FT_MulFix() are declared in freetype.h.               */
    32    * FT_MulDiv() and FT_MulFix() are declared in freetype.h.
    33   /*                                                                       */
    33    *
    34   /*************************************************************************/
    34    */
    35 
    35 
    36 #ifndef  FT_CONFIG_OPTION_NO_ASSEMBLER
    36 #ifndef  FT_CONFIG_OPTION_NO_ASSEMBLER
    37   /* Provide assembler fragments for performance-critical functions. */
    37   /* Provide assembler fragments for performance-critical functions. */
    38   /* These must be defined `static __inline__' with GCC.             */
    38   /* These must be defined `static __inline__' with GCC.             */
    39 
    39 
   244 #define FT_MulFix( a, b )  FT_MULFIX_ASSEMBLER( (FT_Int32)(a), (FT_Int32)(b) )
   244 #define FT_MulFix( a, b )  FT_MULFIX_ASSEMBLER( (FT_Int32)(a), (FT_Int32)(b) )
   245 #endif
   245 #endif
   246 #endif
   246 #endif
   247 
   247 
   248 
   248 
   249   /*************************************************************************/
   249   /**************************************************************************
   250   /*                                                                       */
   250    *
   251   /* <Function>                                                            */
   251    * @function:
   252   /*    FT_MulDiv_No_Round                                                 */
   252    *   FT_MulDiv_No_Round
   253   /*                                                                       */
   253    *
   254   /* <Description>                                                         */
   254    * @description:
   255   /*    A very simple function used to perform the computation `(a*b)/c'   */
   255    *   A very simple function used to perform the computation '(a*b)/c'
   256   /*    (without rounding) with maximum accuracy (it uses a 64-bit         */
   256    *   (without rounding) with maximum accuracy (it uses a 64-bit
   257   /*    intermediate integer whenever necessary).                          */
   257    *   intermediate integer whenever necessary).
   258   /*                                                                       */
   258    *
   259   /*    This function isn't necessarily as fast as some processor specific */
   259    *   This function isn't necessarily as fast as some processor-specific
   260   /*    operations, but is at least completely portable.                   */
   260    *   operations, but is at least completely portable.
   261   /*                                                                       */
   261    *
   262   /* <Input>                                                               */
   262    * @input:
   263   /*    a :: The first multiplier.                                         */
   263    *   a ::
   264   /*    b :: The second multiplier.                                        */
   264    *     The first multiplier.
   265   /*    c :: The divisor.                                                  */
   265    *   b ::
   266   /*                                                                       */
   266    *     The second multiplier.
   267   /* <Return>                                                              */
   267    *   c ::
   268   /*    The result of `(a*b)/c'.  This function never traps when trying to */
   268    *     The divisor.
   269   /*    divide by zero; it simply returns `MaxInt' or `MinInt' depending   */
   269    *
   270   /*    on the signs of `a' and `b'.                                       */
   270    * @return:
   271   /*                                                                       */
   271    *   The result of '(a*b)/c'.  This function never traps when trying to
       
   272    *   divide by zero; it simply returns 'MaxInt' or 'MinInt' depending on
       
   273    *   the signs of 'a' and 'b'.
       
   274    */
   272   FT_BASE( FT_Long )
   275   FT_BASE( FT_Long )
   273   FT_MulDiv_No_Round( FT_Long  a,
   276   FT_MulDiv_No_Round( FT_Long  a,
   274                       FT_Long  b,
   277                       FT_Long  b,
   275                       FT_Long  c );
   278                       FT_Long  c );
   276 
   279 
   277 
   280 
   278   /*
   281   /*
   279    *  A variant of FT_Matrix_Multiply which scales its result afterwards.
   282    * A variant of FT_Matrix_Multiply which scales its result afterwards.  The
   280    *  The idea is that both `a' and `b' are scaled by factors of 10 so that
   283    * idea is that both `a' and `b' are scaled by factors of 10 so that the
   281    *  the values are as precise as possible to get a correct result during
   284    * values are as precise as possible to get a correct result during the
   282    *  the 64bit multiplication.  Let `sa' and `sb' be the scaling factors of
   285    * 64bit multiplication.  Let `sa' and `sb' be the scaling factors of `a'
   283    *  `a' and `b', respectively, then the scaling factor of the result is
   286    * and `b', respectively, then the scaling factor of the result is `sa*sb'.
   284    *  `sa*sb'.
       
   285    */
   287    */
   286   FT_BASE( void )
   288   FT_BASE( void )
   287   FT_Matrix_Multiply_Scaled( const FT_Matrix*  a,
   289   FT_Matrix_Multiply_Scaled( const FT_Matrix*  a,
   288                              FT_Matrix        *b,
   290                              FT_Matrix        *b,
   289                              FT_Long           scaling );
   291                              FT_Long           scaling );
   290 
   292 
   291 
   293 
   292   /*
   294   /*
   293    *  A variant of FT_Vector_Transform.  See comments for
   295    * Check a matrix.  If the transformation would lead to extreme shear or
   294    *  FT_Matrix_Multiply_Scaled.
   296    * extreme scaling, for example, return 0.  If everything is OK, return 1.
       
   297    *
       
   298    * Based on geometric considerations we use the following inequality to
       
   299    * identify a degenerate matrix.
       
   300    *
       
   301    *   50 * abs(xx*yy - xy*yx) < xx^2 + xy^2 + yx^2 + yy^2
       
   302    *
       
   303    * Value 50 is heuristic.
       
   304    */
       
   305   FT_BASE( FT_Bool )
       
   306   FT_Matrix_Check( const FT_Matrix*  matrix );
       
   307 
       
   308 
       
   309   /*
       
   310    * A variant of FT_Vector_Transform.  See comments for
       
   311    * FT_Matrix_Multiply_Scaled.
   295    */
   312    */
   296   FT_BASE( void )
   313   FT_BASE( void )
   297   FT_Vector_Transform_Scaled( FT_Vector*        vector,
   314   FT_Vector_Transform_Scaled( FT_Vector*        vector,
   298                               const FT_Matrix*  matrix,
   315                               const FT_Matrix*  matrix,
   299                               FT_Long           scaling );
   316                               FT_Long           scaling );
   300 
   317 
   301 
   318 
   302   /*
   319   /*
   303    *  This function normalizes a vector and returns its original length.
   320    * This function normalizes a vector and returns its original length.  The
   304    *  The normalized vector is a 16.16 fixed-point unit vector with length
   321    * normalized vector is a 16.16 fixed-point unit vector with length close
   305    *  close to 0x10000.  The accuracy of the returned length is limited to
   322    * to 0x10000.  The accuracy of the returned length is limited to 16 bits
   306    *  16 bits also.  The function utilizes quick inverse square root
   323    * also.  The function utilizes quick inverse square root approximation
   307    *  approximation without divisions and square roots relying on Newton's
   324    * without divisions and square roots relying on Newton's iterations
   308    *  iterations instead.
   325    * instead.
   309    */
   326    */
   310   FT_BASE( FT_UInt32 )
   327   FT_BASE( FT_UInt32 )
   311   FT_Vector_NormLen( FT_Vector*  vector );
   328   FT_Vector_NormLen( FT_Vector*  vector );
   312 
   329 
   313 
   330 
   314   /*
   331   /*
   315    *  Return -1, 0, or +1, depending on the orientation of a given corner.
   332    * Return -1, 0, or +1, depending on the orientation of a given corner.  We
   316    *  We use the Cartesian coordinate system, with positive vertical values
   333    * use the Cartesian coordinate system, with positive vertical values going
   317    *  going upwards.  The function returns +1 if the corner turns to the
   334    * upwards.  The function returns +1 if the corner turns to the left, -1 to
   318    *  left, -1 to the right, and 0 for undecidable cases.
   335    * the right, and 0 for undecidable cases.
   319    */
   336    */
   320   FT_BASE( FT_Int )
   337   FT_BASE( FT_Int )
   321   ft_corner_orientation( FT_Pos  in_x,
   338   ft_corner_orientation( FT_Pos  in_x,
   322                          FT_Pos  in_y,
   339                          FT_Pos  in_y,
   323                          FT_Pos  out_x,
   340                          FT_Pos  out_x,
   324                          FT_Pos  out_y );
   341                          FT_Pos  out_y );
   325 
   342 
   326 
   343 
   327   /*
   344   /*
   328    *  Return TRUE if a corner is flat or nearly flat.  This is equivalent to
   345    * Return TRUE if a corner is flat or nearly flat.  This is equivalent to
   329    *  saying that the corner point is close to its neighbors, or inside an
   346    * saying that the corner point is close to its neighbors, or inside an
   330    *  ellipse defined by the neighbor focal points to be more precise.
   347    * ellipse defined by the neighbor focal points to be more precise.
   331    */
   348    */
   332   FT_BASE( FT_Int )
   349   FT_BASE( FT_Int )
   333   ft_corner_is_flat( FT_Pos  in_x,
   350   ft_corner_is_flat( FT_Pos  in_x,
   334                      FT_Pos  in_y,
   351                      FT_Pos  in_y,
   335                      FT_Pos  out_x,
   352                      FT_Pos  out_x,
   336                      FT_Pos  out_y );
   353                      FT_Pos  out_y );
   337 
   354 
   338 
   355 
   339   /*
   356   /*
   340    *  Return the most significant bit index.
   357    * Return the most significant bit index.
   341    */
   358    */
   342 
   359 
   343 #ifndef  FT_CONFIG_OPTION_NO_ASSEMBLER
   360 #ifndef  FT_CONFIG_OPTION_NO_ASSEMBLER
       
   361 
   344 #if defined( __GNUC__ )                                          && \
   362 #if defined( __GNUC__ )                                          && \
   345     ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 4 ) )
   363     ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 4 ) )
   346 
   364 
   347 #if FT_SIZEOF_INT == 4
   365 #if FT_SIZEOF_INT == 4
   348 
   366 
   350 
   368 
   351 #elif FT_SIZEOF_LONG == 4
   369 #elif FT_SIZEOF_LONG == 4
   352 
   370 
   353 #define FT_MSB( x )  ( 31 - __builtin_clzl( x ) )
   371 #define FT_MSB( x )  ( 31 - __builtin_clzl( x ) )
   354 
   372 
   355 #endif
       
   356 
       
   357 #endif /* __GNUC__ */
   373 #endif /* __GNUC__ */
       
   374 
       
   375 
       
   376 #elif defined( _MSC_VER ) && ( _MSC_VER >= 1400 )
       
   377 
       
   378 #if FT_SIZEOF_INT == 4
       
   379 
       
   380 #include <intrin.h>
       
   381 
       
   382   static __inline FT_Int32
       
   383   FT_MSB_i386( FT_UInt32  x )
       
   384   {
       
   385     unsigned long  where;
       
   386 
       
   387 
       
   388     /* not available in older VC versions */
       
   389     _BitScanReverse( &where, x );
       
   390 
       
   391     return (FT_Int32)where;
       
   392   }
       
   393 
       
   394 #define FT_MSB( x )  ( FT_MSB_i386( x ) )
       
   395 
       
   396 #endif
       
   397 
       
   398 #endif /* _MSC_VER */
       
   399 
       
   400 
   358 #endif /* !FT_CONFIG_OPTION_NO_ASSEMBLER */
   401 #endif /* !FT_CONFIG_OPTION_NO_ASSEMBLER */
   359 
   402 
   360 #ifndef FT_MSB
   403 #ifndef FT_MSB
   361 
   404 
   362   FT_BASE( FT_Int )
   405   FT_BASE( FT_Int )
   364 
   407 
   365 #endif
   408 #endif
   366 
   409 
   367 
   410 
   368   /*
   411   /*
   369    *  Return sqrt(x*x+y*y), which is the same as `FT_Vector_Length' but uses
   412    * Return sqrt(x*x+y*y), which is the same as `FT_Vector_Length' but uses
   370    *  two fixed-point arguments instead.
   413    * two fixed-point arguments instead.
   371    */
   414    */
   372   FT_BASE( FT_Fixed )
   415   FT_BASE( FT_Fixed )
   373   FT_Hypot( FT_Fixed  x,
   416   FT_Hypot( FT_Fixed  x,
   374             FT_Fixed  y );
   417             FT_Fixed  y );
   375 
   418 
   376 
   419 
   377 #if 0
   420 #if 0
   378 
   421 
   379   /*************************************************************************/
   422   /**************************************************************************
   380   /*                                                                       */
   423    *
   381   /* <Function>                                                            */
   424    * @function:
   382   /*    FT_SqrtFixed                                                       */
   425    *   FT_SqrtFixed
   383   /*                                                                       */
   426    *
   384   /* <Description>                                                         */
   427    * @description:
   385   /*    Computes the square root of a 16.16 fixed-point value.             */
   428    *   Computes the square root of a 16.16 fixed-point value.
   386   /*                                                                       */
   429    *
   387   /* <Input>                                                               */
   430    * @input:
   388   /*    x :: The value to compute the root for.                            */
   431    *   x ::
   389   /*                                                                       */
   432    *     The value to compute the root for.
   390   /* <Return>                                                              */
   433    *
   391   /*    The result of `sqrt(x)'.                                           */
   434    * @return:
   392   /*                                                                       */
   435    *   The result of 'sqrt(x)'.
   393   /* <Note>                                                                */
   436    *
   394   /*    This function is not very fast.                                    */
   437    * @note:
   395   /*                                                                       */
   438    *   This function is not very fast.
       
   439    */
   396   FT_BASE( FT_Int32 )
   440   FT_BASE( FT_Int32 )
   397   FT_SqrtFixed( FT_Int32  x );
   441   FT_SqrtFixed( FT_Int32  x );
   398 
   442 
   399 #endif /* 0 */
   443 #endif /* 0 */
   400 
   444 
   407 
   451 
   408 #define ROUND_F26DOT6( x )     ( x >= 0 ? (    ( (x) + 32 ) & -64 )     \
   452 #define ROUND_F26DOT6( x )     ( x >= 0 ? (    ( (x) + 32 ) & -64 )     \
   409                                         : ( -( ( 32 - (x) ) & -64 ) ) )
   453                                         : ( -( ( 32 - (x) ) & -64 ) ) )
   410 
   454 
   411   /*
   455   /*
   412    *  The following macros have two purposes.
   456    * The following macros have two purposes.
   413    *
   457    *
   414    *  . Tag places where overflow is expected and harmless.
   458    * - Tag places where overflow is expected and harmless.
   415    *
   459    *
   416    *  . Avoid run-time sanitizer errors.
   460    * - Avoid run-time sanitizer errors.
   417    *
   461    *
   418    *  Use with care!
   462    * Use with care!
   419    */
   463    */
       
   464 #define ADD_INT( a, b )                           \
       
   465           (FT_Int)( (FT_UInt)(a) + (FT_UInt)(b) )
       
   466 #define SUB_INT( a, b )                           \
       
   467           (FT_Int)( (FT_UInt)(a) - (FT_UInt)(b) )
       
   468 #define MUL_INT( a, b )                           \
       
   469           (FT_Int)( (FT_UInt)(a) * (FT_UInt)(b) )
       
   470 #define NEG_INT( a )                              \
       
   471           (FT_Int)( (FT_UInt)0 - (FT_UInt)(a) )
       
   472 
   420 #define ADD_LONG( a, b )                             \
   473 #define ADD_LONG( a, b )                             \
   421           (FT_Long)( (FT_ULong)(a) + (FT_ULong)(b) )
   474           (FT_Long)( (FT_ULong)(a) + (FT_ULong)(b) )
   422 #define SUB_LONG( a, b )                             \
   475 #define SUB_LONG( a, b )                             \
   423           (FT_Long)( (FT_ULong)(a) - (FT_ULong)(b) )
   476           (FT_Long)( (FT_ULong)(a) - (FT_ULong)(b) )
   424 #define MUL_LONG( a, b )                             \
   477 #define MUL_LONG( a, b )                             \
   433 #define MUL_INT32( a, b )                               \
   486 #define MUL_INT32( a, b )                               \
   434           (FT_Int32)( (FT_UInt32)(a) * (FT_UInt32)(b) )
   487           (FT_Int32)( (FT_UInt32)(a) * (FT_UInt32)(b) )
   435 #define NEG_INT32( a )                                  \
   488 #define NEG_INT32( a )                                  \
   436           (FT_Int32)( (FT_UInt32)0 - (FT_UInt32)(a) )
   489           (FT_Int32)( (FT_UInt32)0 - (FT_UInt32)(a) )
   437 
   490 
       
   491 #ifdef FT_LONG64
       
   492 
       
   493 #define ADD_INT64( a, b )                               \
       
   494           (FT_Int64)( (FT_UInt64)(a) + (FT_UInt64)(b) )
       
   495 #define SUB_INT64( a, b )                               \
       
   496           (FT_Int64)( (FT_UInt64)(a) - (FT_UInt64)(b) )
       
   497 #define MUL_INT64( a, b )                               \
       
   498           (FT_Int64)( (FT_UInt64)(a) * (FT_UInt64)(b) )
       
   499 #define NEG_INT64( a )                                  \
       
   500           (FT_Int64)( (FT_UInt64)0 - (FT_UInt64)(a) )
       
   501 
       
   502 #endif /* FT_LONG64 */
       
   503 
   438 
   504 
   439 FT_END_HEADER
   505 FT_END_HEADER
   440 
   506 
   441 #endif /* FTCALC_H_ */
   507 #endif /* FTCALC_H_ */
   442 
   508