rt_clib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: rt_clib.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2020-06-18
  7. 功能说明: 实时微系统标准C库接口。
  8. 其它说明:
  9. 修改记录:
  10. */
  11. /*------------------------------- 头文件 --------------------------------------
  12. */
  13. #include "rt_clib.h"
  14. #include "rt.h"
  15. /*------------------------------- 宏定义 --------------------------------------
  16. */
  17. /*------------------------------ 类型结构 -------------------------------------
  18. */
  19. /*------------------------------ 全局变量 -------------------------------------
  20. */
  21. /*------------------------------ 函数声明 -------------------------------------
  22. */
  23. /*------------------------------ 外部函数 -------------------------------------
  24. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  25. */
  26. // noted by sunxi: 20220415 好像没有用,屏蔽掉
  27. /*
  28. //64位除法
  29. static inline u64 div64_u64(u64 dividend, u64 divisor)
  30. {
  31. return dividend / divisor;
  32. }
  33. uint32_t long __udivdi3(uint32_t long num,uint32_t long den)
  34. {
  35. return div64_u64(num,den);
  36. }
  37. */
  38. #if 0
  39. //abort,函数打印消息后返回,并不会终止程序运行。
  40. void abort(void)
  41. {
  42. rt_printf("!!!call abort!!!\r\n");
  43. }
  44. #endif
  45. #if 0
  46. //http://www.opensource.apple.com/source/xnu/xnu-792.13.8/libsa/bsearch.c
  47. void * bsearch(const void *key, const void *base0, int nmemb, int size, int (*compar)(const void *, const void *))
  48. {
  49. register const char *base = base0;
  50. register int lim;
  51. register int cmp;
  52. register const void *p;
  53. for (lim = nmemb; lim != 0; lim >>= 1) {
  54. p = base + (lim >> 1) * size;
  55. cmp = (*compar)(key, p);
  56. if (cmp == 0)
  57. return ((void *)p);
  58. if (cmp > 0) { /* key > p: move right */
  59. base = (char *)p + size;
  60. lim--;
  61. } /* else move left */
  62. }
  63. return (0);
  64. }
  65. #endif
  66. // http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/strtol.c
  67. static inline int
  68. _isupper(char c)
  69. {
  70. return (c >= 'A' && c <= 'Z');
  71. }
  72. static inline int
  73. _isalpha(char c)
  74. {
  75. return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
  76. }
  77. static inline int
  78. _isspace(char c)
  79. {
  80. return (c == ' ' || c == '\t' || c == '\n' || c == '\12');
  81. }
  82. static inline int
  83. _isdigit(char c)
  84. {
  85. return (c >= '0' && c <= '9');
  86. }
  87. /*
  88. * Convert a string to a long integer.
  89. *
  90. * Ignores `locale' stuff. Assumes that the upper and lower case
  91. * alphabets and digits are each contiguous.
  92. */
  93. long strtol(const char *nptr, char **endptr, int base)
  94. {
  95. register const char *s = nptr;
  96. register unsigned long acc;
  97. register int c;
  98. register unsigned long cutoff;
  99. register int neg = 0, any, cutlim;
  100. /*
  101. * Skip white space and pick up leading +/- sign if any.
  102. * If base is 0, allow 0x for hex and 0 for octal, else
  103. * assume decimal; if base is already 16, allow 0x.
  104. */
  105. do
  106. {
  107. c = *s++;
  108. } while (_isspace(c));
  109. if (c == '-')
  110. {
  111. neg = 1;
  112. c = *s++;
  113. }
  114. else if (c == '+')
  115. c = *s++;
  116. if ((base == 0 || base == 16) &&
  117. c == '0' && (*s == 'x' || *s == 'X'))
  118. {
  119. c = s[1];
  120. s += 2;
  121. base = 16;
  122. }
  123. else if ((base == 0 || base == 2) &&
  124. c == '0' && (*s == 'b' || *s == 'B'))
  125. {
  126. c = s[1];
  127. s += 2;
  128. base = 2;
  129. }
  130. if (base == 0)
  131. base = c == '0' ? 8 : 10;
  132. /*
  133. * Compute the cutoff value between legal numbers and illegal
  134. * numbers. That is the largest legal value, divided by the
  135. * base. An input number that is greater than this value, if
  136. * followed by a legal input character, is too big. One that
  137. * is equal to this value may be valid or not; the limit
  138. * between valid and invalid numbers is then based on the last
  139. * digit. For instance, if the range for longs is
  140. * [-2147483648..2147483647] and the input base is 10,
  141. * cutoff will be set to 214748364 and cutlim to either
  142. * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  143. * a value > 214748364, or equal but the next digit is > 7 (or 8),
  144. * the number is too big, and we will return a range error.
  145. *
  146. * Set any if any `digits' consumed; make it negative to indicate
  147. * overflow.
  148. */
  149. cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  150. cutlim = cutoff % (unsigned long)base;
  151. cutoff /= (unsigned long)base;
  152. for (acc = 0, any = 0;; c = *s++)
  153. {
  154. if (_isdigit(c))
  155. c -= '0';
  156. else if (_isalpha(c))
  157. c -= _isupper(c) ? 'A' - 10 : 'a' - 10;
  158. else
  159. break;
  160. if (c >= base)
  161. break;
  162. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  163. any = -1;
  164. else
  165. {
  166. any = 1;
  167. acc *= base;
  168. acc += c;
  169. }
  170. }
  171. if (any < 0)
  172. {
  173. acc = neg ? LONG_MIN : LONG_MAX;
  174. // errno = ERANGE;
  175. }
  176. else if (neg)
  177. acc = -acc;
  178. if (endptr != 0)
  179. *endptr = (char *)(any ? s - 1 : nptr);
  180. return (acc);
  181. }
  182. /*
  183. * Convert a string to an unsigned long integer.
  184. *
  185. * Ignores `locale' stuff. Assumes that the upper and lower case
  186. * alphabets and digits are each contiguous.
  187. */
  188. unsigned long strtoul(const char *nptr, char **endptr, int base)
  189. {
  190. register const char *s = nptr;
  191. register unsigned long acc;
  192. register int c;
  193. register unsigned long cutoff;
  194. register int neg = 0, any, cutlim;
  195. /*
  196. * See strtol for comments as to the logic used.
  197. */
  198. do
  199. {
  200. c = *s++;
  201. } while (_isspace(c));
  202. if (c == '-')
  203. {
  204. neg = 1;
  205. c = *s++;
  206. }
  207. else if (c == '+')
  208. c = *s++;
  209. if ((base == 0 || base == 16) &&
  210. c == '0' && (*s == 'x' || *s == 'X'))
  211. {
  212. c = s[1];
  213. s += 2;
  214. base = 16;
  215. }
  216. else if ((base == 0 || base == 2) &&
  217. c == '0' && (*s == 'b' || *s == 'B'))
  218. {
  219. c = s[1];
  220. s += 2;
  221. base = 2;
  222. }
  223. if (base == 0)
  224. base = c == '0' ? 8 : 10;
  225. cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  226. cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  227. for (acc = 0, any = 0;; c = *s++)
  228. {
  229. if (_isdigit(c))
  230. c -= '0';
  231. else if (_isalpha(c))
  232. c -= _isupper(c) ? 'A' - 10 : 'a' - 10;
  233. else
  234. break;
  235. if (c >= base)
  236. break;
  237. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  238. any = -1;
  239. else
  240. {
  241. any = 1;
  242. acc *= base;
  243. acc += c;
  244. }
  245. }
  246. if (any < 0)
  247. {
  248. acc = ULONG_MAX;
  249. // errno = ERANGE;
  250. }
  251. else if (neg)
  252. acc = -acc;
  253. if (endptr != 0)
  254. *endptr = (char *)(any ? s - 1 : nptr);
  255. return (acc);
  256. }
  257. /* MSL
  258. * Copyright 1995-2007 Freescale Corporation. All rights reserved.
  259. *
  260. * $Date: 2009/11/05 19:15:41 $
  261. * $Revision: 1.2 $
  262. */
  263. /*
  264. Author: Matthew D. Fassiotto
  265. Date: first written 4/15/99
  266. Purpose: non-optimal single precision version of standard sqrt functions
  267. Assumptions: --IEEE 754 single precision float format
  268. *fp difference should never produce -0
  269. *casting a float to an int always truncates regardless of fp rounding mode.
  270. *the type _INT32 is 32 bits(i.e. sizeof(_INT32)=sizeof(float)
  271. Note: need to eliminate two divisions in Newton iteration
  272. */
  273. // #include <math.h>
  274. // #include <errno.h>
  275. static int sqrt_guess[] = {0x3F35B99E, 0x3F366D96, 0x3F3720DD, 0x3F37D375, 0x3F388560, 0x3F3936A1, 0x3F39E738,
  276. 0x3F3A9728, 0x3F3B4673, 0x3F3BF51B, 0x3F3CA321, 0x3F3D5087, 0x3F3DFD4E, 0x3F3EA979, 0x3F3F5509, 0x3F400000,
  277. 0x3F40AA5F, 0x3F415428, 0x3F41FD5C, 0x3F42A5FE, 0x3F434E0D, 0x3F43F58D, 0x3F449C7E, 0x3F4542E1, 0x3F45E8B9,
  278. 0x3F468E06, 0x3F4732CA, 0x3F47D706, 0x3F487ABC, 0x3F491DEC, 0x3F49C098, 0x3F4A62C2, 0x3F4B046A, 0x3F4BA592,
  279. 0x3F4C463A, 0x3F4CE665, 0x3F4D8613, 0x3F4E2545, 0x3F4EC3FC, 0x3F4F623A, 0x3F500000, 0x3F509D4E, 0x3F513A26,
  280. 0x3F51D689, 0x3F527278, 0x3F530DF3, 0x3F53A8FD, 0x3F544395, 0x3F54DDBC, 0x3F557775, 0x3F5610BF, 0x3F56A99B,
  281. 0x3F57420B, 0x3F57DA10, 0x3F5871A9, 0x3F5908D9, 0x3F599FA0, 0x3F5A35FE, 0x3F5ACBF5, 0x3F5B6186, 0x3F5BF6B1,
  282. 0x3F5C8B77, 0x3F5D1FD9, 0x3F5DB3D7, 0x3F5E4773, 0x3F5EDAAE, 0x3F5F6D87, 0x3F600000, 0x3F609219, 0x3F6123D4,
  283. 0x3F61B531, 0x3F624630, 0x3F62D6D3, 0x3F636719, 0x3F63F704, 0x3F648695, 0x3F6515CC, 0x3F65A4A9, 0x3F66332E,
  284. 0x3F66C15A, 0x3F674F2F, 0x3F67DCAE, 0x3F6869D6, 0x3F68F6A9, 0x3F698327, 0x3F6A0F50, 0x3F6A9B26, 0x3F6B26A9,
  285. 0x3F6BB1D9, 0x3F6C3CB7, 0x3F6CC744, 0x3F6D517F, 0x3F6DDB6B, 0x3F6E6507, 0x3F6EEE53, 0x3F6F7751, 0x3F700000,
  286. 0x3F708862, 0x3F711076, 0x3F71983E, 0x3F721FBA, 0x3F72A6EA, 0x3F732DCF, 0x3F73B46A, 0x3F743ABA, 0x3F74C0C0,
  287. 0x3F75467E, 0x3F75CBF2, 0x3F76511E, 0x3F76D603, 0x3F775AA0, 0x3F77DEF6, 0x3F786305, 0x3F78E6CE, 0x3F796A52,
  288. 0x3F79ED91, 0x3F7A708B, 0x3F7AF340, 0x3F7B75B1, 0x3F7BF7DF, 0x3F7C79CA, 0x3F7CFB72, 0x3F7D7CD8, 0x3F7DFDFC,
  289. 0x3F7E7EDE, 0x3F7EFF7F, 0x3F7F7FE0, 0x3F800000, 0x3F803FF0};
  290. static int sqrt_guess2[] = {
  291. 0x3F00FF02,
  292. 0x3F017DC7,
  293. 0x3F01FC10,
  294. 0x3F0279DF,
  295. 0x3F02F734,
  296. 0x3F037413,
  297. 0x3F03F07B,
  298. 0x3F046C6F,
  299. 0x3F04E7EE,
  300. 0x3F0562FC,
  301. 0x3F05DD98,
  302. 0x3F0657C5,
  303. 0x3F06D182,
  304. 0x3F074AD3,
  305. 0x3F07C3B6,
  306. 0x3F083C2F,
  307. 0x3F08B43D,
  308. 0x3F092BE3,
  309. 0x3F09A320,
  310. 0x3F0A19F6,
  311. 0x3F0A9067,
  312. 0x3F0B0672,
  313. 0x3F0B7C1A,
  314. 0x3F0BF15E,
  315. 0x3F0C6641,
  316. 0x3F0CDAC3,
  317. 0x3F0D4EE4,
  318. 0x3F0DC2A7,
  319. 0x3F0E360B,
  320. 0x3F0EA912,
  321. 0x3F0F1BBD,
  322. 0x3F0F8E0C,
  323. 0x3F100000,
  324. 0x3F10719A,
  325. 0x3F10E2DC,
  326. 0x3F1153C4,
  327. 0x3F11C456,
  328. 0x3F123491,
  329. 0x3F12A476,
  330. 0x3F131406,
  331. 0x3F138341,
  332. 0x3F13F229,
  333. 0x3F1460BE,
  334. 0x3F14CF01,
  335. 0x3F153CF2,
  336. 0x3F15AA92,
  337. 0x3F1617E3,
  338. 0x3F1684E4,
  339. 0x3F16F196,
  340. 0x3F175DFA,
  341. 0x3F17CA11,
  342. 0x3F1835DC,
  343. 0x3F18A15A,
  344. 0x3F190C8C,
  345. 0x3F197774,
  346. 0x3F19E211,
  347. 0x3F1A4C65,
  348. 0x3F1AB66F,
  349. 0x3F1B2032,
  350. 0x3F1B89AC,
  351. 0x3F1BF2DF,
  352. 0x3F1C5BCB,
  353. 0x3F1CC471,
  354. 0x3F1D2CD1,
  355. 0x3F1D94EC,
  356. 0x3F1DFCC2,
  357. 0x3F1E6455,
  358. 0x3F1ECBA4,
  359. 0x3F1F32AF,
  360. 0x3F1F9979,
  361. 0x3F200000,
  362. 0x3F206646,
  363. 0x3F20CC4A,
  364. 0x3F21320E,
  365. 0x3F219792,
  366. 0x3F21FCD7,
  367. 0x3F2261DC,
  368. 0x3F22C6A3,
  369. 0x3F232B2B,
  370. 0x3F238F75,
  371. 0x3F23F383,
  372. 0x3F245753,
  373. 0x3F24BAE7,
  374. 0x3F251E3E,
  375. 0x3F25815A,
  376. 0x3F25E43B,
  377. 0x3F2646E1,
  378. 0x3F26A94D,
  379. 0x3F270B7F,
  380. 0x3F276D77,
  381. 0x3F27CF36,
  382. 0x3F2830BC,
  383. 0x3F28920A,
  384. 0x3F28F31F,
  385. 0x3F2953FD,
  386. 0x3F29B4A4,
  387. 0x3F2A1514,
  388. 0x3F2A754D,
  389. 0x3F2AD550,
  390. 0x3F2B351D,
  391. 0x3F2B94B5,
  392. 0x3F2BF417,
  393. 0x3F2C5345,
  394. 0x3F2CB23E,
  395. 0x3F2D1104,
  396. 0x3F2D6F95,
  397. 0x3F2DCDF3,
  398. 0x3F2E2C1E,
  399. 0x3F2E8A16,
  400. 0x3F2EE7DB,
  401. 0x3F2F456F,
  402. 0x3F2FA2D0,
  403. 0x3F300000,
  404. 0x3F305CFF,
  405. 0x3F30B9CC,
  406. 0x3F31166A,
  407. 0x3F3172D6,
  408. 0x3F31CF13,
  409. 0x3F322B20,
  410. 0x3F3286FE,
  411. 0x3F32E2AC,
  412. 0x3F333E2C,
  413. 0x3F33997C,
  414. 0x3F33F49F,
  415. 0x3F344F93,
  416. 0x3F34AA5A,
  417. 0x3F3504F3,
  418. 0x3F355F5F,
  419. 0x3F35B99E,
  420. };
  421. #define _UINT32 unsigned int
  422. #define _INT32 int
  423. float sqrtf(float x)
  424. {
  425. const _UINT32 numbits = (sizeof(sqrt_guess)) / (4 * 64) + 5;
  426. /* calculated at compile time(hopefully)--assumes minimal # of
  427. elements in sqrt_guess is 32 or an integral (power of two)*32
  428. */
  429. _UINT32 *u32tmp1 = (_UINT32 *)&x;
  430. _INT32 *s32tmp1 = (_INT32 *)&x;
  431. const _UINT32 bit_shift = 23 - numbits;
  432. const _UINT32 bit_mask = 0x007fffff & (~(sizeof(sqrt_guess) >> 2) << bit_shift);
  433. const _UINT32 first_several_sig_bits_of_x = (*u32tmp1) & bit_mask;
  434. const _INT32 biased_exp = (*u32tmp1) & 0x7f800000;
  435. float guess;
  436. float scaled_x;
  437. _UINT32 *u32tmp2 = (_UINT32 *)&guess;
  438. _UINT32 *u32tmp3 = (_UINT32 *)&scaled_x;
  439. // if(*(_UINT32*)&x & 0x80000000) /* either < 0 or -0 */
  440. // {
  441. // if((*(_UINT32*)&x) & 0x7fffffff) return NAN;
  442. // else return x; /* x = -0 */
  443. // }
  444. // if(!biased_exp) return 0.0f; //flush denormal to 0.0
  445. /* the condition below insures that we round x so that ||sqrt(x)-guess||<=||sqrt(x)-y|| for all y in sqrt_guess[](round to nearest)
  446. since sqrt is monotonically increasing --> ||sqrt(x)-sqrt(guess)|| <= ||sqrt(x)-sqrt(y)||
  447. we look at the remaining low order significant bits of x below the bit_mask.
  448. */
  449. #if 0
  450. #if _EWL_C99
  451. if ((x < 0) && (math_errhandling & MATH_ERRNO))
  452. {
  453. _EWL_LOCALDATA(errno) = EDOM;
  454. return(NAN);
  455. }
  456. #endif
  457. #endif
  458. if (biased_exp & 0x00800000) // if biased_exp is odd then the sqrt of the exponent is 2^^intsqrt(2)
  459. {
  460. (*u32tmp3) = 0x3E800000 + ((*u32tmp1) & 0x007fffff); // scaled_x in [.25,.5)
  461. (*u32tmp2) = sqrt_guess2[(first_several_sig_bits_of_x >> bit_shift)];
  462. }
  463. else
  464. {
  465. (*u32tmp3) = 0x3f000000 + ((*s32tmp1) & 0x007fffff); // scaled_x in [.5,1.0)
  466. (*u32tmp2) = sqrt_guess[(first_several_sig_bits_of_x >> bit_shift)];
  467. }
  468. guess += scaled_x / guess; // now have 12 sig bits
  469. guess = .25f * guess + (scaled_x / guess); // now we have about 24 sig bits
  470. /* we now reduce x to 2^^n*y where y is in [.5,1) we then calculate sqrt(x)=sqrt(2^^n)*sqrt(y)
  471. where if n is even we simply shift the exponent of guess appropriately or if n is odd we shift
  472. and multiply by sqrt(2) if n > 0 and 1/sqrt(2) if n > 0
  473. */
  474. s32tmp1 = (_INT32 *)&guess;
  475. if (biased_exp > 0x3f000000)
  476. (*s32tmp1) += (((biased_exp - 0x3e800000) >> 1) & 0xffbfffff); // this subtracts off bias(127=0x3f80...) // from biased_exp and one more which divides by two
  477. else
  478. (*s32tmp1) -= ((0x3f000000 - biased_exp) >> 1) & 0xffbfffff;
  479. return guess;
  480. }
  481. /*
  482. * ====================================================
  483. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  484. *
  485. * Developed at SunPro, a Sun Microsystems, Inc. business.
  486. * Permission to use, copy, modify, and distribute this
  487. * software is freely granted, provided that this notice
  488. * is preserved.
  489. * ====================================================
  490. */
  491. /*
  492. * from: @(#)fdlibm.h 5.1 93/09/24
  493. */
  494. /* A union which permits us to convert between a float and a 32 bit
  495. int. */
  496. typedef union
  497. {
  498. float value;
  499. u32 word;
  500. } ieee_float_shape_type;
  501. /* Get a 32 bit int from a float. */
  502. #ifndef GET_FLOAT_WORD
  503. #define GET_FLOAT_WORD(i, d) \
  504. do \
  505. { \
  506. ieee_float_shape_type gf_u; \
  507. gf_u.value = (d); \
  508. (i) = gf_u.word; \
  509. } while (0)
  510. #endif
  511. /* Set a float from a 32 bit int. */
  512. #ifndef SET_FLOAT_WORD
  513. #define SET_FLOAT_WORD(d, i) \
  514. do \
  515. { \
  516. ieee_float_shape_type sf_u; \
  517. sf_u.word = (i); \
  518. (d) = sf_u.value; \
  519. } while (0)
  520. #endif
  521. /*
  522. * fabsf(x) returns the absolute value of x.
  523. */
  524. inline float fabsf(float x)
  525. {
  526. #if 1
  527. if (x < 0.0)
  528. {
  529. x = -x;
  530. }
  531. return x;
  532. #else
  533. u32 ix;
  534. GET_FLOAT_UWORD(ix, x);
  535. SET_FLOAT_UWORD(x, ix & 0x7fffffffU);
  536. return x;
  537. #endif
  538. }
  539. /* s_atanf.c -- float version of s_atan.c.
  540. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  541. */
  542. /*
  543. * ====================================================
  544. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  545. *
  546. * Developed at SunPro, a Sun Microsystems, Inc. business.
  547. * Permission to use, copy, modify, and distribute this
  548. * software is freely granted, provided that this notice
  549. * is preserved.
  550. * ====================================================
  551. */
  552. // #include <math.h>
  553. // #include <math_private.h>
  554. static const float atanhi[] = {
  555. 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
  556. 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */
  557. 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */
  558. 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */
  559. };
  560. static const float atanlo[] = {
  561. 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */
  562. 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */
  563. 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */
  564. 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
  565. };
  566. static const float aT[] = {
  567. 3.3333334327e-01, /* 0x3eaaaaaa */
  568. -2.0000000298e-01, /* 0xbe4ccccd */
  569. 1.4285714924e-01, /* 0x3e124925 */
  570. -1.1111110449e-01, /* 0xbde38e38 */
  571. 9.0908870101e-02, /* 0x3dba2e6e */
  572. -7.6918758452e-02, /* 0xbd9d8795 */
  573. 6.6610731184e-02, /* 0x3d886b35 */
  574. -5.8335702866e-02, /* 0xbd6ef16b */
  575. 4.9768779427e-02, /* 0x3d4bda59 */
  576. -3.6531571299e-02, /* 0xbd15a221 */
  577. 1.6285819933e-02, /* 0x3c8569d7 */
  578. };
  579. static const float
  580. one = 1.0,
  581. huge = 1.0e30;
  582. float __atanf(float x)
  583. {
  584. float w, s1, s2, z;
  585. int ix, hx, id;
  586. GET_FLOAT_WORD(hx, x);
  587. ix = hx & 0x7fffffff;
  588. if (ix >= 0x50800000)
  589. { /* if |x| >= 2^34 */
  590. if (ix > 0x7f800000)
  591. return x + x; /* NaN */
  592. if (hx > 0)
  593. return atanhi[3] + atanlo[3];
  594. else
  595. return -atanhi[3] - atanlo[3];
  596. }
  597. if (ix < 0x3ee00000)
  598. { /* |x| < 0.4375 */
  599. if (ix < 0x31000000)
  600. { /* |x| < 2^-29 */
  601. if (huge + x > one)
  602. return x; /* raise inexact */
  603. }
  604. id = -1;
  605. }
  606. else
  607. {
  608. x = fabsf(x);
  609. if (ix < 0x3f980000)
  610. { /* |x| < 1.1875 */
  611. if (ix < 0x3f300000)
  612. { /* 7/16 <=|x|<11/16 */
  613. id = 0;
  614. x = ((float)2.0 * x - one) / ((float)2.0 + x);
  615. }
  616. else
  617. { /* 11/16<=|x|< 19/16 */
  618. id = 1;
  619. x = (x - one) / (x + one);
  620. }
  621. }
  622. else
  623. {
  624. if (ix < 0x401c0000)
  625. { /* |x| < 2.4375 */
  626. id = 2;
  627. x = (x - (float)1.5) / (one + (float)1.5 * x);
  628. }
  629. else
  630. { /* 2.4375 <= |x| < 2^66 */
  631. id = 3;
  632. x = -(float)1.0 / x;
  633. }
  634. }
  635. }
  636. /* end of argument reduction */
  637. z = x * x;
  638. w = z * z;
  639. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  640. s1 = z * (aT[0] + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
  641. s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
  642. if (id < 0)
  643. return x - x * (s1 + s2);
  644. else
  645. {
  646. z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
  647. return (hx < 0) ? -z : z;
  648. }
  649. }
  650. /* e_atan2f.c -- float version of e_atan2.c.
  651. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  652. */
  653. /*
  654. * ====================================================
  655. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  656. *
  657. * Developed at SunPro, a Sun Microsystems, Inc. business.
  658. * Permission to use, copy, modify, and distribute this
  659. * software is freely granted, provided that this notice
  660. * is preserved.
  661. * ====================================================
  662. */
  663. // #include <math.h>
  664. // #include <math_private.h>
  665. static const float
  666. tiny = 1.0e-30,
  667. zero = 0.0,
  668. pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */
  669. pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */
  670. pi = 3.1415927410e+00, /* 0x40490fdb */
  671. pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */
  672. float atan2f(float y, float x)
  673. {
  674. float z;
  675. int32_t k, m, hx, hy, ix, iy;
  676. GET_FLOAT_WORD(hx, x);
  677. ix = hx & 0x7fffffff;
  678. GET_FLOAT_WORD(hy, y);
  679. iy = hy & 0x7fffffff;
  680. if ((ix > 0x7f800000) ||
  681. (iy > 0x7f800000)) /* x or y is NaN */
  682. return x + y;
  683. if (hx == 0x3f800000)
  684. return __atanf(y); /* x=1.0 */
  685. m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
  686. /* when y = 0 */
  687. if (iy == 0)
  688. {
  689. switch (m)
  690. {
  691. case 0:
  692. case 1:
  693. return y; /* atan(+-0,+anything)=+-0 */
  694. case 2:
  695. return pi + tiny; /* atan(+0,-anything) = pi */
  696. case 3:
  697. return -pi - tiny; /* atan(-0,-anything) =-pi */
  698. }
  699. }
  700. /* when x = 0 */
  701. if (ix == 0)
  702. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  703. /* when x is INF */
  704. if (ix == 0x7f800000)
  705. {
  706. if (iy == 0x7f800000)
  707. {
  708. switch (m)
  709. {
  710. case 0:
  711. return pi_o_4 + tiny; /* atan(+INF,+INF) */
  712. case 1:
  713. return -pi_o_4 - tiny; /* atan(-INF,+INF) */
  714. case 2:
  715. return (float)3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/
  716. case 3:
  717. return (float)-3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/
  718. }
  719. }
  720. else
  721. {
  722. switch (m)
  723. {
  724. case 0:
  725. return zero; /* atan(+...,+INF) */
  726. case 1:
  727. return -zero; /* atan(-...,+INF) */
  728. case 2:
  729. return pi + tiny; /* atan(+...,-INF) */
  730. case 3:
  731. return -pi - tiny; /* atan(-...,-INF) */
  732. }
  733. }
  734. }
  735. /* when y is INF */
  736. if (iy == 0x7f800000)
  737. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  738. /* compute y/x */
  739. k = (iy - ix) >> 23;
  740. if (k > 60)
  741. z = pi_o_2 + (float)0.5 * pi_lo; /* |y/x| > 2**60 */
  742. else if (hx < 0 && k < -60)
  743. z = 0.0; /* |y|/x < -2**60 */
  744. else
  745. z = __atanf(fabsf(y / x)); /* safe to do y/x */
  746. switch (m)
  747. {
  748. case 0:
  749. return z; /* atan(+,+) */
  750. case 1:
  751. {
  752. u_int32_t zh;
  753. GET_FLOAT_WORD(zh, z);
  754. SET_FLOAT_WORD(z, zh ^ 0x80000000);
  755. }
  756. return z; /* atan(-,+) */
  757. case 2:
  758. return pi - (z - pi_lo); /* atan(+,-) */
  759. default: /* case 3 */
  760. return (z - pi_lo) - pi; /* atan(-,-) */
  761. }
  762. }
  763. long atol(const char *str)
  764. {
  765. return strtol(str, (char **)0, 10);
  766. }
  767. #if 0
  768. long atoi(const char *str)
  769. {
  770. return strtol(str, (char **)0, 10);
  771. }
  772. #endif
  773. void swap32(void *p)
  774. {
  775. char *ptr, c;
  776. if (!p)
  777. {
  778. return;
  779. }
  780. ptr = (char *)p;
  781. // swap 1,4
  782. c = *ptr;
  783. *ptr = *(ptr + 3);
  784. *(ptr + 3) = c;
  785. // swap 2,3
  786. c = *(ptr + 1);
  787. *(ptr + 1) = *(ptr + 2);
  788. *(ptr + 2) = c;
  789. return;
  790. }
  791. void swap16(void *p)
  792. {
  793. char *ptr, c;
  794. if (!p)
  795. {
  796. return;
  797. }
  798. ptr = (char *)p;
  799. // swap 1,2
  800. c = *ptr;
  801. *ptr = *(ptr + 1);
  802. *(ptr + 1) = c;
  803. return;
  804. }
  805. int test_round(void)
  806. {
  807. int n;
  808. float f;
  809. for (f = -2; f < 2; f += 0.1)
  810. {
  811. n = (int)rt_round(f);
  812. rt_printf("f=%f,n=%d.\r\n", f, n);
  813. }
  814. return 0;
  815. }
  816. /*------------------------------ 内部函数 -------------------------------------
  817. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  818. */
  819. /*------------------------------ 测试函数 -------------------------------------
  820. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  821. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  822. */
  823. /*------------------------------ 文件结束 -------------------------------------
  824. */