/src/compiler/android-ndk/jni/freetype/src/base/ftcalc.c

http://ftk.googlecode.com/ · C · 957 lines · 582 code · 257 blank · 118 comment · 108 complexity · ea8c76f0cdee11b67a8dbf45a01695bd MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftcalc.c */
  4. /* */
  5. /* Arithmetic computations (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2008 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. /***************************************************************************/
  17. /*************************************************************************/
  18. /* */
  19. /* Support for 1-complement arithmetic has been totally dropped in this */
  20. /* release. You can still write your own code if you need it. */
  21. /* */
  22. /*************************************************************************/
  23. /*************************************************************************/
  24. /* */
  25. /* Implementing basic computation routines. */
  26. /* */
  27. /* FT_MulDiv(), FT_MulFix(), FT_DivFix(), FT_RoundFix(), FT_CeilFix(), */
  28. /* and FT_FloorFix() are declared in freetype.h. */
  29. /* */
  30. /*************************************************************************/
  31. #include <ft2build.h>
  32. #include FT_GLYPH_H
  33. #include FT_INTERNAL_CALC_H
  34. #include FT_INTERNAL_DEBUG_H
  35. #include FT_INTERNAL_OBJECTS_H
  36. #ifdef FT_MULFIX_INLINED
  37. #undef FT_MulFix
  38. #endif
  39. /* we need to define a 64-bits data type here */
  40. #ifdef FT_LONG64
  41. typedef FT_INT64 FT_Int64;
  42. #else
  43. typedef struct FT_Int64_
  44. {
  45. FT_UInt32 lo;
  46. FT_UInt32 hi;
  47. } FT_Int64;
  48. #endif /* FT_LONG64 */
  49. /*************************************************************************/
  50. /* */
  51. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  52. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  53. /* messages during execution. */
  54. /* */
  55. #undef FT_COMPONENT
  56. #define FT_COMPONENT trace_calc
  57. /* The following three functions are available regardless of whether */
  58. /* FT_LONG64 is defined. */
  59. /* documentation is in freetype.h */
  60. FT_EXPORT_DEF( FT_Fixed )
  61. FT_RoundFix( FT_Fixed a )
  62. {
  63. return ( a >= 0 ) ? ( a + 0x8000L ) & ~0xFFFFL
  64. : -((-a + 0x8000L ) & ~0xFFFFL );
  65. }
  66. /* documentation is in freetype.h */
  67. FT_EXPORT_DEF( FT_Fixed )
  68. FT_CeilFix( FT_Fixed a )
  69. {
  70. return ( a >= 0 ) ? ( a + 0xFFFFL ) & ~0xFFFFL
  71. : -((-a + 0xFFFFL ) & ~0xFFFFL );
  72. }
  73. /* documentation is in freetype.h */
  74. FT_EXPORT_DEF( FT_Fixed )
  75. FT_FloorFix( FT_Fixed a )
  76. {
  77. return ( a >= 0 ) ? a & ~0xFFFFL
  78. : -((-a) & ~0xFFFFL );
  79. }
  80. #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
  81. /* documentation is in ftcalc.h */
  82. FT_EXPORT_DEF( FT_Int32 )
  83. FT_Sqrt32( FT_Int32 x )
  84. {
  85. FT_UInt32 val, root, newroot, mask;
  86. root = 0;
  87. mask = (FT_UInt32)0x40000000UL;
  88. val = (FT_UInt32)x;
  89. do
  90. {
  91. newroot = root + mask;
  92. if ( newroot <= val )
  93. {
  94. val -= newroot;
  95. root = newroot + mask;
  96. }
  97. root >>= 1;
  98. mask >>= 2;
  99. } while ( mask != 0 );
  100. return root;
  101. }
  102. #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
  103. #ifdef FT_LONG64
  104. /* documentation is in freetype.h */
  105. FT_EXPORT_DEF( FT_Long )
  106. FT_MulDiv( FT_Long a,
  107. FT_Long b,
  108. FT_Long c )
  109. {
  110. FT_Int s;
  111. FT_Long d;
  112. s = 1;
  113. if ( a < 0 ) { a = -a; s = -1; }
  114. if ( b < 0 ) { b = -b; s = -s; }
  115. if ( c < 0 ) { c = -c; s = -s; }
  116. d = (FT_Long)( c > 0 ? ( (FT_Int64)a * b + ( c >> 1 ) ) / c
  117. : 0x7FFFFFFFL );
  118. return ( s > 0 ) ? d : -d;
  119. }
  120. #ifdef TT_USE_BYTECODE_INTERPRETER
  121. /* documentation is in ftcalc.h */
  122. FT_BASE_DEF( FT_Long )
  123. FT_MulDiv_No_Round( FT_Long a,
  124. FT_Long b,
  125. FT_Long c )
  126. {
  127. FT_Int s;
  128. FT_Long d;
  129. s = 1;
  130. if ( a < 0 ) { a = -a; s = -1; }
  131. if ( b < 0 ) { b = -b; s = -s; }
  132. if ( c < 0 ) { c = -c; s = -s; }
  133. d = (FT_Long)( c > 0 ? (FT_Int64)a * b / c
  134. : 0x7FFFFFFFL );
  135. return ( s > 0 ) ? d : -d;
  136. }
  137. #endif /* TT_USE_BYTECODE_INTERPRETER */
  138. /* documentation is in freetype.h */
  139. FT_EXPORT_DEF( FT_Long )
  140. FT_MulFix( FT_Long a,
  141. FT_Long b )
  142. {
  143. #ifdef FT_MULFIX_ASSEMBLER
  144. return FT_MULFIX_ASSEMBLER( a, b );
  145. #else
  146. FT_Int s = 1;
  147. FT_Long c;
  148. if ( a < 0 )
  149. {
  150. a = -a;
  151. s = -1;
  152. }
  153. if ( b < 0 )
  154. {
  155. b = -b;
  156. s = -s;
  157. }
  158. c = (FT_Long)( ( (FT_Int64)a * b + 0x8000L ) >> 16 );
  159. return ( s > 0 ) ? c : -c;
  160. #endif /* FT_MULFIX_ASSEMBLER */
  161. }
  162. /* documentation is in freetype.h */
  163. FT_EXPORT_DEF( FT_Long )
  164. FT_DivFix( FT_Long a,
  165. FT_Long b )
  166. {
  167. FT_Int32 s;
  168. FT_UInt32 q;
  169. s = 1;
  170. if ( a < 0 ) { a = -a; s = -1; }
  171. if ( b < 0 ) { b = -b; s = -s; }
  172. if ( b == 0 )
  173. /* check for division by 0 */
  174. q = 0x7FFFFFFFL;
  175. else
  176. /* compute result directly */
  177. q = (FT_UInt32)( ( ( (FT_Int64)a << 16 ) + ( b >> 1 ) ) / b );
  178. return ( s < 0 ? -(FT_Long)q : (FT_Long)q );
  179. }
  180. #else /* !FT_LONG64 */
  181. static void
  182. ft_multo64( FT_UInt32 x,
  183. FT_UInt32 y,
  184. FT_Int64 *z )
  185. {
  186. FT_UInt32 lo1, hi1, lo2, hi2, lo, hi, i1, i2;
  187. lo1 = x & 0x0000FFFFU; hi1 = x >> 16;
  188. lo2 = y & 0x0000FFFFU; hi2 = y >> 16;
  189. lo = lo1 * lo2;
  190. i1 = lo1 * hi2;
  191. i2 = lo2 * hi1;
  192. hi = hi1 * hi2;
  193. /* Check carry overflow of i1 + i2 */
  194. i1 += i2;
  195. hi += (FT_UInt32)( i1 < i2 ) << 16;
  196. hi += i1 >> 16;
  197. i1 = i1 << 16;
  198. /* Check carry overflow of i1 + lo */
  199. lo += i1;
  200. hi += ( lo < i1 );
  201. z->lo = lo;
  202. z->hi = hi;
  203. }
  204. static FT_UInt32
  205. ft_div64by32( FT_UInt32 hi,
  206. FT_UInt32 lo,
  207. FT_UInt32 y )
  208. {
  209. FT_UInt32 r, q;
  210. FT_Int i;
  211. q = 0;
  212. r = hi;
  213. if ( r >= y )
  214. return (FT_UInt32)0x7FFFFFFFL;
  215. i = 32;
  216. do
  217. {
  218. r <<= 1;
  219. q <<= 1;
  220. r |= lo >> 31;
  221. if ( r >= (FT_UInt32)y )
  222. {
  223. r -= y;
  224. q |= 1;
  225. }
  226. lo <<= 1;
  227. } while ( --i );
  228. return q;
  229. }
  230. static void
  231. FT_Add64( FT_Int64* x,
  232. FT_Int64* y,
  233. FT_Int64 *z )
  234. {
  235. register FT_UInt32 lo, hi;
  236. lo = x->lo + y->lo;
  237. hi = x->hi + y->hi + ( lo < x->lo );
  238. z->lo = lo;
  239. z->hi = hi;
  240. }
  241. /* documentation is in freetype.h */
  242. /* The FT_MulDiv function has been optimized thanks to ideas from */
  243. /* Graham Asher. The trick is to optimize computation when everything */
  244. /* fits within 32-bits (a rather common case). */
  245. /* */
  246. /* we compute 'a*b+c/2', then divide it by 'c'. (positive values) */
  247. /* */
  248. /* 46340 is FLOOR(SQRT(2^31-1)). */
  249. /* */
  250. /* if ( a <= 46340 && b <= 46340 ) then ( a*b <= 0x7FFEA810 ) */
  251. /* */
  252. /* 0x7FFFFFFF - 0x7FFEA810 = 0x157F0 */
  253. /* */
  254. /* if ( c < 0x157F0*2 ) then ( a*b+c/2 <= 0x7FFFFFFF ) */
  255. /* */
  256. /* and 2*0x157F0 = 176096 */
  257. /* */
  258. FT_EXPORT_DEF( FT_Long )
  259. FT_MulDiv( FT_Long a,
  260. FT_Long b,
  261. FT_Long c )
  262. {
  263. long s;
  264. /* XXX: this function does not allow 64-bit arguments */
  265. if ( a == 0 || b == c )
  266. return a;
  267. s = a; a = FT_ABS( a );
  268. s ^= b; b = FT_ABS( b );
  269. s ^= c; c = FT_ABS( c );
  270. if ( a <= 46340L && b <= 46340L && c <= 176095L && c > 0 )
  271. a = ( a * b + ( c >> 1 ) ) / c;
  272. else if ( c > 0 )
  273. {
  274. FT_Int64 temp, temp2;
  275. ft_multo64( (FT_Int32)a, (FT_Int32)b, &temp );
  276. temp2.hi = 0;
  277. temp2.lo = (FT_UInt32)(c >> 1);
  278. FT_Add64( &temp, &temp2, &temp );
  279. a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
  280. }
  281. else
  282. a = 0x7FFFFFFFL;
  283. return ( s < 0 ? -a : a );
  284. }
  285. #ifdef TT_USE_BYTECODE_INTERPRETER
  286. FT_BASE_DEF( FT_Long )
  287. FT_MulDiv_No_Round( FT_Long a,
  288. FT_Long b,
  289. FT_Long c )
  290. {
  291. long s;
  292. if ( a == 0 || b == c )
  293. return a;
  294. s = a; a = FT_ABS( a );
  295. s ^= b; b = FT_ABS( b );
  296. s ^= c; c = FT_ABS( c );
  297. if ( a <= 46340L && b <= 46340L && c > 0 )
  298. a = a * b / c;
  299. else if ( c > 0 )
  300. {
  301. FT_Int64 temp;
  302. ft_multo64( (FT_Int32)a, (FT_Int32)b, &temp );
  303. a = ft_div64by32( temp.hi, temp.lo, (FT_Int32)c );
  304. }
  305. else
  306. a = 0x7FFFFFFFL;
  307. return ( s < 0 ? -a : a );
  308. }
  309. #endif /* TT_USE_BYTECODE_INTERPRETER */
  310. /* documentation is in freetype.h */
  311. FT_EXPORT_DEF( FT_Long )
  312. FT_MulFix( FT_Long a,
  313. FT_Long b )
  314. {
  315. #ifdef FT_MULFIX_ASSEMBLER
  316. return FT_MULFIX_ASSEMBLER( a, b );
  317. #elif 0
  318. /*
  319. * This code is nonportable. See comment below.
  320. *
  321. * However, on a platform where right-shift of a signed quantity fills
  322. * the leftmost bits by copying the sign bit, it might be faster.
  323. */
  324. FT_Long sa, sb;
  325. FT_ULong ua, ub;
  326. if ( a == 0 || b == 0x10000L )
  327. return a;
  328. /*
  329. * This is a clever way of converting a signed number `a' into its
  330. * absolute value (stored back into `a') and its sign. The sign is
  331. * stored in `sa'; 0 means `a' was positive or zero, and -1 means `a'
  332. * was negative. (Similarly for `b' and `sb').
  333. *
  334. * Unfortunately, it doesn't work (at least not portably).
  335. *
  336. * It makes the assumption that right-shift on a negative signed value
  337. * fills the leftmost bits by copying the sign bit. This is wrong.
  338. * According to K&R 2nd ed, section `A7.8 Shift Operators' on page 206,
  339. * the result of right-shift of a negative signed value is
  340. * implementation-defined. At least one implementation fills the
  341. * leftmost bits with 0s (i.e., it is exactly the same as an unsigned
  342. * right shift). This means that when `a' is negative, `sa' ends up
  343. * with the value 1 rather than -1. After that, everything else goes
  344. * wrong.
  345. */
  346. sa = ( a >> ( sizeof ( a ) * 8 - 1 ) );
  347. a = ( a ^ sa ) - sa;
  348. sb = ( b >> ( sizeof ( b ) * 8 - 1 ) );
  349. b = ( b ^ sb ) - sb;
  350. ua = (FT_ULong)a;
  351. ub = (FT_ULong)b;
  352. if ( ua <= 2048 && ub <= 1048576L )
  353. ua = ( ua * ub + 0x8000U ) >> 16;
  354. else
  355. {
  356. FT_ULong al = ua & 0xFFFFU;
  357. ua = ( ua >> 16 ) * ub + al * ( ub >> 16 ) +
  358. ( ( al * ( ub & 0xFFFFU ) + 0x8000U ) >> 16 );
  359. }
  360. sa ^= sb,
  361. ua = (FT_ULong)(( ua ^ sa ) - sa);
  362. return (FT_Long)ua;
  363. #else /* 0 */
  364. FT_Long s;
  365. FT_ULong ua, ub;
  366. if ( a == 0 || b == 0x10000L )
  367. return a;
  368. s = a; a = FT_ABS( a );
  369. s ^= b; b = FT_ABS( b );
  370. ua = (FT_ULong)a;
  371. ub = (FT_ULong)b;
  372. if ( ua <= 2048 && ub <= 1048576L )
  373. ua = ( ua * ub + 0x8000UL ) >> 16;
  374. else
  375. {
  376. FT_ULong al = ua & 0xFFFFUL;
  377. ua = ( ua >> 16 ) * ub + al * ( ub >> 16 ) +
  378. ( ( al * ( ub & 0xFFFFUL ) + 0x8000UL ) >> 16 );
  379. }
  380. return ( s < 0 ? -(FT_Long)ua : (FT_Long)ua );
  381. #endif /* 0 */
  382. }
  383. /* documentation is in freetype.h */
  384. FT_EXPORT_DEF( FT_Long )
  385. FT_DivFix( FT_Long a,
  386. FT_Long b )
  387. {
  388. FT_Int32 s;
  389. FT_UInt32 q;
  390. /* XXX: this function does not allow 64-bit arguments */
  391. s = (FT_Int32)a; a = FT_ABS( a );
  392. s ^= (FT_Int32)b; b = FT_ABS( b );
  393. if ( b == 0 )
  394. {
  395. /* check for division by 0 */
  396. q = (FT_UInt32)0x7FFFFFFFL;
  397. }
  398. else if ( ( a >> 16 ) == 0 )
  399. {
  400. /* compute result directly */
  401. q = (FT_UInt32)( (a << 16) + (b >> 1) ) / (FT_UInt32)b;
  402. }
  403. else
  404. {
  405. /* we need more bits; we have to do it by hand */
  406. FT_Int64 temp, temp2;
  407. temp.hi = (FT_Int32) (a >> 16);
  408. temp.lo = (FT_UInt32)(a << 16);
  409. temp2.hi = 0;
  410. temp2.lo = (FT_UInt32)( b >> 1 );
  411. FT_Add64( &temp, &temp2, &temp );
  412. q = ft_div64by32( temp.hi, temp.lo, (FT_Int32)b );
  413. }
  414. return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
  415. }
  416. #if 0
  417. /* documentation is in ftcalc.h */
  418. FT_EXPORT_DEF( void )
  419. FT_MulTo64( FT_Int32 x,
  420. FT_Int32 y,
  421. FT_Int64 *z )
  422. {
  423. FT_Int32 s;
  424. s = x; x = FT_ABS( x );
  425. s ^= y; y = FT_ABS( y );
  426. ft_multo64( x, y, z );
  427. if ( s < 0 )
  428. {
  429. z->lo = (FT_UInt32)-(FT_Int32)z->lo;
  430. z->hi = ~z->hi + !( z->lo );
  431. }
  432. }
  433. /* apparently, the second version of this code is not compiled correctly */
  434. /* on Mac machines with the MPW C compiler.. tsk, tsk, tsk... */
  435. #if 1
  436. FT_EXPORT_DEF( FT_Int32 )
  437. FT_Div64by32( FT_Int64* x,
  438. FT_Int32 y )
  439. {
  440. FT_Int32 s;
  441. FT_UInt32 q, r, i, lo;
  442. s = x->hi;
  443. if ( s < 0 )
  444. {
  445. x->lo = (FT_UInt32)-(FT_Int32)x->lo;
  446. x->hi = ~x->hi + !x->lo;
  447. }
  448. s ^= y; y = FT_ABS( y );
  449. /* Shortcut */
  450. if ( x->hi == 0 )
  451. {
  452. if ( y > 0 )
  453. q = x->lo / y;
  454. else
  455. q = 0x7FFFFFFFL;
  456. return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
  457. }
  458. r = x->hi;
  459. lo = x->lo;
  460. if ( r >= (FT_UInt32)y ) /* we know y is to be treated as unsigned here */
  461. return ( s < 0 ? 0x80000001UL : 0x7FFFFFFFUL );
  462. /* Return Max/Min Int32 if division overflow. */
  463. /* This includes division by zero! */
  464. q = 0;
  465. for ( i = 0; i < 32; i++ )
  466. {
  467. r <<= 1;
  468. q <<= 1;
  469. r |= lo >> 31;
  470. if ( r >= (FT_UInt32)y )
  471. {
  472. r -= y;
  473. q |= 1;
  474. }
  475. lo <<= 1;
  476. }
  477. return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
  478. }
  479. #else /* 0 */
  480. FT_EXPORT_DEF( FT_Int32 )
  481. FT_Div64by32( FT_Int64* x,
  482. FT_Int32 y )
  483. {
  484. FT_Int32 s;
  485. FT_UInt32 q;
  486. s = x->hi;
  487. if ( s < 0 )
  488. {
  489. x->lo = (FT_UInt32)-(FT_Int32)x->lo;
  490. x->hi = ~x->hi + !x->lo;
  491. }
  492. s ^= y; y = FT_ABS( y );
  493. /* Shortcut */
  494. if ( x->hi == 0 )
  495. {
  496. if ( y > 0 )
  497. q = ( x->lo + ( y >> 1 ) ) / y;
  498. else
  499. q = 0x7FFFFFFFL;
  500. return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
  501. }
  502. q = ft_div64by32( x->hi, x->lo, y );
  503. return ( s < 0 ? -(FT_Int32)q : (FT_Int32)q );
  504. }
  505. #endif /* 0 */
  506. #endif /* 0 */
  507. #endif /* FT_LONG64 */
  508. /* documentation is in ftglyph.h */
  509. FT_EXPORT_DEF( void )
  510. FT_Matrix_Multiply( const FT_Matrix* a,
  511. FT_Matrix *b )
  512. {
  513. FT_Fixed xx, xy, yx, yy;
  514. if ( !a || !b )
  515. return;
  516. xx = FT_MulFix( a->xx, b->xx ) + FT_MulFix( a->xy, b->yx );
  517. xy = FT_MulFix( a->xx, b->xy ) + FT_MulFix( a->xy, b->yy );
  518. yx = FT_MulFix( a->yx, b->xx ) + FT_MulFix( a->yy, b->yx );
  519. yy = FT_MulFix( a->yx, b->xy ) + FT_MulFix( a->yy, b->yy );
  520. b->xx = xx; b->xy = xy;
  521. b->yx = yx; b->yy = yy;
  522. }
  523. /* documentation is in ftglyph.h */
  524. FT_EXPORT_DEF( FT_Error )
  525. FT_Matrix_Invert( FT_Matrix* matrix )
  526. {
  527. FT_Pos delta, xx, yy;
  528. if ( !matrix )
  529. return FT_Err_Invalid_Argument;
  530. /* compute discriminant */
  531. delta = FT_MulFix( matrix->xx, matrix->yy ) -
  532. FT_MulFix( matrix->xy, matrix->yx );
  533. if ( !delta )
  534. return FT_Err_Invalid_Argument; /* matrix can't be inverted */
  535. matrix->xy = - FT_DivFix( matrix->xy, delta );
  536. matrix->yx = - FT_DivFix( matrix->yx, delta );
  537. xx = matrix->xx;
  538. yy = matrix->yy;
  539. matrix->xx = FT_DivFix( yy, delta );
  540. matrix->yy = FT_DivFix( xx, delta );
  541. return FT_Err_Ok;
  542. }
  543. /* documentation is in ftcalc.h */
  544. FT_BASE_DEF( void )
  545. FT_Matrix_Multiply_Scaled( const FT_Matrix* a,
  546. FT_Matrix *b,
  547. FT_Long scaling )
  548. {
  549. FT_Fixed xx, xy, yx, yy;
  550. FT_Long val = 0x10000L * scaling;
  551. if ( !a || !b )
  552. return;
  553. xx = FT_MulDiv( a->xx, b->xx, val ) + FT_MulDiv( a->xy, b->yx, val );
  554. xy = FT_MulDiv( a->xx, b->xy, val ) + FT_MulDiv( a->xy, b->yy, val );
  555. yx = FT_MulDiv( a->yx, b->xx, val ) + FT_MulDiv( a->yy, b->yx, val );
  556. yy = FT_MulDiv( a->yx, b->xy, val ) + FT_MulDiv( a->yy, b->yy, val );
  557. b->xx = xx; b->xy = xy;
  558. b->yx = yx; b->yy = yy;
  559. }
  560. /* documentation is in ftcalc.h */
  561. FT_BASE_DEF( void )
  562. FT_Vector_Transform_Scaled( FT_Vector* vector,
  563. const FT_Matrix* matrix,
  564. FT_Long scaling )
  565. {
  566. FT_Pos xz, yz;
  567. FT_Long val = 0x10000L * scaling;
  568. if ( !vector || !matrix )
  569. return;
  570. xz = FT_MulDiv( vector->x, matrix->xx, val ) +
  571. FT_MulDiv( vector->y, matrix->xy, val );
  572. yz = FT_MulDiv( vector->x, matrix->yx, val ) +
  573. FT_MulDiv( vector->y, matrix->yy, val );
  574. vector->x = xz;
  575. vector->y = yz;
  576. }
  577. /* documentation is in ftcalc.h */
  578. FT_BASE_DEF( FT_Int32 )
  579. FT_SqrtFixed( FT_Int32 x )
  580. {
  581. FT_UInt32 root, rem_hi, rem_lo, test_div;
  582. FT_Int count;
  583. root = 0;
  584. if ( x > 0 )
  585. {
  586. rem_hi = 0;
  587. rem_lo = x;
  588. count = 24;
  589. do
  590. {
  591. rem_hi = ( rem_hi << 2 ) | ( rem_lo >> 30 );
  592. rem_lo <<= 2;
  593. root <<= 1;
  594. test_div = ( root << 1 ) + 1;
  595. if ( rem_hi >= test_div )
  596. {
  597. rem_hi -= test_div;
  598. root += 1;
  599. }
  600. } while ( --count );
  601. }
  602. return (FT_Int32)root;
  603. }
  604. /* documentation is in ftcalc.h */
  605. FT_BASE_DEF( FT_Int )
  606. ft_corner_orientation( FT_Pos in_x,
  607. FT_Pos in_y,
  608. FT_Pos out_x,
  609. FT_Pos out_y )
  610. {
  611. FT_Long result; /* avoid overflow on 16-bit system */
  612. /* deal with the trivial cases quickly */
  613. if ( in_y == 0 )
  614. {
  615. if ( in_x >= 0 )
  616. result = out_y;
  617. else
  618. result = -out_y;
  619. }
  620. else if ( in_x == 0 )
  621. {
  622. if ( in_y >= 0 )
  623. result = -out_x;
  624. else
  625. result = out_x;
  626. }
  627. else if ( out_y == 0 )
  628. {
  629. if ( out_x >= 0 )
  630. result = in_y;
  631. else
  632. result = -in_y;
  633. }
  634. else if ( out_x == 0 )
  635. {
  636. if ( out_y >= 0 )
  637. result = -in_x;
  638. else
  639. result = in_x;
  640. }
  641. else /* general case */
  642. {
  643. #ifdef FT_LONG64
  644. FT_Int64 delta = (FT_Int64)in_x * out_y - (FT_Int64)in_y * out_x;
  645. if ( delta == 0 )
  646. result = 0;
  647. else
  648. result = 1 - 2 * ( delta < 0 );
  649. #else
  650. FT_Int64 z1, z2;
  651. /* XXX: this function does not allow 64-bit arguments */
  652. ft_multo64( (FT_Int32)in_x, (FT_Int32)out_y, &z1 );
  653. ft_multo64( (FT_Int32)in_y, (FT_Int32)out_x, &z2 );
  654. if ( z1.hi > z2.hi )
  655. result = +1;
  656. else if ( z1.hi < z2.hi )
  657. result = -1;
  658. else if ( z1.lo > z2.lo )
  659. result = +1;
  660. else if ( z1.lo < z2.lo )
  661. result = -1;
  662. else
  663. result = 0;
  664. #endif
  665. }
  666. /* XXX: only the sign of return value, +1/0/-1 must be used */
  667. return (FT_Int)result;
  668. }
  669. /* documentation is in ftcalc.h */
  670. FT_BASE_DEF( FT_Int )
  671. ft_corner_is_flat( FT_Pos in_x,
  672. FT_Pos in_y,
  673. FT_Pos out_x,
  674. FT_Pos out_y )
  675. {
  676. FT_Pos ax = in_x;
  677. FT_Pos ay = in_y;
  678. FT_Pos d_in, d_out, d_corner;
  679. if ( ax < 0 )
  680. ax = -ax;
  681. if ( ay < 0 )
  682. ay = -ay;
  683. d_in = ax + ay;
  684. ax = out_x;
  685. if ( ax < 0 )
  686. ax = -ax;
  687. ay = out_y;
  688. if ( ay < 0 )
  689. ay = -ay;
  690. d_out = ax + ay;
  691. ax = out_x + in_x;
  692. if ( ax < 0 )
  693. ax = -ax;
  694. ay = out_y + in_y;
  695. if ( ay < 0 )
  696. ay = -ay;
  697. d_corner = ax + ay;
  698. return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );
  699. }
  700. /* END */