fttrigon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /***************************************************************************/
  2. /* */
  3. /* fttrigon.c */
  4. /* */
  5. /* FreeType trigonometric functions (body). */
  6. /* */
  7. /* Copyright 2001 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. #include <ft2build.h>
  18. #include FT_TRIGONOMETRY_H
  19. /* the following is 0.2715717684432231 * 2^30 */
  20. #define FT_TRIG_COSCALE 0x11616E8EUL
  21. /* this table was generated for FT_PI = 180L << 16, i.e. degrees */
  22. #define FT_TRIG_MAX_ITERS 23
  23. static const FT_Fixed
  24. ft_trig_arctan_table[24] =
  25. {
  26. 4157273L, 2949120L, 1740967L, 919879L, 466945L, 234379L, 117304L,
  27. 58666L, 29335L, 14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
  28. 57L, 29L, 14L, 7L, 4L, 2L, 1L
  29. };
  30. /* the Cordic shrink factor, multiplied by 2^32 */
  31. #define FT_TRIG_SCALE 1166391785UL /* 0x4585BA38UL */
  32. #ifdef FT_CONFIG_HAS_INT64
  33. /* multiply a given value by the CORDIC shrink factor */
  34. static FT_Fixed
  35. ft_trig_downscale( FT_Fixed val )
  36. {
  37. FT_Fixed s;
  38. FT_Int64 v;
  39. s = val;
  40. val = ( val >= 0 ) ? val : -val;
  41. v = ( val * (FT_Int64)FT_TRIG_SCALE ) + 0x100000000UL;
  42. val = (FT_Fixed)( v >> 32 );
  43. return ( s >= 0 ) ? val : -val;
  44. }
  45. #else /* !FT_CONFIG_HAS_INT64 */
  46. /* multiply a given value by the CORDIC shrink factor */
  47. static FT_Fixed
  48. ft_trig_downscale( FT_Fixed val )
  49. {
  50. FT_Fixed s;
  51. FT_UInt32 v1, v2, k1, k2, hi, lo1, lo2, lo3;
  52. s = val;
  53. val = ( val >= 0 ) ? val : -val;
  54. v1 = (FT_UInt32)val >> 16;
  55. v2 = (FT_UInt32)val & 0xFFFF;
  56. k1 = FT_TRIG_SCALE >> 16; /* constant */
  57. k2 = FT_TRIG_SCALE & 0xFFFF; /* constant */
  58. hi = k1 * v1;
  59. lo1 = k1 * v2 + k2 * v1; /* can't overflow */
  60. lo2 = ( k2 * v2 ) >> 16;
  61. lo3 = ( lo1 >= lo2 ) ? lo1 : lo2;
  62. lo1 += lo2;
  63. hi += lo1 >> 16;
  64. if ( lo1 < lo3 )
  65. hi += 0x10000UL;
  66. val = (FT_Fixed)hi;
  67. return ( s >= 0 ) ? val : -val;
  68. }
  69. #endif /* !FT_CONFIG_HAS_INT64 */
  70. static FT_Int
  71. ft_trig_prenorm( FT_Vector* vec )
  72. {
  73. FT_Fixed x, y, z;
  74. FT_Int shift;
  75. x = vec->x;
  76. y = vec->y;
  77. z = ( ( x >= 0 ) ? x : - x ) | ( (y >= 0) ? y : -y );
  78. shift = 0;
  79. if ( z < ( 1L << 27 ) )
  80. {
  81. do
  82. {
  83. shift++;
  84. z <<= 1;
  85. } while ( z < ( 1L << 27 ) );
  86. vec->x = x << shift;
  87. vec->y = y << shift;
  88. }
  89. else if ( z > ( 1L << 28 ) )
  90. {
  91. do
  92. {
  93. shift++;
  94. z >>= 1;
  95. } while ( z > ( 1L << 28 ) );
  96. vec->x = x >> shift;
  97. vec->y = y >> shift;
  98. shift = -shift;
  99. }
  100. return shift;
  101. }
  102. static void
  103. ft_trig_pseudo_rotate( FT_Vector* vec,
  104. FT_Angle theta )
  105. {
  106. FT_Int i;
  107. FT_Fixed x, y, xtemp;
  108. const FT_Fixed *arctanptr;
  109. x = vec->x;
  110. y = vec->y;
  111. /* Get angle between -90 and 90 degrees */
  112. while ( theta <= -FT_ANGLE_PI2 )
  113. {
  114. x = -x;
  115. y = -y;
  116. theta += FT_ANGLE_PI;
  117. }
  118. while ( theta > FT_ANGLE_PI2 )
  119. {
  120. x = -x;
  121. y = -y;
  122. theta -= FT_ANGLE_PI;
  123. }
  124. /* Initial pseudorotation, with left shift */
  125. arctanptr = ft_trig_arctan_table;
  126. if ( theta < 0 )
  127. {
  128. xtemp = x + ( y << 1 );
  129. y = y - ( x << 1 );
  130. x = xtemp;
  131. theta += *arctanptr++;
  132. }
  133. else
  134. {
  135. xtemp = x - ( y << 1 );
  136. y = y + ( x << 1 );
  137. x = xtemp;
  138. theta -= *arctanptr++;
  139. }
  140. /* Subsequent pseudorotations, with right shifts */
  141. i = 0;
  142. do
  143. {
  144. if ( theta < 0 )
  145. {
  146. xtemp = x + ( y >> i );
  147. y = y - ( x >> i );
  148. x = xtemp;
  149. theta += *arctanptr++;
  150. }
  151. else
  152. {
  153. xtemp = x - ( y >> i );
  154. y = y + ( x >> i );
  155. x = xtemp;
  156. theta -= *arctanptr++;
  157. }
  158. } while ( ++i < FT_TRIG_MAX_ITERS );
  159. vec->x = x;
  160. vec->y = y;
  161. }
  162. static void
  163. ft_trig_pseudo_polarize( FT_Vector* vec )
  164. {
  165. FT_Fixed theta;
  166. FT_Fixed yi, i;
  167. FT_Fixed x, y;
  168. const FT_Fixed *arctanptr;
  169. x = vec->x;
  170. y = vec->y;
  171. /* Get the vector into the right half plane */
  172. theta = 0;
  173. if ( x < 0 )
  174. {
  175. x = -x;
  176. y = -y;
  177. theta = 2 * FT_ANGLE_PI2;
  178. }
  179. if ( y > 0 )
  180. theta = - theta;
  181. arctanptr = ft_trig_arctan_table;
  182. if ( y < 0 )
  183. {
  184. /* Rotate positive */
  185. yi = y + ( x << 1 );
  186. x = x - ( y << 1 );
  187. y = yi;
  188. theta -= *arctanptr++; /* Subtract angle */
  189. }
  190. else
  191. {
  192. /* Rotate negative */
  193. yi = y - ( x << 1 );
  194. x = x + ( y << 1 );
  195. y = yi;
  196. theta += *arctanptr++; /* Add angle */
  197. }
  198. i = 0;
  199. do
  200. {
  201. if ( y < 0 )
  202. {
  203. /* Rotate positive */
  204. yi = y + ( x >> i );
  205. x = x - ( y >> i );
  206. y = yi;
  207. theta -= *arctanptr++;
  208. }
  209. else
  210. {
  211. /* Rotate negative */
  212. yi = y - ( x >> i );
  213. x = x + ( y >> i );
  214. y = yi;
  215. theta += *arctanptr++;
  216. }
  217. } while ( ++i < FT_TRIG_MAX_ITERS );
  218. /* round theta */
  219. if ( theta >= 0 )
  220. theta = ( theta + 16 ) & -32;
  221. else
  222. theta = - (( -theta + 16 ) & -32);
  223. vec->x = x;
  224. vec->y = theta;
  225. }
  226. /* documentation is in fttrigon.h */
  227. FT_EXPORT_DEF( FT_Fixed )
  228. FT_Cos( FT_Angle angle )
  229. {
  230. FT_Vector v;
  231. v.x = FT_TRIG_COSCALE >> 2;
  232. v.y = 0;
  233. ft_trig_pseudo_rotate( &v, angle );
  234. return v.x / ( 1 << 12 );
  235. }
  236. /* documentation is in fttrigon.h */
  237. FT_EXPORT_DEF( FT_Fixed )
  238. FT_Sin( FT_Angle angle )
  239. {
  240. return FT_Cos( FT_ANGLE_PI2 - angle );
  241. }
  242. /* documentation is in fttrigon.h */
  243. FT_EXPORT_DEF( FT_Fixed )
  244. FT_Tan( FT_Angle angle )
  245. {
  246. FT_Vector v;
  247. v.x = FT_TRIG_COSCALE >> 2;
  248. v.y = 0;
  249. ft_trig_pseudo_rotate( &v, angle );
  250. return FT_DivFix( v.y, v.x );
  251. }
  252. /* documentation is in fttrigon.h */
  253. FT_EXPORT_DEF( FT_Angle )
  254. FT_Atan2( FT_Fixed dx,
  255. FT_Fixed dy )
  256. {
  257. FT_Vector v;
  258. if ( dx == 0 && dy == 0 )
  259. return 0;
  260. v.x = dx;
  261. v.y = dy;
  262. ft_trig_prenorm( &v );
  263. ft_trig_pseudo_polarize( &v );
  264. return v.y;
  265. }
  266. /* documentation is in fttrigon.h */
  267. FT_EXPORT_DEF( void )
  268. FT_Vector_Unit( FT_Vector* vec,
  269. FT_Angle angle )
  270. {
  271. vec->x = FT_TRIG_COSCALE >> 2;
  272. vec->y = 0;
  273. ft_trig_pseudo_rotate( vec, angle );
  274. vec->x >>= 12;
  275. vec->y >>= 12;
  276. }
  277. /* documentation is in fttrigon.h */
  278. FT_EXPORT_DEF( void )
  279. FT_Vector_Rotate( FT_Vector* vec,
  280. FT_Angle angle )
  281. {
  282. FT_Int shift;
  283. FT_Vector v;
  284. v.x = vec->x;
  285. v.y = vec->y;
  286. if ( angle && ( v.x != 0 || v.y != 0 ) )
  287. {
  288. shift = ft_trig_prenorm( &v );
  289. ft_trig_pseudo_rotate( &v, angle );
  290. v.x = ft_trig_downscale( v.x );
  291. v.y = ft_trig_downscale( v.y );
  292. if ( shift >= 0 )
  293. {
  294. vec->x = v.x >> shift;
  295. vec->y = v.y >> shift;
  296. }
  297. else
  298. {
  299. shift = -shift;
  300. vec->x = v.x << shift;
  301. vec->y = v.y << shift;
  302. }
  303. }
  304. }
  305. /* documentation is in fttrigon.h */
  306. FT_EXPORT_DEF( FT_Fixed )
  307. FT_Vector_Length( FT_Vector* vec )
  308. {
  309. FT_Int shift;
  310. FT_Vector v;
  311. v = *vec;
  312. /* handle trivial cases */
  313. if ( v.x == 0 )
  314. {
  315. return ( v.y >= 0 ) ? v.y : -v.y;
  316. }
  317. else if ( v.y == 0 )
  318. {
  319. return ( v.x >= 0 ) ? v.x : -v.x;
  320. }
  321. /* general case */
  322. shift = ft_trig_prenorm( &v );
  323. ft_trig_pseudo_polarize( &v );
  324. v.x = ft_trig_downscale( v.x );
  325. if ( shift > 0 )
  326. return ( v.x + ( 1 << ( shift - 1 ) ) ) >> shift;
  327. return v.x << -shift;
  328. }
  329. /* documentation is in fttrigon.h */
  330. FT_EXPORT_DEF( void )
  331. FT_Vector_Polarize( FT_Vector* vec,
  332. FT_Fixed *length,
  333. FT_Angle *angle )
  334. {
  335. FT_Int shift;
  336. FT_Vector v;
  337. v = *vec;
  338. if ( v.x == 0 && v.y == 0 )
  339. return;
  340. shift = ft_trig_prenorm( &v );
  341. ft_trig_pseudo_polarize( &v );
  342. v.x = ft_trig_downscale( v.x );
  343. *length = ( shift >= 0 ) ? ( v.x >> shift ) : ( v.x << -shift );
  344. *angle = v.y;
  345. }
  346. /* documentation is in fttrigon.h */
  347. FT_EXPORT_DEF( void )
  348. FT_Vector_From_Polar( FT_Vector* vec,
  349. FT_Fixed length,
  350. FT_Angle angle )
  351. {
  352. vec->x = length;
  353. vec->y = 0;
  354. FT_Vector_Rotate( vec, angle );
  355. }
  356. /* documentation is in fttrigon.h */
  357. FT_EXPORT_DEF( FT_Angle )
  358. FT_Angle_Diff( FT_Angle angle1,
  359. FT_Angle angle2 )
  360. {
  361. FT_Angle delta = angle2 - angle1;
  362. delta %= FT_ANGLE_2PI;
  363. if ( delta > FT_ANGLE_PI )
  364. delta -= FT_ANGLE_2PI;
  365. return delta;
  366. }
  367. /* END */