1 /*         ______   ___    ___
2  *        /\  _  \ /\_ \  /\_ \
3  *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
4  *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
5  *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
6  *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7  *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8  *                                           /\____/
9  *                                           \_/__/
10  *
11  *      Fixed point math routines and lookup tables.
12  *
13  *      By Shawn Hargreaves.
14  *
15  *      See readme.txt for copyright information.
16  */
17 
18 
19 #include <math.h>
20 
21 #include "allegro.h"
22 
23 
24 
25 fixed _cos_tbl[512] =
26 {
27    /* precalculated fixed point (16.16) cosines for a full circle (0-255) */
28 
29    65536L,  65531L,  65516L,  65492L,  65457L,  65413L,  65358L,  65294L,
30    65220L,  65137L,  65043L,  64940L,  64827L,  64704L,  64571L,  64429L,
31    64277L,  64115L,  63944L,  63763L,  63572L,  63372L,  63162L,  62943L,
32    62714L,  62476L,  62228L,  61971L,  61705L,  61429L,  61145L,  60851L,
33    60547L,  60235L,  59914L,  59583L,  59244L,  58896L,  58538L,  58172L,
34    57798L,  57414L,  57022L,  56621L,  56212L,  55794L,  55368L,  54934L,
35    54491L,  54040L,  53581L,  53114L,  52639L,  52156L,  51665L,  51166L,
36    50660L,  50146L,  49624L,  49095L,  48559L,  48015L,  47464L,  46906L,
37    46341L,  45769L,  45190L,  44604L,  44011L,  43412L,  42806L,  42194L,
38    41576L,  40951L,  40320L,  39683L,  39040L,  38391L,  37736L,  37076L,
39    36410L,  35738L,  35062L,  34380L,  33692L,  33000L,  32303L,  31600L,
40    30893L,  30182L,  29466L,  28745L,  28020L,  27291L,  26558L,  25821L,
41    25080L,  24335L,  23586L,  22834L,  22078L,  21320L,  20557L,  19792L,
42    19024L,  18253L,  17479L,  16703L,  15924L,  15143L,  14359L,  13573L,
43    12785L,  11996L,  11204L,  10411L,  9616L,   8820L,   8022L,   7224L,
44    6424L,   5623L,   4821L,   4019L,   3216L,   2412L,   1608L,   804L,
45    0L,      -804L,   -1608L,  -2412L,  -3216L,  -4019L,  -4821L,  -5623L,
46    -6424L,  -7224L,  -8022L,  -8820L,  -9616L,  -10411L, -11204L, -11996L,
47    -12785L, -13573L, -14359L, -15143L, -15924L, -16703L, -17479L, -18253L,
48    -19024L, -19792L, -20557L, -21320L, -22078L, -22834L, -23586L, -24335L,
49    -25080L, -25821L, -26558L, -27291L, -28020L, -28745L, -29466L, -30182L,
50    -30893L, -31600L, -32303L, -33000L, -33692L, -34380L, -35062L, -35738L,
51    -36410L, -37076L, -37736L, -38391L, -39040L, -39683L, -40320L, -40951L,
52    -41576L, -42194L, -42806L, -43412L, -44011L, -44604L, -45190L, -45769L,
53    -46341L, -46906L, -47464L, -48015L, -48559L, -49095L, -49624L, -50146L,
54    -50660L, -51166L, -51665L, -52156L, -52639L, -53114L, -53581L, -54040L,
55    -54491L, -54934L, -55368L, -55794L, -56212L, -56621L, -57022L, -57414L,
56    -57798L, -58172L, -58538L, -58896L, -59244L, -59583L, -59914L, -60235L,
57    -60547L, -60851L, -61145L, -61429L, -61705L, -61971L, -62228L, -62476L,
58    -62714L, -62943L, -63162L, -63372L, -63572L, -63763L, -63944L, -64115L,
59    -64277L, -64429L, -64571L, -64704L, -64827L, -64940L, -65043L, -65137L,
60    -65220L, -65294L, -65358L, -65413L, -65457L, -65492L, -65516L, -65531L,
61    -65536L, -65531L, -65516L, -65492L, -65457L, -65413L, -65358L, -65294L,
62    -65220L, -65137L, -65043L, -64940L, -64827L, -64704L, -64571L, -64429L,
63    -64277L, -64115L, -63944L, -63763L, -63572L, -63372L, -63162L, -62943L,
64    -62714L, -62476L, -62228L, -61971L, -61705L, -61429L, -61145L, -60851L,
65    -60547L, -60235L, -59914L, -59583L, -59244L, -58896L, -58538L, -58172L,
66    -57798L, -57414L, -57022L, -56621L, -56212L, -55794L, -55368L, -54934L,
67    -54491L, -54040L, -53581L, -53114L, -52639L, -52156L, -51665L, -51166L,
68    -50660L, -50146L, -49624L, -49095L, -48559L, -48015L, -47464L, -46906L,
69    -46341L, -45769L, -45190L, -44604L, -44011L, -43412L, -42806L, -42194L,
70    -41576L, -40951L, -40320L, -39683L, -39040L, -38391L, -37736L, -37076L,
71    -36410L, -35738L, -35062L, -34380L, -33692L, -33000L, -32303L, -31600L,
72    -30893L, -30182L, -29466L, -28745L, -28020L, -27291L, -26558L, -25821L,
73    -25080L, -24335L, -23586L, -22834L, -22078L, -21320L, -20557L, -19792L,
74    -19024L, -18253L, -17479L, -16703L, -15924L, -15143L, -14359L, -13573L,
75    -12785L, -11996L, -11204L, -10411L, -9616L,  -8820L,  -8022L,  -7224L,
76    -6424L,  -5623L,  -4821L,  -4019L,  -3216L,  -2412L,  -1608L,  -804L,
77    0L,      804L,    1608L,   2412L,   3216L,   4019L,   4821L,   5623L,
78    6424L,   7224L,   8022L,   8820L,   9616L,   10411L,  11204L,  11996L,
79    12785L,  13573L,  14359L,  15143L,  15924L,  16703L,  17479L,  18253L,
80    19024L,  19792L,  20557L,  21320L,  22078L,  22834L,  23586L,  24335L,
81    25080L,  25821L,  26558L,  27291L,  28020L,  28745L,  29466L,  30182L,
82    30893L,  31600L,  32303L,  33000L,  33692L,  34380L,  35062L,  35738L,
83    36410L,  37076L,  37736L,  38391L,  39040L,  39683L,  40320L,  40951L,
84    41576L,  42194L,  42806L,  43412L,  44011L,  44604L,  45190L,  45769L,
85    46341L,  46906L,  47464L,  48015L,  48559L,  49095L,  49624L,  50146L,
86    50660L,  51166L,  51665L,  52156L,  52639L,  53114L,  53581L,  54040L,
87    54491L,  54934L,  55368L,  55794L,  56212L,  56621L,  57022L,  57414L,
88    57798L,  58172L,  58538L,  58896L,  59244L,  59583L,  59914L,  60235L,
89    60547L,  60851L,  61145L,  61429L,  61705L,  61971L,  62228L,  62476L,
90    62714L,  62943L,  63162L,  63372L,  63572L,  63763L,  63944L,  64115L,
91    64277L,  64429L,  64571L,  64704L,  64827L,  64940L,  65043L,  65137L,
92    65220L,  65294L,  65358L,  65413L,  65457L,  65492L,  65516L,  65531L
93 };
94 
95 
96 
97 fixed _tan_tbl[256] =
98 {
99    /* precalculated fixed point (16.16) tangents for a half circle (0-127) */
100 
101    0L,      804L,    1609L,   2414L,   3220L,   4026L,   4834L,   5644L,
102    6455L,   7268L,   8083L,   8901L,   9721L,   10545L,  11372L,  12202L,
103    13036L,  13874L,  14717L,  15564L,  16416L,  17273L,  18136L,  19005L,
104    19880L,  20762L,  21650L,  22546L,  23449L,  24360L,  25280L,  26208L,
105    27146L,  28093L,  29050L,  30018L,  30996L,  31986L,  32988L,  34002L,
106    35030L,  36071L,  37126L,  38196L,  39281L,  40382L,  41500L,  42636L,
107    43790L,  44963L,  46156L,  47369L,  48605L,  49863L,  51145L,  52451L,
108    53784L,  55144L,  56532L,  57950L,  59398L,  60880L,  62395L,  63947L,
109    65536L,  67165L,  68835L,  70548L,  72308L,  74116L,  75974L,  77887L,
110    79856L,  81885L,  83977L,  86135L,  88365L,  90670L,  93054L,  95523L,
111    98082L,  100736L, 103493L, 106358L, 109340L, 112447L, 115687L, 119071L,
112    122609L, 126314L, 130198L, 134276L, 138564L, 143081L, 147847L, 152884L,
113    158218L, 163878L, 169896L, 176309L, 183161L, 190499L, 198380L, 206870L,
114    216043L, 225990L, 236817L, 248648L, 261634L, 275959L, 291845L, 309568L,
115    329472L, 351993L, 377693L, 407305L, 441808L, 482534L, 531352L, 590958L,
116    665398L, 761030L, 888450L, 1066730L,1334016L,1779314L,2669641L,5340086L,
117    -2147483647L,-5340086L,-2669641L,-1779314L,-1334016L,-1066730L,-888450L,-761030L,
118    -665398L,-590958L,-531352L,-482534L,-441808L,-407305L,-377693L,-351993L,
119    -329472L,-309568L,-291845L,-275959L,-261634L,-248648L,-236817L,-225990L,
120    -216043L,-206870L,-198380L,-190499L,-183161L,-176309L,-169896L,-163878L,
121    -158218L,-152884L,-147847L,-143081L,-138564L,-134276L,-130198L,-126314L,
122    -122609L,-119071L,-115687L,-112447L,-109340L,-106358L,-103493L,-100736L,
123    -98082L, -95523L, -93054L, -90670L, -88365L, -86135L, -83977L, -81885L,
124    -79856L, -77887L, -75974L, -74116L, -72308L, -70548L, -68835L, -67165L,
125    -65536L, -63947L, -62395L, -60880L, -59398L, -57950L, -56532L, -55144L,
126    -53784L, -52451L, -51145L, -49863L, -48605L, -47369L, -46156L, -44963L,
127    -43790L, -42636L, -41500L, -40382L, -39281L, -38196L, -37126L, -36071L,
128    -35030L, -34002L, -32988L, -31986L, -30996L, -30018L, -29050L, -28093L,
129    -27146L, -26208L, -25280L, -24360L, -23449L, -22546L, -21650L, -20762L,
130    -19880L, -19005L, -18136L, -17273L, -16416L, -15564L, -14717L, -13874L,
131    -13036L, -12202L, -11372L, -10545L, -9721L,  -8901L,  -8083L,  -7268L,
132    -6455L,  -5644L,  -4834L,  -4026L,  -3220L,  -2414L,  -1609L,  -804L
133 };
134 
135 
136 
137 fixed _acos_tbl[513] =
138 {
139    /* precalculated fixed point (16.16) inverse cosines (-1 to 1) */
140 
141    0x800000L,  0x7C65C7L,  0x7AE75AL,  0x79C19EL,  0x78C9BEL,  0x77EF25L,  0x772953L,  0x76733AL,
142    0x75C991L,  0x752A10L,  0x74930CL,  0x740345L,  0x7379C1L,  0x72F5BAL,  0x72768FL,  0x71FBBCL,
143    0x7184D3L,  0x711174L,  0x70A152L,  0x703426L,  0x6FC9B5L,  0x6F61C9L,  0x6EFC36L,  0x6E98D1L,
144    0x6E3777L,  0x6DD805L,  0x6D7A5EL,  0x6D1E68L,  0x6CC40BL,  0x6C6B2FL,  0x6C13C1L,  0x6BBDAFL,
145    0x6B68E6L,  0x6B1558L,  0x6AC2F5L,  0x6A71B1L,  0x6A217EL,  0x69D251L,  0x698420L,  0x6936DFL,
146    0x68EA85L,  0x689F0AL,  0x685465L,  0x680A8DL,  0x67C17DL,  0x67792CL,  0x673194L,  0x66EAAFL,
147    0x66A476L,  0x665EE5L,  0x6619F5L,  0x65D5A2L,  0x6591E7L,  0x654EBFL,  0x650C26L,  0x64CA18L,
148    0x648890L,  0x64478CL,  0x640706L,  0x63C6FCL,  0x63876BL,  0x63484FL,  0x6309A5L,  0x62CB6AL,
149    0x628D9CL,  0x625037L,  0x621339L,  0x61D69FL,  0x619A68L,  0x615E90L,  0x612316L,  0x60E7F7L,
150    0x60AD31L,  0x6072C3L,  0x6038A9L,  0x5FFEE3L,  0x5FC56EL,  0x5F8C49L,  0x5F5372L,  0x5F1AE7L,
151    0x5EE2A7L,  0x5EAAB0L,  0x5E7301L,  0x5E3B98L,  0x5E0473L,  0x5DCD92L,  0x5D96F3L,  0x5D6095L,
152    0x5D2A76L,  0x5CF496L,  0x5CBEF2L,  0x5C898BL,  0x5C545EL,  0x5C1F6BL,  0x5BEAB0L,  0x5BB62DL,
153    0x5B81E1L,  0x5B4DCAL,  0x5B19E7L,  0x5AE638L,  0x5AB2BCL,  0x5A7F72L,  0x5A4C59L,  0x5A1970L,
154    0x59E6B6L,  0x59B42AL,  0x5981CCL,  0x594F9BL,  0x591D96L,  0x58EBBDL,  0x58BA0EL,  0x588889L,
155    0x58572DL,  0x5825FAL,  0x57F4EEL,  0x57C40AL,  0x57934DL,  0x5762B5L,  0x573243L,  0x5701F5L,
156    0x56D1CCL,  0x56A1C6L,  0x5671E4L,  0x564224L,  0x561285L,  0x55E309L,  0x55B3ADL,  0x558471L,
157    0x555555L,  0x552659L,  0x54F77BL,  0x54C8BCL,  0x549A1BL,  0x546B98L,  0x543D31L,  0x540EE7L,
158    0x53E0B9L,  0x53B2A7L,  0x5384B0L,  0x5356D4L,  0x532912L,  0x52FB6BL,  0x52CDDDL,  0x52A068L,
159    0x52730CL,  0x5245C9L,  0x52189EL,  0x51EB8BL,  0x51BE8FL,  0x5191AAL,  0x5164DCL,  0x513825L,
160    0x510B83L,  0x50DEF7L,  0x50B280L,  0x50861FL,  0x5059D2L,  0x502D99L,  0x500175L,  0x4FD564L,
161    0x4FA967L,  0x4F7D7DL,  0x4F51A6L,  0x4F25E2L,  0x4EFA30L,  0x4ECE90L,  0x4EA301L,  0x4E7784L,
162    0x4E4C19L,  0x4E20BEL,  0x4DF574L,  0x4DCA3AL,  0x4D9F10L,  0x4D73F6L,  0x4D48ECL,  0x4D1DF1L,
163    0x4CF305L,  0x4CC829L,  0x4C9D5AL,  0x4C729AL,  0x4C47E9L,  0x4C1D45L,  0x4BF2AEL,  0x4BC826L,
164    0x4B9DAAL,  0x4B733BL,  0x4B48D9L,  0x4B1E84L,  0x4AF43BL,  0x4AC9FEL,  0x4A9FCDL,  0x4A75A7L,
165    0x4A4B8DL,  0x4A217EL,  0x49F77AL,  0x49CD81L,  0x49A393L,  0x4979AFL,  0x494FD5L,  0x492605L,
166    0x48FC3FL,  0x48D282L,  0x48A8CFL,  0x487F25L,  0x485584L,  0x482BECL,  0x48025DL,  0x47D8D6L,
167    0x47AF57L,  0x4785E0L,  0x475C72L,  0x47330AL,  0x4709ABL,  0x46E052L,  0x46B701L,  0x468DB7L,
168    0x466474L,  0x463B37L,  0x461201L,  0x45E8D0L,  0x45BFA6L,  0x459682L,  0x456D64L,  0x45444BL,
169    0x451B37L,  0x44F229L,  0x44C920L,  0x44A01CL,  0x44771CL,  0x444E21L,  0x44252AL,  0x43FC38L,
170    0x43D349L,  0x43AA5FL,  0x438178L,  0x435894L,  0x432FB4L,  0x4306D8L,  0x42DDFEL,  0x42B527L,
171    0x428C53L,  0x426381L,  0x423AB2L,  0x4211E5L,  0x41E91AL,  0x41C051L,  0x41978AL,  0x416EC5L,
172    0x414601L,  0x411D3EL,  0x40F47CL,  0x40CBBBL,  0x40A2FBL,  0x407A3CL,  0x40517DL,  0x4028BEL,
173    0x400000L,  0x3FD742L,  0x3FAE83L,  0x3F85C4L,  0x3F5D05L,  0x3F3445L,  0x3F0B84L,  0x3EE2C2L,
174    0x3EB9FFL,  0x3E913BL,  0x3E6876L,  0x3E3FAFL,  0x3E16E6L,  0x3DEE1BL,  0x3DC54EL,  0x3D9C7FL,
175    0x3D73ADL,  0x3D4AD9L,  0x3D2202L,  0x3CF928L,  0x3CD04CL,  0x3CA76CL,  0x3C7E88L,  0x3C55A1L,
176    0x3C2CB7L,  0x3C03C8L,  0x3BDAD6L,  0x3BB1DFL,  0x3B88E4L,  0x3B5FE4L,  0x3B36E0L,  0x3B0DD7L,
177    0x3AE4C9L,  0x3ABBB5L,  0x3A929CL,  0x3A697EL,  0x3A405AL,  0x3A1730L,  0x39EDFFL,  0x39C4C9L,
178    0x399B8CL,  0x397249L,  0x3948FFL,  0x391FAEL,  0x38F655L,  0x38CCF6L,  0x38A38EL,  0x387A20L,
179    0x3850A9L,  0x38272AL,  0x37FDA3L,  0x37D414L,  0x37AA7CL,  0x3780DBL,  0x375731L,  0x372D7EL,
180    0x3703C1L,  0x36D9FBL,  0x36B02BL,  0x368651L,  0x365C6DL,  0x36327FL,  0x360886L,  0x35DE82L,
181    0x35B473L,  0x358A59L,  0x356033L,  0x353602L,  0x350BC5L,  0x34E17CL,  0x34B727L,  0x348CC5L,
182    0x346256L,  0x3437DAL,  0x340D52L,  0x33E2BBL,  0x33B817L,  0x338D66L,  0x3362A6L,  0x3337D7L,
183    0x330CFBL,  0x32E20FL,  0x32B714L,  0x328C0AL,  0x3260F0L,  0x3235C6L,  0x320A8CL,  0x31DF42L,
184    0x31B3E7L,  0x31887CL,  0x315CFFL,  0x313170L,  0x3105D0L,  0x30DA1EL,  0x30AE5AL,  0x308283L,
185    0x305699L,  0x302A9CL,  0x2FFE8BL,  0x2FD267L,  0x2FA62EL,  0x2F79E1L,  0x2F4D80L,  0x2F2109L,
186    0x2EF47DL,  0x2EC7DBL,  0x2E9B24L,  0x2E6E56L,  0x2E4171L,  0x2E1475L,  0x2DE762L,  0x2DBA37L,
187    0x2D8CF4L,  0x2D5F98L,  0x2D3223L,  0x2D0495L,  0x2CD6EEL,  0x2CA92CL,  0x2C7B50L,  0x2C4D59L,
188    0x2C1F47L,  0x2BF119L,  0x2BC2CFL,  0x2B9468L,  0x2B65E5L,  0x2B3744L,  0x2B0885L,  0x2AD9A7L,
189    0x2AAAABL,  0x2A7B8FL,  0x2A4C53L,  0x2A1CF7L,  0x29ED7BL,  0x29BDDCL,  0x298E1CL,  0x295E3AL,
190    0x292E34L,  0x28FE0BL,  0x28CDBDL,  0x289D4BL,  0x286CB3L,  0x283BF6L,  0x280B12L,  0x27DA06L,
191    0x27A8D3L,  0x277777L,  0x2745F2L,  0x271443L,  0x26E26AL,  0x26B065L,  0x267E34L,  0x264BD6L,
192    0x26194AL,  0x25E690L,  0x25B3A7L,  0x25808EL,  0x254D44L,  0x2519C8L,  0x24E619L,  0x24B236L,
193    0x247E1FL,  0x2449D3L,  0x241550L,  0x23E095L,  0x23ABA2L,  0x237675L,  0x23410EL,  0x230B6AL,
194    0x22D58AL,  0x229F6BL,  0x22690DL,  0x22326EL,  0x21FB8DL,  0x21C468L,  0x218CFFL,  0x215550L,
195    0x211D59L,  0x20E519L,  0x20AC8EL,  0x2073B7L,  0x203A92L,  0x20011DL,  0x1FC757L,  0x1F8D3DL,
196    0x1F52CFL,  0x1F1809L,  0x1EDCEAL,  0x1EA170L,  0x1E6598L,  0x1E2961L,  0x1DECC7L,  0x1DAFC9L,
197    0x1D7264L,  0x1D3496L,  0x1CF65BL,  0x1CB7B1L,  0x1C7895L,  0x1C3904L,  0x1BF8FAL,  0x1BB874L,
198    0x1B7770L,  0x1B35E8L,  0x1AF3DAL,  0x1AB141L,  0x1A6E19L,  0x1A2A5EL,  0x19E60BL,  0x19A11BL,
199    0x195B8AL,  0x191551L,  0x18CE6CL,  0x1886D4L,  0x183E83L,  0x17F573L,  0x17AB9BL,  0x1760F6L,
200    0x17157BL,  0x16C921L,  0x167BE0L,  0x162DAFL,  0x15DE82L,  0x158E4FL,  0x153D0BL,  0x14EAA8L,
201    0x14971AL,  0x144251L,  0x13EC3FL,  0x1394D1L,  0x133BF5L,  0x12E198L,  0x1285A2L,  0x1227FBL,
202    0x11C889L,  0x11672FL,  0x1103CAL,  0x109E37L,  0x10364BL,  0xFCBDAL,   0xF5EAEL,   0xEEE8CL,
203    0xE7B2DL,   0xE0444L,   0xD8971L,   0xD0A46L,   0xC863FL,   0xBFCBBL,   0xB6CF4L,   0xAD5F0L,
204    0xA366FL,   0x98CC6L,   0x8D6ADL,   0x810DBL,   0x73642L,   0x63E62L,   0x518A6L,   0x39A39L,
205    0x0L
206 };
207 
208 
209 
210 /* fixatan:
211  *  Fixed point inverse tangent. Does a binary search on the tan table.
212  */
fixatan(fixed x)213 fixed fixatan(fixed x)
214 {
215    int a, b, c;            /* for binary search */
216    fixed d;                /* difference value for search */
217 
218    if (x >= 0) {           /* search the first part of tan table */
219       a = 0;
220       b = 127;
221    }
222    else {                  /* search the second half instead */
223       a = 128;
224       b = 255;
225    }
226 
227    do {
228       c = (a + b) >> 1;
229       d = x - _tan_tbl[c];
230 
231       if (d > 0)
232 	 a = c + 1;
233       else
234 	 if (d < 0)
235 	    b = c - 1;
236 
237    } while ((a <= b) && (d));
238 
239    if (x >= 0)
240       return ((long)c) << 15;
241 
242    return (-0x00800000L + (((long)c) << 15));
243 }
244 
245 
246 
247 /* fixatan2:
248  *  Like the libc atan2, but for fixed point numbers.
249  */
fixatan2(fixed y,fixed x)250 fixed fixatan2(fixed y, fixed x)
251 {
252    fixed r;
253 
254    if (x==0) {
255       if (y==0) {
256 	 *allegro_errno = EDOM;
257 	 return 0L;
258       }
259       else
260 	 return ((y < 0) ? -0x00400000L : 0x00400000L);
261    }
262 
263    *allegro_errno = 0;
264    r = fixdiv(y, x);
265 
266    if (*allegro_errno) {
267       *allegro_errno = 0;
268       return ((y < 0) ? -0x00400000L : 0x00400000L);
269    }
270 
271    r = fixatan(r);
272 
273    if (x >= 0)
274       return r;
275 
276    if (y >= 0)
277       return 0x00800000L + r;
278 
279    return r - 0x00800000L;
280 }
281 
282 
283 
284 /* fixtorad_r, radtofix_r:
285  *  Ratios for converting between radians and fixed point angles.
286  */
287 AL_CONST fixed fixtorad_r = (fixed)1608;     /* 2pi/256 */
288 AL_CONST fixed radtofix_r = (fixed)2670177;  /* 256/2pi */
289 
290 
291 
292 #if (defined ALLEGRO_I386) && (!defined ALLEGRO_NO_ASM)
293 
294 
295 
296 unsigned short _sqrt_table[256] =
297 {
298    /* this table is used by the fixsqrt() and fixhypot() routines in imisc.s */
299 
300    0x2D4,   0x103F,  0x16CD,  0x1BDB,  0x201F,  0x23E3,  0x274B,  0x2A6D,
301    0x2D57,  0x3015,  0x32AC,  0x3524,  0x377F,  0x39C2,  0x3BEE,  0x3E08,
302    0x400F,  0x4207,  0x43F0,  0x45CC,  0x479C,  0x4960,  0x4B19,  0x4CC9,
303    0x4E6F,  0x500C,  0x51A2,  0x532F,  0x54B6,  0x5635,  0x57AE,  0x5921,
304    0x5A8D,  0x5BF4,  0x5D56,  0x5EB3,  0x600A,  0x615D,  0x62AB,  0x63F5,
305    0x653B,  0x667D,  0x67BA,  0x68F5,  0x6A2B,  0x6B5E,  0x6C8D,  0x6DBA,
306    0x6EE3,  0x7009,  0x712C,  0x724C,  0x7369,  0x7484,  0x759C,  0x76B1,
307    0x77C4,  0x78D4,  0x79E2,  0x7AEE,  0x7BF7,  0x7CFE,  0x7E04,  0x7F07,
308    0x8007,  0x8106,  0x8203,  0x82FF,  0x83F8,  0x84EF,  0x85E5,  0x86D9,
309    0x87CB,  0x88BB,  0x89AA,  0x8A97,  0x8B83,  0x8C6D,  0x8D56,  0x8E3D,
310    0x8F22,  0x9007,  0x90E9,  0x91CB,  0x92AB,  0x938A,  0x9467,  0x9543,
311    0x961E,  0x96F8,  0x97D0,  0x98A8,  0x997E,  0x9A53,  0x9B26,  0x9BF9,
312    0x9CCA,  0x9D9B,  0x9E6A,  0x9F39,  0xA006,  0xA0D2,  0xA19D,  0xA268,
313    0xA331,  0xA3F9,  0xA4C1,  0xA587,  0xA64D,  0xA711,  0xA7D5,  0xA898,
314    0xA95A,  0xAA1B,  0xAADB,  0xAB9A,  0xAC59,  0xAD16,  0xADD3,  0xAE8F,
315    0xAF4B,  0xB005,  0xB0BF,  0xB178,  0xB230,  0xB2E8,  0xB39F,  0xB455,
316    0xB50A,  0xB5BF,  0xB673,  0xB726,  0xB7D9,  0xB88A,  0xB93C,  0xB9EC,
317    0xBA9C,  0xBB4B,  0xBBFA,  0xBCA8,  0xBD55,  0xBE02,  0xBEAE,  0xBF5A,
318    0xC005,  0xC0AF,  0xC159,  0xC202,  0xC2AB,  0xC353,  0xC3FA,  0xC4A1,
319    0xC548,  0xC5ED,  0xC693,  0xC737,  0xC7DC,  0xC87F,  0xC923,  0xC9C5,
320    0xCA67,  0xCB09,  0xCBAA,  0xCC4B,  0xCCEB,  0xCD8B,  0xCE2A,  0xCEC8,
321    0xCF67,  0xD004,  0xD0A2,  0xD13F,  0xD1DB,  0xD277,  0xD312,  0xD3AD,
322    0xD448,  0xD4E2,  0xD57C,  0xD615,  0xD6AE,  0xD746,  0xD7DE,  0xD876,
323    0xD90D,  0xD9A4,  0xDA3A,  0xDAD0,  0xDB66,  0xDBFB,  0xDC90,  0xDD24,
324    0xDDB8,  0xDE4C,  0xDEDF,  0xDF72,  0xE004,  0xE096,  0xE128,  0xE1B9,
325    0xE24A,  0xE2DB,  0xE36B,  0xE3FB,  0xE48B,  0xE51A,  0xE5A9,  0xE637,
326    0xE6C5,  0xE753,  0xE7E1,  0xE86E,  0xE8FB,  0xE987,  0xEA13,  0xEA9F,
327    0xEB2B,  0xEBB6,  0xEC41,  0xECCB,  0xED55,  0xEDDF,  0xEE69,  0xEEF2,
328    0xEF7B,  0xF004,  0xF08C,  0xF114,  0xF19C,  0xF223,  0xF2AB,  0xF332,
329    0xF3B8,  0xF43E,  0xF4C4,  0xF54A,  0xF5D0,  0xF655,  0xF6DA,  0xF75E,
330    0xF7E3,  0xF867,  0xF8EA,  0xF96E,  0xF9F1,  0xFA74,  0xFAF7,  0xFB79,
331    0xFBFB,  0xFC7D,  0xFCFF,  0xFD80,  0xFE02,  0xFE82,  0xFF03,  0xFF83
332 };
333 
334 
335 
336 #else       /* not i386, so use straight C versions */
337 
338 
339 
340 /* fixsqrt:
341  *  Fixed point square root routine for non-i386.
342  */
fixsqrt(fixed x)343 fixed fixsqrt(fixed x)
344 {
345    if (x > 0)
346       return ftofix(sqrt(fixtof(x)));
347 
348    if (x < 0)
349       *allegro_errno = EDOM;
350 
351    return 0;
352 }
353 
354 
355 
356 /* fixhypot:
357  *  Fixed point sqrt (x*x+y*y) for non-i386.
358  */
fixhypot(fixed x,fixed y)359 fixed fixhypot(fixed x, fixed y)
360 {
361    return ftofix(hypot(fixtof(x), fixtof(y)));
362 }
363 
364 
365 
366 #endif      /* i386 vs. portable C implementations */
367 
368