math.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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: return -pi-tiny;/* atan(-0,-anything) =-pi */
  478. }
  479. }
  480. /* when x = 0 */
  481. if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  482. /* when x is INF */
  483. if(ix==0x7ff00000) {
  484. if(iy==0x7ff00000) {
  485. switch(m) {
  486. case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
  487. case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
  488. case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
  489. case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
  490. }
  491. } else {
  492. switch(m) {
  493. case 0: return zero ; /* atan(+...,+INF) */
  494. case 1: return -zero ; /* atan(-...,+INF) */
  495. case 2: return pi+tiny ; /* atan(+...,-INF) */
  496. case 3: return -pi-tiny ; /* atan(-...,-INF) */
  497. }
  498. }
  499. }
  500. /* when y is INF */
  501. if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  502. /* compute y/x */
  503. k = (iy-ix)>>20;
  504. if(k > 60) { /* |y/x| > 2**60 */
  505. z=pi_o_2+0.5*pi_lo;
  506. m&=1;
  507. }
  508. else if(hx<0&&k<-60) z=0.0; /* 0 > |y|/x > -2**-60 */
  509. else z=freebsd_atan(fabs(y/x)); /* safe to do y/x */
  510. switch (m) {
  511. case 0: return z ; /* atan(+,+) */
  512. case 1: return -z ; /* atan(-,+) */
  513. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  514. default: /* case 3 */
  515. return (z-pi_lo)-pi;/* atan(-,-) */
  516. }
  517. }
  518. static double freebsd_sqrt(double x) /* wrapper sqrt */
  519. {
  520. double z;
  521. int32_t sign = (int)0x80000000;
  522. int32_t ix0,s0,q,m,t,i;
  523. u_int32_t r,t1,s1,ix1,q1;
  524. EXTRACT_WORDS(ix0,ix1,x);
  525. /* take care of Inf and NaN */
  526. if((ix0&0x7ff00000)==0x7ff00000) {
  527. return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  528. sqrt(-inf)=sNaN */
  529. }
  530. /* take care of zero */
  531. if(ix0<=0) {
  532. if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
  533. else if(ix0<0)
  534. return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
  535. }
  536. /* normalize x */
  537. m = (ix0>>20);
  538. if(m==0) { /* subnormal x */
  539. while(ix0==0) {
  540. m -= 21;
  541. ix0 |= (ix1>>11); ix1 <<= 21;
  542. }
  543. for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
  544. m -= i-1;
  545. ix0 |= (ix1>>(32-i));
  546. ix1 <<= i;
  547. }
  548. m -= 1023; /* unbias exponent */
  549. ix0 = (ix0&0x000fffff)|0x00100000;
  550. if(m&1){ /* odd m, double x to make it even */
  551. ix0 += ix0 + ((ix1&sign)>>31);
  552. ix1 += ix1;
  553. }
  554. m >>= 1; /* m = [m/2] */
  555. /* generate sqrt(x) bit by bit */
  556. ix0 += ix0 + ((ix1&sign)>>31);
  557. ix1 += ix1;
  558. q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
  559. r = 0x00200000; /* r = moving bit from right to left */
  560. while(r!=0) {
  561. t = s0+r;
  562. if(t<=ix0) {
  563. s0 = t+r;
  564. ix0 -= t;
  565. q += r;
  566. }
  567. ix0 += ix0 + ((ix1&sign)>>31);
  568. ix1 += ix1;
  569. r>>=1;
  570. }
  571. r = sign;
  572. while(r!=0) {
  573. t1 = s1+r;
  574. t = s0;
  575. if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
  576. s1 = t1+r;
  577. if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
  578. ix0 -= t;
  579. if (ix1 < t1) ix0 -= 1;
  580. ix1 -= t1;
  581. q1 += r;
  582. }
  583. ix0 += ix0 + ((ix1&sign)>>31);
  584. ix1 += ix1;
  585. r>>=1;
  586. }
  587. /* use floating add to find out rounding direction */
  588. if((ix0|ix1)!=0) {
  589. z = one-tiny; /* trigger inexact flag */
  590. if (z>=one) {
  591. z = one+tiny;
  592. if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;}
  593. else if (z>one) {
  594. if (q1==(u_int32_t)0xfffffffe) q+=1;
  595. q1+=2;
  596. } else
  597. q1 += (q1&1);
  598. }
  599. }
  600. ix0 = (q>>1)+0x3fe00000;
  601. ix1 = q1>>1;
  602. if ((q&1)==1) ix1 |= sign;
  603. ix0 += (m <<20);
  604. INSERT_WORDS(z,ix0,ix1);
  605. return z;
  606. }
  607. static double freebsd_floor(double x)
  608. {
  609. int32_t i0,i1,j0;
  610. u_int32_t i,j;
  611. EXTRACT_WORDS(i0,i1,x);
  612. j0 = ((i0>>20)&0x7ff)-0x3ff;
  613. if(j0<20) {
  614. if(j0<0) { /* raise inexact if x != 0 */
  615. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  616. if(i0>=0) {i0=i1=0;}
  617. else if(((i0&0x7fffffff)|i1)!=0)
  618. { i0=0xbff00000;i1=0;}
  619. }
  620. } else {
  621. i = (0x000fffff)>>j0;
  622. if(((i0&i)|i1)==0) return x; /* x is integral */
  623. if(huge+x>0.0) { /* raise inexact flag */
  624. if(i0<0) i0 += (0x00100000)>>j0;
  625. i0 &= (~i); i1=0;
  626. }
  627. }
  628. } else if (j0>51) {
  629. if(j0==0x400) return x+x; /* inf or NaN */
  630. else return x; /* x is integral */
  631. } else {
  632. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  633. if((i1&i)==0) return x; /* x is integral */
  634. if(huge+x>0.0) { /* raise inexact flag */
  635. if(i0<0) {
  636. if(j0==20) i0+=1;
  637. else {
  638. j = i1+(1<<(52-j0));
  639. if(j<i1) i0 +=1 ; /* got a carry */
  640. i1=j;
  641. }
  642. }
  643. i1 &= (~i);
  644. }
  645. }
  646. INSERT_WORDS(x,i0,i1);
  647. return x;
  648. }
  649. static double freebsd_ceil(double x)
  650. {
  651. int32_t i0,i1,j0;
  652. u_int32_t i,j;
  653. EXTRACT_WORDS(i0,i1,x);
  654. j0 = ((i0>>20)&0x7ff)-0x3ff;
  655. if(j0<20) {
  656. if(j0<0) { /* raise inexact if x != 0 */
  657. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  658. if(i0<0) {i0=0x80000000;i1=0;}
  659. else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  660. }
  661. } else {
  662. i = (0x000fffff)>>j0;
  663. if(((i0&i)|i1)==0) return x; /* x is integral */
  664. if(huge+x>0.0) { /* raise inexact flag */
  665. if(i0>0) i0 += (0x00100000)>>j0;
  666. i0 &= (~i); i1=0;
  667. }
  668. }
  669. } else if (j0>51) {
  670. if(j0==0x400) return x+x; /* inf or NaN */
  671. else return x; /* x is integral */
  672. } else {
  673. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  674. if((i1&i)==0) return x; /* x is integral */
  675. if(huge+x>0.0) { /* raise inexact flag */
  676. if(i0>0) {
  677. if(j0==20) i0+=1;
  678. else {
  679. j = i1 + (1<<(52-j0));
  680. if(j<i1) i0+=1; /* got a carry */
  681. i1 = j;
  682. }
  683. }
  684. i1 &= (~i);
  685. }
  686. }
  687. INSERT_WORDS(x,i0,i1);
  688. return x;
  689. }
  690. static double freebsd_rint(double x)
  691. {
  692. int32_t i0,j0,sx;
  693. u_int32_t i,i1;
  694. double w,t;
  695. EXTRACT_WORDS(i0,i1,x);
  696. sx = (i0>>31)&1;
  697. j0 = ((i0>>20)&0x7ff)-0x3ff;
  698. if(j0<20) {
  699. if(j0<0) {
  700. if(((i0&0x7fffffff)|i1)==0) return x;
  701. i1 |= (i0&0x0fffff);
  702. i0 &= 0xfffe0000;
  703. i0 |= ((i1|-i1)>>12)&0x80000;
  704. SET_HIGH_WORD(x,i0);
  705. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  706. t = w-TWO52[sx];
  707. GET_HIGH_WORD(i0,t);
  708. SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31));
  709. return t;
  710. } else {
  711. i = (0x000fffff)>>j0;
  712. if(((i0&i)|i1)==0) return x; /* x is integral */
  713. i>>=1;
  714. if(((i0&i)|i1)!=0) {
  715. /*
  716. * Some bit is set after the 0.5 bit. To avoid the
  717. * possibility of errors from double rounding in
  718. * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
  719. * guard bit. We do this for all j0<=51. The
  720. * adjustment is trickiest for j0==18 and j0==19
  721. * since then it spans the word boundary.
  722. */
  723. if(j0==19) i1 = 0x40000000; else
  724. if(j0==18) i1 = 0x80000000; else
  725. i0 = (i0&(~i))|((0x20000)>>j0);
  726. }
  727. }
  728. } else if (j0>51) {
  729. if(j0==0x400) return x+x; /* inf or NaN */
  730. else return x; /* x is integral */
  731. } else {
  732. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  733. if((i1&i)==0) return x; /* x is integral */
  734. i>>=1;
  735. if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20));
  736. }
  737. INSERT_WORDS(x,i0,i1);
  738. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  739. return w-TWO52[sx];
  740. }
  741. static int freebsd_isnan(double d)
  742. {
  743. if (is_little_endian()) {
  744. IEEEd2bits_L u;
  745. u.d = d;
  746. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  747. }
  748. else {
  749. IEEEd2bits_B u;
  750. u.d = d;
  751. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  752. }
  753. }
  754. static double freebsd_fabs(double x)
  755. {
  756. u_int32_t high;
  757. GET_HIGH_WORD(high,x);
  758. SET_HIGH_WORD(x,high&0x7fffffff);
  759. return x;
  760. }
  761. static const float huge_f = 1.0e30F;
  762. static const float
  763. TWO23[2]={
  764. 8.3886080000e+06, /* 0x4b000000 */
  765. -8.3886080000e+06, /* 0xcb000000 */
  766. };
  767. static float
  768. freebsd_truncf(float x)
  769. {
  770. int32_t i0,j0;
  771. u_int32_t i;
  772. GET_FLOAT_WORD(i0,x);
  773. j0 = ((i0>>23)&0xff)-0x7f;
  774. if(j0<23) {
  775. if(j0<0) { /* raise inexact if x != 0 */
  776. if(huge_f+x>0.0F) /* |x|<1, so return 0*sign(x) */
  777. i0 &= 0x80000000;
  778. } else {
  779. i = (0x007fffff)>>j0;
  780. if((i0&i)==0) return x; /* x is integral */
  781. if(huge_f+x>0.0F) /* raise inexact flag */
  782. i0 &= (~i);
  783. }
  784. } else {
  785. if(j0==0x80) return x+x; /* inf or NaN */
  786. else return x; /* x is integral */
  787. }
  788. SET_FLOAT_WORD(x,i0);
  789. return x;
  790. }
  791. static float
  792. freebsd_rintf(float x)
  793. {
  794. int32_t i0,j0,sx;
  795. float w,t;
  796. GET_FLOAT_WORD(i0,x);
  797. sx = (i0>>31)&1;
  798. j0 = ((i0>>23)&0xff)-0x7f;
  799. if(j0<23) {
  800. if(j0<0) {
  801. if((i0&0x7fffffff)==0) return x;
  802. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  803. t = w-TWO23[sx];
  804. GET_FLOAT_WORD(i0,t);
  805. SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
  806. return t;
  807. }
  808. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  809. return w-TWO23[sx];
  810. }
  811. if(j0==0x80) return x+x; /* inf or NaN */
  812. else return x; /* x is integral */
  813. }
  814. static float
  815. freebsd_ceilf(float x)
  816. {
  817. int32_t i0,j0;
  818. u_int32_t i;
  819. GET_FLOAT_WORD(i0,x);
  820. j0 = ((i0>>23)&0xff)-0x7f;
  821. if(j0<23) {
  822. if(j0<0) { /* raise inexact if x != 0 */
  823. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  824. if(i0<0) {i0=0x80000000;}
  825. else if(i0!=0) { i0=0x3f800000;}
  826. }
  827. } else {
  828. i = (0x007fffff)>>j0;
  829. if((i0&i)==0) return x; /* x is integral */
  830. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  831. if(i0>0) i0 += (0x00800000)>>j0;
  832. i0 &= (~i);
  833. }
  834. }
  835. } else {
  836. if(j0==0x80) return x+x; /* inf or NaN */
  837. else return x; /* x is integral */
  838. }
  839. SET_FLOAT_WORD(x,i0);
  840. return x;
  841. }
  842. static float
  843. freebsd_floorf(float x)
  844. {
  845. int32_t i0,j0;
  846. u_int32_t i;
  847. GET_FLOAT_WORD(i0,x);
  848. j0 = ((i0>>23)&0xff)-0x7f;
  849. if(j0<23) {
  850. if(j0<0) { /* raise inexact if x != 0 */
  851. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  852. if(i0>=0) {i0=0;}
  853. else if((i0&0x7fffffff)!=0)
  854. { i0=0xbf800000;}
  855. }
  856. } else {
  857. i = (0x007fffff)>>j0;
  858. if((i0&i)==0) return x; /* x is integral */
  859. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  860. if(i0<0) i0 += (0x00800000)>>j0;
  861. i0 &= (~i);
  862. }
  863. }
  864. } else {
  865. if(j0==0x80) return x+x; /* inf or NaN */
  866. else return x; /* x is integral */
  867. }
  868. SET_FLOAT_WORD(x,i0);
  869. return x;
  870. }
  871. static float
  872. freebsd_fminf(float x, float y)
  873. {
  874. if (is_little_endian()) {
  875. IEEEf2bits_L u[2];
  876. u[0].f = x;
  877. u[1].f = y;
  878. /* Check for NaNs to avoid raising spurious exceptions. */
  879. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  880. return (y);
  881. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  882. return (x);
  883. /* Handle comparisons of signed zeroes. */
  884. if (u[0].bits.sign != u[1].bits.sign)
  885. return (u[u[1].bits.sign].f);
  886. }
  887. else {
  888. IEEEf2bits_B u[2];
  889. u[0].f = x;
  890. u[1].f = y;
  891. /* Check for NaNs to avoid raising spurious exceptions. */
  892. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  893. return (y);
  894. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  895. return (x);
  896. /* Handle comparisons of signed zeroes. */
  897. if (u[0].bits.sign != u[1].bits.sign)
  898. return (u[u[1].bits.sign].f);
  899. }
  900. return (x < y ? x : y);
  901. }
  902. static float
  903. freebsd_fmaxf(float x, float y)
  904. {
  905. if (is_little_endian()) {
  906. IEEEf2bits_L u[2];
  907. u[0].f = x;
  908. u[1].f = y;
  909. /* Check for NaNs to avoid raising spurious exceptions. */
  910. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  911. return (y);
  912. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  913. return (x);
  914. /* Handle comparisons of signed zeroes. */
  915. if (u[0].bits.sign != u[1].bits.sign)
  916. return (u[u[0].bits.sign].f);
  917. }
  918. else {
  919. IEEEf2bits_B u[2];
  920. u[0].f = x;
  921. u[1].f = y;
  922. /* Check for NaNs to avoid raising spurious exceptions. */
  923. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  924. return (y);
  925. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  926. return (x);
  927. /* Handle comparisons of signed zeroes. */
  928. if (u[0].bits.sign != u[1].bits.sign)
  929. return (u[u[0].bits.sign].f);
  930. }
  931. return (x > y ? x : y);
  932. }
  933. static double
  934. freebsd_copysign(double x, double y)
  935. {
  936. u_int32_t hx,hy;
  937. GET_HIGH_WORD(hx,x);
  938. GET_HIGH_WORD(hy,y);
  939. SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000));
  940. return x;
  941. }
  942. static double
  943. freebsd_scalbn(double x, int n)
  944. {
  945. int32_t k,hx,lx;
  946. EXTRACT_WORDS(hx,lx,x);
  947. k = (hx&0x7ff00000)>>20; /* extract exponent */
  948. if (k==0) { /* 0 or subnormal x */
  949. if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
  950. x *= two54;
  951. GET_HIGH_WORD(hx,x);
  952. k = ((hx&0x7ff00000)>>20) - 54;
  953. if (n< -50000) return tiny*x; /*underflow*/
  954. }
  955. if (k==0x7ff) return x+x; /* NaN or Inf */
  956. k = k+n;
  957. if (k > 0x7fe) return huge*freebsd_copysign(huge,x); /* overflow */
  958. if (k > 0) /* normal result */
  959. {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
  960. if (k <= -54) {
  961. if (n > 50000) /* in case integer overflow in n+k */
  962. return huge*freebsd_copysign(huge,x); /*overflow*/
  963. else return tiny*freebsd_copysign(tiny,x); /*underflow*/
  964. }
  965. k += 54; /* subnormal result */
  966. SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
  967. return x*twom54;
  968. }
  969. static double
  970. freebsd_pow(double x, double y)
  971. {
  972. double z,ax,z_h,z_l,p_h,p_l;
  973. double y1,t1,t2,r,s,t,u,v,w;
  974. int32_t i,j,k,yisint,n;
  975. int32_t hx,hy,ix,iy;
  976. u_int32_t lx,ly;
  977. EXTRACT_WORDS(hx,lx,x);
  978. EXTRACT_WORDS(hy,ly,y);
  979. ix = hx&0x7fffffff; iy = hy&0x7fffffff;
  980. /* y==zero: x**0 = 1 */
  981. if((iy|ly)==0) return one;
  982. /* x==1: 1**y = 1, even if y is NaN */
  983. if (hx==0x3ff00000 && lx == 0) return one;
  984. /* y!=zero: result is NaN if either arg is NaN */
  985. if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
  986. iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
  987. return (x+0.0)+(y+0.0);
  988. /* determine if y is an odd int when x < 0
  989. * yisint = 0 ... y is not an integer
  990. * yisint = 1 ... y is an odd int
  991. * yisint = 2 ... y is an even int
  992. */
  993. yisint = 0;
  994. if(hx<0) {
  995. if(iy>=0x43400000) yisint = 2; /* even integer y */
  996. else if(iy>=0x3ff00000) {
  997. k = (iy>>20)-0x3ff; /* exponent */
  998. if(k>20) {
  999. j = ly>>(52-k);
  1000. if((j<<(52-k))==ly) yisint = 2-(j&1);
  1001. } else if(ly==0) {
  1002. j = iy>>(20-k);
  1003. if((j<<(20-k))==iy) yisint = 2-(j&1);
  1004. }
  1005. }
  1006. }
  1007. /* special value of y */
  1008. if(ly==0) {
  1009. if (iy==0x7ff00000) { /* y is +-inf */
  1010. if(((ix-0x3ff00000)|lx)==0)
  1011. return one; /* (-1)**+-inf is NaN */
  1012. else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
  1013. return (hy>=0)? y: zero;
  1014. else /* (|x|<1)**-,+inf = inf,0 */
  1015. return (hy<0)?-y: zero;
  1016. }
  1017. if(iy==0x3ff00000) { /* y is +-1 */
  1018. if(hy<0) return one/x; else return x;
  1019. }
  1020. if(hy==0x40000000) return x*x; /* y is 2 */
  1021. if(hy==0x40080000) return x*x*x; /* y is 3 */
  1022. if(hy==0x40100000) { /* y is 4 */
  1023. u = x*x;
  1024. return u*u;
  1025. }
  1026. if(hy==0x3fe00000) { /* y is 0.5 */
  1027. if(hx>=0) /* x >= +0 */
  1028. return sqrt(x);
  1029. }
  1030. }
  1031. ax = fabs(x);
  1032. /* special value of x */
  1033. if(lx==0) {
  1034. if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
  1035. z = ax; /*x is +-0,+-inf,+-1*/
  1036. if(hy<0) z = one/z; /* z = (1/|x|) */
  1037. if(hx<0) {
  1038. if(((ix-0x3ff00000)|yisint)==0) {
  1039. z = (z-z)/(z-z); /* (-1)**non-int is NaN */
  1040. } else if(yisint==1)
  1041. z = -z; /* (x<0)**odd = -(|x|**odd) */
  1042. }
  1043. return z;
  1044. }
  1045. }
  1046. /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
  1047. n = (hx>>31)+1;
  1048. but ANSI C says a right shift of a signed negative quantity is
  1049. implementation defined. */
  1050. n = ((u_int32_t)hx>>31)-1;
  1051. /* (x<0)**(non-int) is NaN */
  1052. if((n|yisint)==0) return (x-x)/(x-x);
  1053. s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
  1054. if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */
  1055. /* |y| is huge */
  1056. if(iy>0x41e00000) { /* if |y| > 2**31 */
  1057. if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */
  1058. if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;
  1059. if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;
  1060. }
  1061. /* over/underflow if x is not close to one */
  1062. if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;
  1063. if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;
  1064. /* now |1-x| is tiny <= 2**-20, suffice to compute
  1065. log(x) by x-x^2/2+x^3/3-x^4/4 */
  1066. t = ax-one; /* t has 20 trailing zeros */
  1067. w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
  1068. u = ivln2_h*t; /* ivln2_h has 21 sig. bits */
  1069. v = t*ivln2_l-w*ivln2;
  1070. t1 = u+v;
  1071. SET_LOW_WORD(t1,0);
  1072. t2 = v-(t1-u);
  1073. } else {
  1074. double ss,s2,s_h,s_l,t_h,t_l;
  1075. n = 0;
  1076. /* take care subnormal number */
  1077. if(ix<0x00100000)
  1078. {ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); }
  1079. n += ((ix)>>20)-0x3ff;
  1080. j = ix&0x000fffff;
  1081. /* determine interval */
  1082. ix = j|0x3ff00000; /* normalize ix */
  1083. if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */
  1084. else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */
  1085. else {k=0;n+=1;ix -= 0x00100000;}
  1086. SET_HIGH_WORD(ax,ix);
  1087. /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  1088. u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  1089. v = one/(ax+bp[k]);
  1090. ss = u*v;
  1091. s_h = ss;
  1092. SET_LOW_WORD(s_h,0);
  1093. /* t_h=ax+bp[k] High */
  1094. t_h = zero;
  1095. SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18));
  1096. t_l = ax - (t_h-bp[k]);
  1097. s_l = v*((u-s_h*t_h)-s_h*t_l);
  1098. /* compute log(ax) */
  1099. s2 = ss*ss;
  1100. r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
  1101. r += s_l*(s_h+ss);
  1102. s2 = s_h*s_h;
  1103. t_h = 3.0+s2+r;
  1104. SET_LOW_WORD(t_h,0);
  1105. t_l = r-((t_h-3.0)-s2);
  1106. /* u+v = ss*(1+...) */
  1107. u = s_h*t_h;
  1108. v = s_l*t_h+t_l*ss;
  1109. /* 2/(3log2)*(ss+...) */
  1110. p_h = u+v;
  1111. SET_LOW_WORD(p_h,0);
  1112. p_l = v-(p_h-u);
  1113. z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
  1114. z_l = cp_l*p_h+p_l*cp+dp_l[k];
  1115. /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  1116. t = (double)n;
  1117. t1 = (((z_h+z_l)+dp_h[k])+t);
  1118. SET_LOW_WORD(t1,0);
  1119. t2 = z_l-(((t1-t)-dp_h[k])-z_h);
  1120. }
  1121. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  1122. y1 = y;
  1123. SET_LOW_WORD(y1,0);
  1124. p_l = (y-y1)*t1+y*t2;
  1125. p_h = y1*t1;
  1126. z = p_l+p_h;
  1127. EXTRACT_WORDS(j,i,z);
  1128. if (j>=0x40900000) { /* z >= 1024 */
  1129. if(((j-0x40900000)|i)!=0) /* if z > 1024 */
  1130. return s*huge*huge; /* overflow */
  1131. else {
  1132. if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */
  1133. }
  1134. } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */
  1135. if(((j-0xc090cc00)|i)!=0) /* z < -1075 */
  1136. return s*tiny*tiny; /* underflow */
  1137. else {
  1138. if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */
  1139. }
  1140. }
  1141. /*
  1142. * compute 2**(p_h+p_l)
  1143. */
  1144. i = j&0x7fffffff;
  1145. k = (i>>20)-0x3ff;
  1146. n = 0;
  1147. if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
  1148. n = j+(0x00100000>>(k+1));
  1149. k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */
  1150. t = zero;
  1151. SET_HIGH_WORD(t,n&~(0x000fffff>>k));
  1152. n = ((n&0x000fffff)|0x00100000)>>(20-k);
  1153. if(j<0) n = -n;
  1154. p_h -= t;
  1155. }
  1156. t = p_l+p_h;
  1157. SET_LOW_WORD(t,0);
  1158. u = t*lg2_h;
  1159. v = (p_l-(t-p_h))*lg2+t*lg2_l;
  1160. z = u+v;
  1161. w = v-(z-u);
  1162. t = z*z;
  1163. t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  1164. r = (z*t1)/(t1-two)-(w+z*w);
  1165. z = one-(r-z);
  1166. GET_HIGH_WORD(j,z);
  1167. j += (n<<20);
  1168. if((j>>20)<=0) z = freebsd_scalbn(z,n); /* subnormal output */
  1169. else SET_HIGH_WORD(z,j);
  1170. return s*z;
  1171. }
  1172. double atan(double x)
  1173. {
  1174. return freebsd_atan(x);
  1175. }
  1176. double atan2(double y, double x)
  1177. {
  1178. return freebsd_atan2(y, x);
  1179. }
  1180. double sqrt(double x)
  1181. {
  1182. return freebsd_sqrt(x);
  1183. }
  1184. double floor(double x)
  1185. {
  1186. return freebsd_floor(x);
  1187. }
  1188. double ceil(double x)
  1189. {
  1190. return freebsd_ceil(x);
  1191. }
  1192. double fmin(double x, double y)
  1193. {
  1194. return x < y ? x : y;
  1195. }
  1196. double fmax(double x, double y)
  1197. {
  1198. return x > y ? x : y;
  1199. }
  1200. double rint(double x)
  1201. {
  1202. return freebsd_rint(x);
  1203. }
  1204. double fabs(double x)
  1205. {
  1206. return freebsd_fabs(x);
  1207. }
  1208. int isnan(double x)
  1209. {
  1210. return freebsd_isnan(x);
  1211. }
  1212. double trunc(double x)
  1213. {
  1214. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  1215. }
  1216. int signbit(double x)
  1217. {
  1218. return ((__HI(x) & 0x80000000) >> 31);
  1219. }
  1220. float
  1221. truncf(float x)
  1222. {
  1223. return freebsd_truncf(x);
  1224. }
  1225. float
  1226. rintf(float x)
  1227. {
  1228. return freebsd_rintf(x);
  1229. }
  1230. float
  1231. ceilf(float x)
  1232. {
  1233. return freebsd_ceilf(x);
  1234. }
  1235. float
  1236. floorf(float x)
  1237. {
  1238. return freebsd_floorf(x);
  1239. }
  1240. float
  1241. fminf(float x, float y)
  1242. {
  1243. return freebsd_fminf(x, y);
  1244. }
  1245. float
  1246. fmaxf(float x, float y)
  1247. {
  1248. return freebsd_fmaxf(x, y);
  1249. }
  1250. double
  1251. pow(double x, double y)
  1252. {
  1253. return freebsd_pow(x, y);
  1254. }
  1255. double
  1256. scalbn(double x, int n)
  1257. {
  1258. return freebsd_scalbn(x, n);
  1259. }