math.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #include "platform_common.h"
  31. #define __FDLIBM_STDC__
  32. #ifndef FLT_EVAL_METHOD
  33. #define FLT_EVAL_METHOD 0
  34. #endif
  35. typedef uint32_t u_int32_t;
  36. typedef uint64_t u_int64_t;
  37. typedef union u32double_tag {
  38. int *pint;
  39. double *pdouble;
  40. } U32DOUBLE;
  41. static inline int *
  42. pdouble2pint(double *pdouble)
  43. {
  44. U32DOUBLE u;
  45. u.pdouble = pdouble;
  46. return u.pint;
  47. }
  48. typedef union
  49. {
  50. double value;
  51. struct
  52. {
  53. u_int32_t lsw;
  54. u_int32_t msw;
  55. } parts;
  56. struct
  57. {
  58. u_int64_t w;
  59. } xparts;
  60. } ieee_double_shape_type_little;
  61. typedef union
  62. {
  63. double value;
  64. struct
  65. {
  66. u_int32_t msw;
  67. u_int32_t lsw;
  68. } parts;
  69. struct
  70. {
  71. u_int64_t w;
  72. } xparts;
  73. } ieee_double_shape_type_big;
  74. typedef union {
  75. double d;
  76. struct {
  77. unsigned int manl :32;
  78. unsigned int manh :20;
  79. unsigned int exp :11;
  80. unsigned int sign :1;
  81. } bits;
  82. } IEEEd2bits_L;
  83. typedef union {
  84. double d;
  85. struct {
  86. unsigned int sign :1;
  87. unsigned int exp :11;
  88. unsigned int manh :20;
  89. unsigned int manl :32;
  90. } bits;
  91. } IEEEd2bits_B;
  92. typedef union {
  93. float f;
  94. struct {
  95. unsigned int man :23;
  96. unsigned int exp :8;
  97. unsigned int sign :1;
  98. } bits;
  99. } IEEEf2bits_L;
  100. typedef union {
  101. float f;
  102. struct {
  103. unsigned int sign :1;
  104. unsigned int exp :8;
  105. unsigned int man :23;
  106. } bits;
  107. } IEEEf2bits_B;
  108. static union {
  109. int a;
  110. char b;
  111. } __ue = { .a = 1 };
  112. #define is_little_endian() (__ue.b == 1)
  113. #define __HIL(x) *(1+pdouble2pint(&x))
  114. #define __LOL(x) *(pdouble2pint(&x))
  115. #define __HIB(x) *(pdouble2pint(&x))
  116. #define __LOB(x) *(1+pdouble2pint(&x))
  117. /* Get two 32 bit ints from a double. */
  118. #define EXTRACT_WORDS_L(ix0,ix1,d) \
  119. do { \
  120. ieee_double_shape_type_little ew_u; \
  121. ew_u.value = (d); \
  122. (ix0) = ew_u.parts.msw; \
  123. (ix1) = ew_u.parts.lsw; \
  124. } while (0)
  125. /* Set a double from two 32 bit ints. */
  126. #define INSERT_WORDS_L(d,ix0,ix1) \
  127. do { \
  128. ieee_double_shape_type_little iw_u; \
  129. iw_u.parts.msw = (ix0); \
  130. iw_u.parts.lsw = (ix1); \
  131. (d) = iw_u.value; \
  132. } while (0)
  133. /* Get two 32 bit ints from a double. */
  134. #define EXTRACT_WORDS_B(ix0,ix1,d) \
  135. do { \
  136. ieee_double_shape_type_big ew_u; \
  137. ew_u.value = (d); \
  138. (ix0) = ew_u.parts.msw; \
  139. (ix1) = ew_u.parts.lsw; \
  140. } while (0)
  141. /* Set a double from two 32 bit ints. */
  142. #define INSERT_WORDS_B(d,ix0,ix1) \
  143. do { \
  144. ieee_double_shape_type_big iw_u; \
  145. iw_u.parts.msw = (ix0); \
  146. iw_u.parts.lsw = (ix1); \
  147. (d) = iw_u.value; \
  148. } while (0)
  149. /* Get the more significant 32 bit int from a double. */
  150. #define GET_HIGH_WORD_L(i,d) \
  151. do { \
  152. ieee_double_shape_type_little gh_u; \
  153. gh_u.value = (d); \
  154. (i) = gh_u.parts.msw; \
  155. } while (0)
  156. /* Get the more significant 32 bit int from a double. */
  157. #define GET_HIGH_WORD_B(i,d) \
  158. do { \
  159. ieee_double_shape_type_big gh_u; \
  160. gh_u.value = (d); \
  161. (i) = gh_u.parts.msw; \
  162. } while (0)
  163. /* Set the more significant 32 bits of a double from an int. */
  164. #define SET_HIGH_WORD_L(d,v) \
  165. do { \
  166. ieee_double_shape_type_little sh_u; \
  167. sh_u.value = (d); \
  168. sh_u.parts.msw = (v); \
  169. (d) = sh_u.value; \
  170. } while (0)
  171. /* Set the more significant 32 bits of a double from an int. */
  172. #define SET_HIGH_WORD_B(d,v) \
  173. do { \
  174. ieee_double_shape_type_big sh_u; \
  175. sh_u.value = (d); \
  176. sh_u.parts.msw = (v); \
  177. (d) = sh_u.value; \
  178. } while (0)
  179. /* Set the less significant 32 bits of a double from an int. */
  180. #define SET_LOW_WORD_L(d,v) \
  181. do { \
  182. ieee_double_shape_type_little sh_u; \
  183. sh_u.value = (d); \
  184. sh_u.parts.lsw = (v); \
  185. (d) = sh_u.value; \
  186. } while (0)
  187. /* Set the more significant 32 bits of a double from an int. */
  188. #define SET_LOW_WORD_B(d,v) \
  189. do { \
  190. ieee_double_shape_type_big sh_u; \
  191. sh_u.value = (d); \
  192. sh_u.parts.lsw = (v); \
  193. (d) = sh_u.value; \
  194. } while (0)
  195. /* Get the less significant 32 bit int from a double. */
  196. #define GET_LOW_WORD_L(i,d) \
  197. do { \
  198. ieee_double_shape_type_little gl_u; \
  199. gl_u.value = (d); \
  200. (i) = gl_u.parts.lsw; \
  201. } while (0)
  202. /* Get the less significant 32 bit int from a double. */
  203. #define GET_LOW_WORD_B(i,d) \
  204. do { \
  205. ieee_double_shape_type_big gl_u; \
  206. gl_u.value = (d); \
  207. (i) = gl_u.parts.lsw; \
  208. } while (0)
  209. /*
  210. * A union which permits us to convert between a float and a 32 bit
  211. * int.
  212. */
  213. typedef union
  214. {
  215. float value;
  216. /* FIXME: Assumes 32 bit int. */
  217. unsigned int word;
  218. } ieee_float_shape_type;
  219. /* Get a 32 bit int from a float. */
  220. #define GET_FLOAT_WORD(i,d) \
  221. do { \
  222. ieee_float_shape_type gf_u; \
  223. gf_u.value = (d); \
  224. (i) = gf_u.word; \
  225. } while (0)
  226. /* Set a float from a 32 bit int. */
  227. #define SET_FLOAT_WORD(d,i) \
  228. do { \
  229. ieee_float_shape_type sf_u; \
  230. sf_u.word = (i); \
  231. (d) = sf_u.value; \
  232. } while (0)
  233. /* Macro wrappers. */
  234. #define EXTRACT_WORDS(ix0,ix1,d) do { \
  235. if (is_little_endian()) \
  236. EXTRACT_WORDS_L(ix0,ix1,d); \
  237. else \
  238. EXTRACT_WORDS_B(ix0,ix1,d); \
  239. } while (0)
  240. #define INSERT_WORDS(d,ix0,ix1) do { \
  241. if (is_little_endian()) \
  242. INSERT_WORDS_L(d,ix0,ix1); \
  243. else \
  244. INSERT_WORDS_B(d,ix0,ix1); \
  245. } while (0)
  246. #define GET_HIGH_WORD(i,d) \
  247. do { \
  248. if (is_little_endian()) \
  249. GET_HIGH_WORD_L(i,d); \
  250. else \
  251. GET_HIGH_WORD_B(i,d); \
  252. } while (0)
  253. #define SET_HIGH_WORD(d,v) \
  254. do { \
  255. if (is_little_endian()) \
  256. SET_HIGH_WORD_L(d,v); \
  257. else \
  258. SET_HIGH_WORD_B(d,v); \
  259. } while (0)
  260. #define GET_LOW_WORD(d,v) \
  261. do { \
  262. if (is_little_endian()) \
  263. GET_LOW_WORD_L(d,v); \
  264. else \
  265. GET_LOW_WORD_B(d,v); \
  266. } while (0)
  267. #define SET_LOW_WORD(d,v) \
  268. do { \
  269. if (is_little_endian()) \
  270. SET_LOW_WORD_L(d,v); \
  271. else \
  272. SET_LOW_WORD_B(d,v); \
  273. } while (0)
  274. #define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x))
  275. #define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x))
  276. /*
  277. * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
  278. */
  279. #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
  280. #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
  281. #else
  282. #define STRICT_ASSIGN(type, lval, rval) do { \
  283. volatile type __lval; \
  284. \
  285. if (sizeof(type) >= sizeof(long double)) \
  286. (lval) = (rval); \
  287. else { \
  288. __lval = (rval); \
  289. (lval) = __lval; \
  290. } \
  291. } while (0)
  292. #endif
  293. #ifdef __FDLIBM_STDC__
  294. static const double huge = 1.0e300;
  295. #else
  296. static double huge = 1.0e300;
  297. #endif
  298. #ifdef __STDC__
  299. static const double
  300. #else
  301. static double
  302. #endif
  303. tiny = 1.0e-300;
  304. #ifdef __STDC__
  305. static const double
  306. #else
  307. static double
  308. #endif
  309. one= 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
  310. #ifdef __STDC__
  311. static const double
  312. #else
  313. static double
  314. #endif
  315. TWO52[2]={
  316. 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
  317. -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
  318. };
  319. #ifdef __STDC__
  320. static const double
  321. #else
  322. static double
  323. #endif
  324. atanhi[] = {
  325. 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
  326. 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
  327. 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
  328. 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
  329. };
  330. #ifdef __STDC__
  331. static const double
  332. #else
  333. static double
  334. #endif
  335. atanlo[] = {
  336. 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */
  337. 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
  338. 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
  339. 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */
  340. };
  341. #ifdef __STDC__
  342. static const double
  343. #else
  344. static double
  345. #endif
  346. aT[] = {
  347. 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */
  348. -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */
  349. 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */
  350. -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */
  351. 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */
  352. -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */
  353. 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */
  354. -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */
  355. 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */
  356. -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */
  357. 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */
  358. };
  359. #ifdef __STDC__
  360. static const double
  361. #else
  362. static double
  363. #endif
  364. zero = 0.0,
  365. pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
  366. pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
  367. pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
  368. pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
  369. #ifdef __STDC__
  370. static const double
  371. #else
  372. static double
  373. #endif
  374. bp[] = {1.0, 1.5,},
  375. dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
  376. dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
  377. two = 2.0,
  378. two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */
  379. two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
  380. twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
  381. /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
  382. L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
  383. L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
  384. L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
  385. L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
  386. L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
  387. L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
  388. P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
  389. P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
  390. P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
  391. P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
  392. P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
  393. lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
  394. lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
  395. lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
  396. ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
  397. cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
  398. cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
  399. cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
  400. ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
  401. ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
  402. ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
  403. static double freebsd_sqrt(double x);
  404. static double freebsd_floor(double x);
  405. static double freebsd_ceil(double x);
  406. static double freebsd_fabs(double x);
  407. static double freebsd_rint(double x);
  408. static int freebsd_isnan(double x);
  409. static double freebsd_atan(double x);
  410. static double freebsd_atan2(double y, double x);
  411. static double freebsd_atan(double x)
  412. {
  413. double w,s1,s2,z;
  414. int32_t ix,hx,id;
  415. GET_HIGH_WORD(hx,x);
  416. ix = hx&0x7fffffff;
  417. if(ix>=0x44100000) { /* if |x| >= 2^66 */
  418. u_int32_t low;
  419. GET_LOW_WORD(low,x);
  420. if(ix>0x7ff00000||
  421. (ix==0x7ff00000&&(low!=0)))
  422. return x+x; /* NaN */
  423. if(hx>0) return atanhi[3]+*(volatile double *)&atanlo[3];
  424. else return -atanhi[3]-*(volatile double *)&atanlo[3];
  425. } if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
  426. if (ix < 0x3e400000) { /* |x| < 2^-27 */
  427. if(huge+x>one) return x; /* raise inexact */
  428. }
  429. id = -1;
  430. } else {
  431. x = freebsd_fabs(x);
  432. if (ix < 0x3ff30000) { /* |x| < 1.1875 */
  433. if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
  434. id = 0; x = (2.0*x-one)/(2.0+x);
  435. } else { /* 11/16<=|x|< 19/16 */
  436. id = 1; x = (x-one)/(x+one);
  437. }
  438. } else {
  439. if (ix < 0x40038000) { /* |x| < 2.4375 */
  440. id = 2; x = (x-1.5)/(one+1.5*x);
  441. } else { /* 2.4375 <= |x| < 2^66 */
  442. id = 3; x = -1.0/x;
  443. }
  444. }}
  445. /* end of argument reduction */
  446. z = x*x;
  447. w = z*z;
  448. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  449. s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));
  450. s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));
  451. if (id<0) return x - x*(s1+s2);
  452. else {
  453. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  454. return (hx<0)? -z:z;
  455. }
  456. }
  457. static double freebsd_atan2(double y, double x)
  458. {
  459. double z;
  460. int32_t k,m,hx,hy,ix,iy;
  461. u_int32_t lx,ly;
  462. EXTRACT_WORDS(hx,lx,x);
  463. ix = hx&0x7fffffff;
  464. EXTRACT_WORDS(hy,ly,y);
  465. iy = hy&0x7fffffff;
  466. if(((ix|((lx|-lx)>>31))>0x7ff00000)||
  467. ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */
  468. return x+y;
  469. if(hx==0x3ff00000&&lx==0) return freebsd_atan(y); /* x=1.0 */
  470. m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
  471. /* when y = 0 */
  472. if((iy|ly)==0) {
  473. switch(m) {
  474. case 0:
  475. case 1: return y; /* atan(+-0,+anything)=+-0 */
  476. case 2: return pi+tiny;/* atan(+0,-anything) = pi */
  477. case 3:
  478. default: return -pi-tiny;/* atan(-0,-anything) =-pi */
  479. }
  480. }
  481. /* when x = 0 */
  482. if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  483. /* when x is INF */
  484. if(ix==0x7ff00000) {
  485. if(iy==0x7ff00000) {
  486. switch(m) {
  487. case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
  488. case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
  489. case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
  490. case 3:
  491. default: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
  492. }
  493. } else {
  494. switch(m) {
  495. case 0: return zero ; /* atan(+...,+INF) */
  496. case 1: return -zero ; /* atan(-...,+INF) */
  497. case 2: return pi+tiny ; /* atan(+...,-INF) */
  498. case 3:
  499. default: return -pi-tiny ; /* atan(-...,-INF) */
  500. }
  501. }
  502. }
  503. /* when y is INF */
  504. if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  505. /* compute y/x */
  506. k = (iy-ix)>>20;
  507. if(k > 60) { /* |y/x| > 2**60 */
  508. z=pi_o_2+0.5*pi_lo;
  509. m&=1;
  510. }
  511. else if(hx<0&&k<-60) z=0.0; /* 0 > |y|/x > -2**-60 */
  512. else z=freebsd_atan(fabs(y/x)); /* safe to do y/x */
  513. switch (m) {
  514. case 0: return z ; /* atan(+,+) */
  515. case 1: return -z ; /* atan(-,+) */
  516. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  517. default: /* case 3 */
  518. return (z-pi_lo)-pi;/* atan(-,-) */
  519. }
  520. }
  521. static double freebsd_sqrt(double x) /* wrapper sqrt */
  522. {
  523. double z;
  524. int32_t sign = (int)0x80000000;
  525. int32_t ix0,s0,q,m,t,i;
  526. u_int32_t r,t1,s1,ix1,q1;
  527. EXTRACT_WORDS(ix0,ix1,x);
  528. /* take care of Inf and NaN */
  529. if((ix0&0x7ff00000)==0x7ff00000) {
  530. return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  531. sqrt(-inf)=sNaN */
  532. }
  533. /* take care of zero */
  534. if(ix0<=0) {
  535. if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
  536. else if(ix0<0)
  537. return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
  538. }
  539. /* normalize x */
  540. m = (ix0>>20);
  541. if(m==0) { /* subnormal x */
  542. while(ix0==0) {
  543. m -= 21;
  544. ix0 |= (ix1>>11); ix1 <<= 21;
  545. }
  546. for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
  547. m -= i-1;
  548. ix0 |= (ix1>>(32-i));
  549. ix1 <<= i;
  550. }
  551. m -= 1023; /* unbias exponent */
  552. ix0 = (ix0&0x000fffff)|0x00100000;
  553. if(m&1){ /* odd m, double x to make it even */
  554. ix0 += ix0 + ((ix1&sign)>>31);
  555. ix1 += ix1;
  556. }
  557. m >>= 1; /* m = [m/2] */
  558. /* generate sqrt(x) bit by bit */
  559. ix0 += ix0 + ((ix1&sign)>>31);
  560. ix1 += ix1;
  561. q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
  562. r = 0x00200000; /* r = moving bit from right to left */
  563. while(r!=0) {
  564. t = s0+r;
  565. if(t<=ix0) {
  566. s0 = t+r;
  567. ix0 -= t;
  568. q += r;
  569. }
  570. ix0 += ix0 + ((ix1&sign)>>31);
  571. ix1 += ix1;
  572. r>>=1;
  573. }
  574. r = sign;
  575. while(r!=0) {
  576. t1 = s1+r;
  577. t = s0;
  578. if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
  579. s1 = t1+r;
  580. if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
  581. ix0 -= t;
  582. if (ix1 < t1) ix0 -= 1;
  583. ix1 -= t1;
  584. q1 += r;
  585. }
  586. ix0 += ix0 + ((ix1&sign)>>31);
  587. ix1 += ix1;
  588. r>>=1;
  589. }
  590. /* use floating add to find out rounding direction */
  591. if((ix0|ix1)!=0) {
  592. z = one-tiny; /* trigger inexact flag */
  593. if (z>=one) {
  594. z = one+tiny;
  595. if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;}
  596. else if (z>one) {
  597. if (q1==(u_int32_t)0xfffffffe) q+=1;
  598. q1+=2;
  599. } else
  600. q1 += (q1&1);
  601. }
  602. }
  603. ix0 = (q>>1)+0x3fe00000;
  604. ix1 = q1>>1;
  605. if ((q&1)==1) ix1 |= sign;
  606. ix0 += (m <<20);
  607. INSERT_WORDS(z,ix0,ix1);
  608. return z;
  609. }
  610. static double freebsd_floor(double x)
  611. {
  612. int32_t i0,i1,j0;
  613. u_int32_t i,j;
  614. EXTRACT_WORDS(i0,i1,x);
  615. j0 = ((i0>>20)&0x7ff)-0x3ff;
  616. if(j0<20) {
  617. if(j0<0) { /* raise inexact if x != 0 */
  618. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  619. if(i0>=0) {i0=i1=0;}
  620. else if(((i0&0x7fffffff)|i1)!=0)
  621. { i0=0xbff00000;i1=0;}
  622. }
  623. } else {
  624. i = (0x000fffff)>>j0;
  625. if(((i0&i)|i1)==0) return x; /* x is integral */
  626. if(huge+x>0.0) { /* raise inexact flag */
  627. if(i0<0) i0 += (0x00100000)>>j0;
  628. i0 &= (~i); i1=0;
  629. }
  630. }
  631. } else if (j0>51) {
  632. if(j0==0x400) return x+x; /* inf or NaN */
  633. else return x; /* x is integral */
  634. } else {
  635. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  636. if((i1&i)==0) return x; /* x is integral */
  637. if(huge+x>0.0) { /* raise inexact flag */
  638. if(i0<0) {
  639. if(j0==20) i0+=1;
  640. else {
  641. j = i1+(1<<(52-j0));
  642. if(j<i1) i0 +=1 ; /* got a carry */
  643. i1=j;
  644. }
  645. }
  646. i1 &= (~i);
  647. }
  648. }
  649. INSERT_WORDS(x,i0,i1);
  650. return x;
  651. }
  652. static double freebsd_ceil(double x)
  653. {
  654. int32_t i0,i1,j0;
  655. u_int32_t i,j;
  656. EXTRACT_WORDS(i0,i1,x);
  657. j0 = ((i0>>20)&0x7ff)-0x3ff;
  658. if(j0<20) {
  659. if(j0<0) { /* raise inexact if x != 0 */
  660. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  661. if(i0<0) {i0=0x80000000;i1=0;}
  662. else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  663. }
  664. } else {
  665. i = (0x000fffff)>>j0;
  666. if(((i0&i)|i1)==0) return x; /* x is integral */
  667. if(huge+x>0.0) { /* raise inexact flag */
  668. if(i0>0) i0 += (0x00100000)>>j0;
  669. i0 &= (~i); i1=0;
  670. }
  671. }
  672. } else if (j0>51) {
  673. if(j0==0x400) return x+x; /* inf or NaN */
  674. else return x; /* x is integral */
  675. } else {
  676. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  677. if((i1&i)==0) return x; /* x is integral */
  678. if(huge+x>0.0) { /* raise inexact flag */
  679. if(i0>0) {
  680. if(j0==20) i0+=1;
  681. else {
  682. j = i1 + (1<<(52-j0));
  683. if(j<i1) i0+=1; /* got a carry */
  684. i1 = j;
  685. }
  686. }
  687. i1 &= (~i);
  688. }
  689. }
  690. INSERT_WORDS(x,i0,i1);
  691. return x;
  692. }
  693. static double freebsd_rint(double x)
  694. {
  695. int32_t i0,j0,sx;
  696. u_int32_t i,i1;
  697. double w,t;
  698. EXTRACT_WORDS(i0,i1,x);
  699. sx = (i0>>31)&1;
  700. j0 = ((i0>>20)&0x7ff)-0x3ff;
  701. if(j0<20) {
  702. if(j0<0) {
  703. if(((i0&0x7fffffff)|i1)==0) return x;
  704. i1 |= (i0&0x0fffff);
  705. i0 &= 0xfffe0000;
  706. i0 |= ((i1|-i1)>>12)&0x80000;
  707. SET_HIGH_WORD(x,i0);
  708. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  709. t = w-TWO52[sx];
  710. GET_HIGH_WORD(i0,t);
  711. SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31));
  712. return t;
  713. } else {
  714. i = (0x000fffff)>>j0;
  715. if(((i0&i)|i1)==0) return x; /* x is integral */
  716. i>>=1;
  717. if(((i0&i)|i1)!=0) {
  718. /*
  719. * Some bit is set after the 0.5 bit. To avoid the
  720. * possibility of errors from double rounding in
  721. * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
  722. * guard bit. We do this for all j0<=51. The
  723. * adjustment is trickiest for j0==18 and j0==19
  724. * since then it spans the word boundary.
  725. */
  726. if(j0==19) i1 = 0x40000000; else
  727. if(j0==18) i1 = 0x80000000; else
  728. i0 = (i0&(~i))|((0x20000)>>j0);
  729. }
  730. }
  731. } else if (j0>51) {
  732. if(j0==0x400) return x+x; /* inf or NaN */
  733. else return x; /* x is integral */
  734. } else {
  735. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  736. if((i1&i)==0) return x; /* x is integral */
  737. i>>=1;
  738. if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20));
  739. }
  740. INSERT_WORDS(x,i0,i1);
  741. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  742. return w-TWO52[sx];
  743. }
  744. static int freebsd_isnan(double d)
  745. {
  746. if (is_little_endian()) {
  747. IEEEd2bits_L u;
  748. u.d = d;
  749. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  750. }
  751. else {
  752. IEEEd2bits_B u;
  753. u.d = d;
  754. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  755. }
  756. }
  757. static double freebsd_fabs(double x)
  758. {
  759. u_int32_t high;
  760. GET_HIGH_WORD(high,x);
  761. SET_HIGH_WORD(x,high&0x7fffffff);
  762. return x;
  763. }
  764. static const float huge_f = 1.0e30F;
  765. static const float
  766. TWO23[2]={
  767. 8.3886080000e+06, /* 0x4b000000 */
  768. -8.3886080000e+06, /* 0xcb000000 */
  769. };
  770. static float
  771. freebsd_truncf(float x)
  772. {
  773. int32_t i0,j0;
  774. u_int32_t i;
  775. GET_FLOAT_WORD(i0,x);
  776. j0 = ((i0>>23)&0xff)-0x7f;
  777. if(j0<23) {
  778. if(j0<0) { /* raise inexact if x != 0 */
  779. if(huge_f+x>0.0F) /* |x|<1, so return 0*sign(x) */
  780. i0 &= 0x80000000;
  781. } else {
  782. i = (0x007fffff)>>j0;
  783. if((i0&i)==0) return x; /* x is integral */
  784. if(huge_f+x>0.0F) /* raise inexact flag */
  785. i0 &= (~i);
  786. }
  787. } else {
  788. if(j0==0x80) return x+x; /* inf or NaN */
  789. else return x; /* x is integral */
  790. }
  791. SET_FLOAT_WORD(x,i0);
  792. return x;
  793. }
  794. static float
  795. freebsd_rintf(float x)
  796. {
  797. int32_t i0,j0,sx;
  798. float w,t;
  799. GET_FLOAT_WORD(i0,x);
  800. sx = (i0>>31)&1;
  801. j0 = ((i0>>23)&0xff)-0x7f;
  802. if(j0<23) {
  803. if(j0<0) {
  804. if((i0&0x7fffffff)==0) return x;
  805. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  806. t = w-TWO23[sx];
  807. GET_FLOAT_WORD(i0,t);
  808. SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
  809. return t;
  810. }
  811. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  812. return w-TWO23[sx];
  813. }
  814. if(j0==0x80) return x+x; /* inf or NaN */
  815. else return x; /* x is integral */
  816. }
  817. static float
  818. freebsd_ceilf(float x)
  819. {
  820. int32_t i0,j0;
  821. u_int32_t i;
  822. GET_FLOAT_WORD(i0,x);
  823. j0 = ((i0>>23)&0xff)-0x7f;
  824. if(j0<23) {
  825. if(j0<0) { /* raise inexact if x != 0 */
  826. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  827. if(i0<0) {i0=0x80000000;}
  828. else if(i0!=0) { i0=0x3f800000;}
  829. }
  830. } else {
  831. i = (0x007fffff)>>j0;
  832. if((i0&i)==0) return x; /* x is integral */
  833. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  834. if(i0>0) i0 += (0x00800000)>>j0;
  835. i0 &= (~i);
  836. }
  837. }
  838. } else {
  839. if(j0==0x80) return x+x; /* inf or NaN */
  840. else return x; /* x is integral */
  841. }
  842. SET_FLOAT_WORD(x,i0);
  843. return x;
  844. }
  845. static float
  846. freebsd_floorf(float x)
  847. {
  848. int32_t i0,j0;
  849. u_int32_t i;
  850. GET_FLOAT_WORD(i0,x);
  851. j0 = ((i0>>23)&0xff)-0x7f;
  852. if(j0<23) {
  853. if(j0<0) { /* raise inexact if x != 0 */
  854. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  855. if(i0>=0) {i0=0;}
  856. else if((i0&0x7fffffff)!=0)
  857. { i0=0xbf800000;}
  858. }
  859. } else {
  860. i = (0x007fffff)>>j0;
  861. if((i0&i)==0) return x; /* x is integral */
  862. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  863. if(i0<0) i0 += (0x00800000)>>j0;
  864. i0 &= (~i);
  865. }
  866. }
  867. } else {
  868. if(j0==0x80) return x+x; /* inf or NaN */
  869. else return x; /* x is integral */
  870. }
  871. SET_FLOAT_WORD(x,i0);
  872. return x;
  873. }
  874. static float
  875. freebsd_fminf(float x, float y)
  876. {
  877. if (is_little_endian()) {
  878. IEEEf2bits_L u[2];
  879. u[0].f = x;
  880. u[1].f = y;
  881. /* Check for NaNs to avoid raising spurious exceptions. */
  882. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  883. return (y);
  884. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  885. return (x);
  886. /* Handle comparisons of signed zeroes. */
  887. if (u[0].bits.sign != u[1].bits.sign)
  888. return (u[u[1].bits.sign].f);
  889. }
  890. else {
  891. IEEEf2bits_B u[2];
  892. u[0].f = x;
  893. u[1].f = y;
  894. /* Check for NaNs to avoid raising spurious exceptions. */
  895. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  896. return (y);
  897. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  898. return (x);
  899. /* Handle comparisons of signed zeroes. */
  900. if (u[0].bits.sign != u[1].bits.sign)
  901. return (u[u[1].bits.sign].f);
  902. }
  903. return (x < y ? x : y);
  904. }
  905. static float
  906. freebsd_fmaxf(float x, float y)
  907. {
  908. if (is_little_endian()) {
  909. IEEEf2bits_L u[2];
  910. u[0].f = x;
  911. u[1].f = y;
  912. /* Check for NaNs to avoid raising spurious exceptions. */
  913. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  914. return (y);
  915. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  916. return (x);
  917. /* Handle comparisons of signed zeroes. */
  918. if (u[0].bits.sign != u[1].bits.sign)
  919. return (u[u[0].bits.sign].f);
  920. }
  921. else {
  922. IEEEf2bits_B u[2];
  923. u[0].f = x;
  924. u[1].f = y;
  925. /* Check for NaNs to avoid raising spurious exceptions. */
  926. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  927. return (y);
  928. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  929. return (x);
  930. /* Handle comparisons of signed zeroes. */
  931. if (u[0].bits.sign != u[1].bits.sign)
  932. return (u[u[0].bits.sign].f);
  933. }
  934. return (x > y ? x : y);
  935. }
  936. static double
  937. freebsd_copysign(double x, double y)
  938. {
  939. u_int32_t hx,hy;
  940. GET_HIGH_WORD(hx,x);
  941. GET_HIGH_WORD(hy,y);
  942. SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000));
  943. return x;
  944. }
  945. static double
  946. freebsd_scalbn(double x, int n)
  947. {
  948. int32_t k,hx,lx;
  949. EXTRACT_WORDS(hx,lx,x);
  950. k = (hx&0x7ff00000)>>20; /* extract exponent */
  951. if (k==0) { /* 0 or subnormal x */
  952. if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
  953. x *= two54;
  954. GET_HIGH_WORD(hx,x);
  955. k = ((hx&0x7ff00000)>>20) - 54;
  956. if (n< -50000) return tiny*x; /*underflow*/
  957. }
  958. if (k==0x7ff) return x+x; /* NaN or Inf */
  959. k = k+n;
  960. if (k > 0x7fe) return huge*freebsd_copysign(huge,x); /* overflow */
  961. if (k > 0) /* normal result */
  962. {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
  963. if (k <= -54) {
  964. if (n > 50000) /* in case integer overflow in n+k */
  965. return huge*freebsd_copysign(huge,x); /*overflow*/
  966. else return tiny*freebsd_copysign(tiny,x); /*underflow*/
  967. }
  968. k += 54; /* subnormal result */
  969. SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
  970. return x*twom54;
  971. }
  972. static double
  973. freebsd_pow(double x, double y)
  974. {
  975. double z,ax,z_h,z_l,p_h,p_l;
  976. double y1,t1,t2,r,s,t,u,v,w;
  977. int32_t i,j,k,yisint,n;
  978. int32_t hx,hy,ix,iy;
  979. u_int32_t lx,ly;
  980. EXTRACT_WORDS(hx,lx,x);
  981. EXTRACT_WORDS(hy,ly,y);
  982. ix = hx&0x7fffffff; iy = hy&0x7fffffff;
  983. /* y==zero: x**0 = 1 */
  984. if((iy|ly)==0) return one;
  985. /* x==1: 1**y = 1, even if y is NaN */
  986. if (hx==0x3ff00000 && lx == 0) return one;
  987. /* y!=zero: result is NaN if either arg is NaN */
  988. if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
  989. iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
  990. return (x+0.0)+(y+0.0);
  991. /* determine if y is an odd int when x < 0
  992. * yisint = 0 ... y is not an integer
  993. * yisint = 1 ... y is an odd int
  994. * yisint = 2 ... y is an even int
  995. */
  996. yisint = 0;
  997. if(hx<0) {
  998. if(iy>=0x43400000) yisint = 2; /* even integer y */
  999. else if(iy>=0x3ff00000) {
  1000. k = (iy>>20)-0x3ff; /* exponent */
  1001. if(k>20) {
  1002. j = ly>>(52-k);
  1003. if((j<<(52-k))==ly) yisint = 2-(j&1);
  1004. } else if(ly==0) {
  1005. j = iy>>(20-k);
  1006. if((j<<(20-k))==iy) yisint = 2-(j&1);
  1007. }
  1008. }
  1009. }
  1010. /* special value of y */
  1011. if(ly==0) {
  1012. if (iy==0x7ff00000) { /* y is +-inf */
  1013. if(((ix-0x3ff00000)|lx)==0)
  1014. return one; /* (-1)**+-inf is NaN */
  1015. else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
  1016. return (hy>=0)? y: zero;
  1017. else /* (|x|<1)**-,+inf = inf,0 */
  1018. return (hy<0)?-y: zero;
  1019. }
  1020. if(iy==0x3ff00000) { /* y is +-1 */
  1021. if(hy<0) return one/x; else return x;
  1022. }
  1023. if(hy==0x40000000) return x*x; /* y is 2 */
  1024. if(hy==0x40080000) return x*x*x; /* y is 3 */
  1025. if(hy==0x40100000) { /* y is 4 */
  1026. u = x*x;
  1027. return u*u;
  1028. }
  1029. if(hy==0x3fe00000) { /* y is 0.5 */
  1030. if(hx>=0) /* x >= +0 */
  1031. return sqrt(x);
  1032. }
  1033. }
  1034. ax = fabs(x);
  1035. /* special value of x */
  1036. if(lx==0) {
  1037. if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
  1038. z = ax; /*x is +-0,+-inf,+-1*/
  1039. if(hy<0) z = one/z; /* z = (1/|x|) */
  1040. if(hx<0) {
  1041. if(((ix-0x3ff00000)|yisint)==0) {
  1042. z = (z-z)/(z-z); /* (-1)**non-int is NaN */
  1043. } else if(yisint==1)
  1044. z = -z; /* (x<0)**odd = -(|x|**odd) */
  1045. }
  1046. return z;
  1047. }
  1048. }
  1049. /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
  1050. n = (hx>>31)+1;
  1051. but ANSI C says a right shift of a signed negative quantity is
  1052. implementation defined. */
  1053. n = ((u_int32_t)hx>>31)-1;
  1054. /* (x<0)**(non-int) is NaN */
  1055. if((n|yisint)==0) return (x-x)/(x-x);
  1056. s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
  1057. if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */
  1058. /* |y| is huge */
  1059. if(iy>0x41e00000) { /* if |y| > 2**31 */
  1060. if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */
  1061. if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;
  1062. if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;
  1063. }
  1064. /* over/underflow if x is not close to one */
  1065. if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;
  1066. if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;
  1067. /* now |1-x| is tiny <= 2**-20, suffice to compute
  1068. log(x) by x-x^2/2+x^3/3-x^4/4 */
  1069. t = ax-one; /* t has 20 trailing zeros */
  1070. w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
  1071. u = ivln2_h*t; /* ivln2_h has 21 sig. bits */
  1072. v = t*ivln2_l-w*ivln2;
  1073. t1 = u+v;
  1074. SET_LOW_WORD(t1,0);
  1075. t2 = v-(t1-u);
  1076. } else {
  1077. double ss,s2,s_h,s_l,t_h,t_l;
  1078. n = 0;
  1079. /* take care subnormal number */
  1080. if(ix<0x00100000)
  1081. {ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); }
  1082. n += ((ix)>>20)-0x3ff;
  1083. j = ix&0x000fffff;
  1084. /* determine interval */
  1085. ix = j|0x3ff00000; /* normalize ix */
  1086. if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */
  1087. else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */
  1088. else {k=0;n+=1;ix -= 0x00100000;}
  1089. SET_HIGH_WORD(ax,ix);
  1090. /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  1091. u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  1092. v = one/(ax+bp[k]);
  1093. ss = u*v;
  1094. s_h = ss;
  1095. SET_LOW_WORD(s_h,0);
  1096. /* t_h=ax+bp[k] High */
  1097. t_h = zero;
  1098. SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18));
  1099. t_l = ax - (t_h-bp[k]);
  1100. s_l = v*((u-s_h*t_h)-s_h*t_l);
  1101. /* compute log(ax) */
  1102. s2 = ss*ss;
  1103. r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
  1104. r += s_l*(s_h+ss);
  1105. s2 = s_h*s_h;
  1106. t_h = 3.0+s2+r;
  1107. SET_LOW_WORD(t_h,0);
  1108. t_l = r-((t_h-3.0)-s2);
  1109. /* u+v = ss*(1+...) */
  1110. u = s_h*t_h;
  1111. v = s_l*t_h+t_l*ss;
  1112. /* 2/(3log2)*(ss+...) */
  1113. p_h = u+v;
  1114. SET_LOW_WORD(p_h,0);
  1115. p_l = v-(p_h-u);
  1116. z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
  1117. z_l = cp_l*p_h+p_l*cp+dp_l[k];
  1118. /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  1119. t = (double)n;
  1120. t1 = (((z_h+z_l)+dp_h[k])+t);
  1121. SET_LOW_WORD(t1,0);
  1122. t2 = z_l-(((t1-t)-dp_h[k])-z_h);
  1123. }
  1124. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  1125. y1 = y;
  1126. SET_LOW_WORD(y1,0);
  1127. p_l = (y-y1)*t1+y*t2;
  1128. p_h = y1*t1;
  1129. z = p_l+p_h;
  1130. EXTRACT_WORDS(j,i,z);
  1131. if (j>=0x40900000) { /* z >= 1024 */
  1132. if(((j-0x40900000)|i)!=0) /* if z > 1024 */
  1133. return s*huge*huge; /* overflow */
  1134. else {
  1135. if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */
  1136. }
  1137. } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */
  1138. if(((j-0xc090cc00)|i)!=0) /* z < -1075 */
  1139. return s*tiny*tiny; /* underflow */
  1140. else {
  1141. if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */
  1142. }
  1143. }
  1144. /*
  1145. * compute 2**(p_h+p_l)
  1146. */
  1147. i = j&0x7fffffff;
  1148. k = (i>>20)-0x3ff;
  1149. n = 0;
  1150. if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
  1151. n = j+(0x00100000>>(k+1));
  1152. k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */
  1153. t = zero;
  1154. SET_HIGH_WORD(t,n&~(0x000fffff>>k));
  1155. n = ((n&0x000fffff)|0x00100000)>>(20-k);
  1156. if(j<0) n = -n;
  1157. p_h -= t;
  1158. }
  1159. t = p_l+p_h;
  1160. SET_LOW_WORD(t,0);
  1161. u = t*lg2_h;
  1162. v = (p_l-(t-p_h))*lg2+t*lg2_l;
  1163. z = u+v;
  1164. w = v-(z-u);
  1165. t = z*z;
  1166. t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  1167. r = (z*t1)/(t1-two)-(w+z*w);
  1168. z = one-(r-z);
  1169. GET_HIGH_WORD(j,z);
  1170. j += (n<<20);
  1171. if((j>>20)<=0) z = freebsd_scalbn(z,n); /* subnormal output */
  1172. else SET_HIGH_WORD(z,j);
  1173. return s*z;
  1174. }
  1175. double atan(double x)
  1176. {
  1177. return freebsd_atan(x);
  1178. }
  1179. double atan2(double y, double x)
  1180. {
  1181. return freebsd_atan2(y, x);
  1182. }
  1183. double sqrt(double x)
  1184. {
  1185. return freebsd_sqrt(x);
  1186. }
  1187. double floor(double x)
  1188. {
  1189. return freebsd_floor(x);
  1190. }
  1191. double ceil(double x)
  1192. {
  1193. return freebsd_ceil(x);
  1194. }
  1195. double fmin(double x, double y)
  1196. {
  1197. return x < y ? x : y;
  1198. }
  1199. double fmax(double x, double y)
  1200. {
  1201. return x > y ? x : y;
  1202. }
  1203. double rint(double x)
  1204. {
  1205. return freebsd_rint(x);
  1206. }
  1207. double fabs(double x)
  1208. {
  1209. return freebsd_fabs(x);
  1210. }
  1211. int isnan(double x)
  1212. {
  1213. return freebsd_isnan(x);
  1214. }
  1215. double trunc(double x)
  1216. {
  1217. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  1218. }
  1219. int signbit(double x)
  1220. {
  1221. return ((__HI(x) & 0x80000000) >> 31);
  1222. }
  1223. float
  1224. truncf(float x)
  1225. {
  1226. return freebsd_truncf(x);
  1227. }
  1228. float
  1229. rintf(float x)
  1230. {
  1231. return freebsd_rintf(x);
  1232. }
  1233. float
  1234. ceilf(float x)
  1235. {
  1236. return freebsd_ceilf(x);
  1237. }
  1238. float
  1239. floorf(float x)
  1240. {
  1241. return freebsd_floorf(x);
  1242. }
  1243. float
  1244. fminf(float x, float y)
  1245. {
  1246. return freebsd_fminf(x, y);
  1247. }
  1248. float
  1249. fmaxf(float x, float y)
  1250. {
  1251. return freebsd_fmaxf(x, y);
  1252. }
  1253. double
  1254. pow(double x, double y)
  1255. {
  1256. return freebsd_pow(x, y);
  1257. }
  1258. double
  1259. scalbn(double x, int n)
  1260. {
  1261. return freebsd_scalbn(x, n);
  1262. }