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 |
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 |
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 ) \ |