rt_clib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. c = *s++;
  107. } while (_isspace(c));
  108. if (c == '-') {
  109. neg = 1;
  110. c = *s++;
  111. } else if (c == '+')
  112. c = *s++;
  113. if ((base == 0 || base == 16) &&
  114. c == '0' && (*s == 'x' || *s == 'X')) {
  115. c = s[1];
  116. s += 2;
  117. base = 16;
  118. } else if ((base == 0 || base == 2) &&
  119. c == '0' && (*s == 'b' || *s == 'B')) {
  120. c = s[1];
  121. s += 2;
  122. base = 2;
  123. }
  124. if (base == 0)
  125. base = c == '0' ? 8 : 10;
  126. /*
  127. * Compute the cutoff value between legal numbers and illegal
  128. * numbers. That is the largest legal value, divided by the
  129. * base. An input number that is greater than this value, if
  130. * followed by a legal input character, is too big. One that
  131. * is equal to this value may be valid or not; the limit
  132. * between valid and invalid numbers is then based on the last
  133. * digit. For instance, if the range for longs is
  134. * [-2147483648..2147483647] and the input base is 10,
  135. * cutoff will be set to 214748364 and cutlim to either
  136. * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  137. * a value > 214748364, or equal but the next digit is > 7 (or 8),
  138. * the number is too big, and we will return a range error.
  139. *
  140. * Set any if any `digits' consumed; make it negative to indicate
  141. * overflow.
  142. */
  143. cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  144. cutlim = cutoff % (unsigned long)base;
  145. cutoff /= (unsigned long)base;
  146. for (acc = 0, any = 0;; c = *s++) {
  147. if (_isdigit(c))
  148. c -= '0';
  149. else if (_isalpha(c))
  150. c -= _isupper(c) ? 'A' - 10 : 'a' - 10;
  151. else
  152. break;
  153. if (c >= base)
  154. break;
  155. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  156. any = -1;
  157. else {
  158. any = 1;
  159. acc *= base;
  160. acc += c;
  161. }
  162. }
  163. if (any < 0) {
  164. acc = neg ? LONG_MIN : LONG_MAX;
  165. // errno = ERANGE;
  166. } else if (neg)
  167. acc = -acc;
  168. if (endptr != 0)
  169. *endptr = (char *)(any ? s - 1 : nptr);
  170. return (acc);
  171. }
  172. /*
  173. * Convert a string to an unsigned long integer.
  174. *
  175. * Ignores `locale' stuff. Assumes that the upper and lower case
  176. * alphabets and digits are each contiguous.
  177. */
  178. unsigned long strtoul(const char *nptr, char **endptr, int base)
  179. {
  180. register const char *s = nptr;
  181. register unsigned long acc;
  182. register int c;
  183. register unsigned long cutoff;
  184. register int neg = 0, any, cutlim;
  185. /*
  186. * See strtol for comments as to the logic used.
  187. */
  188. do {
  189. c = *s++;
  190. } while (_isspace(c));
  191. if (c == '-') {
  192. neg = 1;
  193. c = *s++;
  194. } else if (c == '+')
  195. c = *s++;
  196. if ((base == 0 || base == 16) &&
  197. c == '0' && (*s == 'x' || *s == 'X')) {
  198. c = s[1];
  199. s += 2;
  200. base = 16;
  201. } else if ((base == 0 || base == 2) &&
  202. c == '0' && (*s == 'b' || *s == 'B')) {
  203. c = s[1];
  204. s += 2;
  205. base = 2;
  206. }
  207. if (base == 0)
  208. base = c == '0' ? 8 : 10;
  209. cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  210. cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  211. for (acc = 0, any = 0;; c = *s++) {
  212. if (_isdigit(c))
  213. c -= '0';
  214. else if (_isalpha(c))
  215. c -= _isupper(c) ? 'A' - 10 : 'a' - 10;
  216. else
  217. break;
  218. if (c >= base)
  219. break;
  220. if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  221. any = -1;
  222. else {
  223. any = 1;
  224. acc *= base;
  225. acc += c;
  226. }
  227. }
  228. if (any < 0) {
  229. acc = ULONG_MAX;
  230. // errno = ERANGE;
  231. } else if (neg)
  232. acc = -acc;
  233. if (endptr != 0)
  234. *endptr = (char *)(any ? s - 1 : nptr);
  235. return (acc);
  236. }
  237. /* MSL
  238. * Copyright 1995-2007 Freescale Corporation. All rights reserved.
  239. *
  240. * $Date: 2009/11/05 19:15:41 $
  241. * $Revision: 1.2 $
  242. */
  243. /*
  244. Author: Matthew D. Fassiotto
  245. Date: first written 4/15/99
  246. Purpose: non-optimal single precision version of standard sqrt functions
  247. Assumptions: --IEEE 754 single precision float format
  248. *fp difference should never produce -0
  249. *casting a float to an int always truncates regardless of fp rounding mode.
  250. *the type _INT32 is 32 bits(i.e. sizeof(_INT32)=sizeof(float)
  251. Note: need to eliminate two divisions in Newton iteration
  252. */
  253. //#include <math.h>
  254. //#include <errno.h>
  255. static int sqrt_guess[]={0x3F35B99E,0x3F366D96,0x3F3720DD,0x3F37D375,0x3F388560,0x3F3936A1,0x3F39E738,
  256. 0x3F3A9728,0x3F3B4673,0x3F3BF51B,0x3F3CA321,0x3F3D5087,0x3F3DFD4E,0x3F3EA979,0x3F3F5509,0x3F400000,
  257. 0x3F40AA5F,0x3F415428,0x3F41FD5C,0x3F42A5FE,0x3F434E0D,0x3F43F58D,0x3F449C7E,0x3F4542E1,0x3F45E8B9,
  258. 0x3F468E06,0x3F4732CA,0x3F47D706,0x3F487ABC,0x3F491DEC,0x3F49C098,0x3F4A62C2,0x3F4B046A,0x3F4BA592,
  259. 0x3F4C463A,0x3F4CE665,0x3F4D8613,0x3F4E2545,0x3F4EC3FC,0x3F4F623A,0x3F500000,0x3F509D4E,0x3F513A26,
  260. 0x3F51D689,0x3F527278,0x3F530DF3,0x3F53A8FD,0x3F544395,0x3F54DDBC,0x3F557775,0x3F5610BF,0x3F56A99B,
  261. 0x3F57420B,0x3F57DA10,0x3F5871A9,0x3F5908D9,0x3F599FA0,0x3F5A35FE,0x3F5ACBF5,0x3F5B6186,0x3F5BF6B1,
  262. 0x3F5C8B77,0x3F5D1FD9,0x3F5DB3D7,0x3F5E4773,0x3F5EDAAE,0x3F5F6D87,0x3F600000,0x3F609219,0x3F6123D4,
  263. 0x3F61B531,0x3F624630,0x3F62D6D3,0x3F636719,0x3F63F704,0x3F648695,0x3F6515CC,0x3F65A4A9,0x3F66332E,
  264. 0x3F66C15A,0x3F674F2F,0x3F67DCAE,0x3F6869D6,0x3F68F6A9,0x3F698327,0x3F6A0F50,0x3F6A9B26,0x3F6B26A9,
  265. 0x3F6BB1D9,0x3F6C3CB7,0x3F6CC744,0x3F6D517F,0x3F6DDB6B,0x3F6E6507,0x3F6EEE53,0x3F6F7751,0x3F700000,
  266. 0x3F708862,0x3F711076,0x3F71983E,0x3F721FBA,0x3F72A6EA,0x3F732DCF,0x3F73B46A,0x3F743ABA,0x3F74C0C0,
  267. 0x3F75467E,0x3F75CBF2,0x3F76511E,0x3F76D603,0x3F775AA0,0x3F77DEF6,0x3F786305,0x3F78E6CE,0x3F796A52,
  268. 0x3F79ED91,0x3F7A708B,0x3F7AF340,0x3F7B75B1,0x3F7BF7DF,0x3F7C79CA,0x3F7CFB72,0x3F7D7CD8,0x3F7DFDFC,
  269. 0x3F7E7EDE,0x3F7EFF7F,0x3F7F7FE0,0x3F800000,0x3F803FF0};
  270. static int sqrt_guess2[]={0x3F00FF02,0x3F017DC7,0x3F01FC10,0x3F0279DF,0x3F02F734,0x3F037413,0x3F03F07B,
  271. 0x3F046C6F,0x3F04E7EE,0x3F0562FC,0x3F05DD98,0x3F0657C5,0x3F06D182,0x3F074AD3,0x3F07C3B6,0x3F083C2F,
  272. 0x3F08B43D,0x3F092BE3,0x3F09A320,0x3F0A19F6,0x3F0A9067,0x3F0B0672,0x3F0B7C1A,0x3F0BF15E,0x3F0C6641,
  273. 0x3F0CDAC3,0x3F0D4EE4,0x3F0DC2A7,0x3F0E360B,0x3F0EA912,0x3F0F1BBD,0x3F0F8E0C,0x3F100000,0x3F10719A,
  274. 0x3F10E2DC,0x3F1153C4,0x3F11C456,0x3F123491,0x3F12A476,0x3F131406,0x3F138341,0x3F13F229,0x3F1460BE,
  275. 0x3F14CF01,0x3F153CF2,0x3F15AA92,0x3F1617E3,0x3F1684E4,0x3F16F196,0x3F175DFA,0x3F17CA11,0x3F1835DC,
  276. 0x3F18A15A,0x3F190C8C,0x3F197774,0x3F19E211,0x3F1A4C65,0x3F1AB66F,0x3F1B2032,0x3F1B89AC,0x3F1BF2DF,
  277. 0x3F1C5BCB,0x3F1CC471,0x3F1D2CD1,0x3F1D94EC,0x3F1DFCC2,0x3F1E6455,0x3F1ECBA4,0x3F1F32AF,0x3F1F9979,
  278. 0x3F200000,0x3F206646,0x3F20CC4A,0x3F21320E,0x3F219792,0x3F21FCD7,0x3F2261DC,0x3F22C6A3,0x3F232B2B,
  279. 0x3F238F75,0x3F23F383,0x3F245753,0x3F24BAE7,0x3F251E3E,0x3F25815A,0x3F25E43B,0x3F2646E1,0x3F26A94D,
  280. 0x3F270B7F,0x3F276D77,0x3F27CF36,0x3F2830BC,0x3F28920A,0x3F28F31F,0x3F2953FD,0x3F29B4A4,0x3F2A1514,
  281. 0x3F2A754D,0x3F2AD550,0x3F2B351D,0x3F2B94B5,0x3F2BF417,0x3F2C5345,0x3F2CB23E,0x3F2D1104,0x3F2D6F95,
  282. 0x3F2DCDF3,0x3F2E2C1E,0x3F2E8A16,0x3F2EE7DB,0x3F2F456F,0x3F2FA2D0,0x3F300000,0x3F305CFF,0x3F30B9CC,
  283. 0x3F31166A,0x3F3172D6,0x3F31CF13,0x3F322B20,0x3F3286FE,0x3F32E2AC,0x3F333E2C,0x3F33997C,0x3F33F49F,
  284. 0x3F344F93,0x3F34AA5A,0x3F3504F3,0x3F355F5F,0x3F35B99E,
  285. };
  286. #define _UINT32 unsigned int
  287. #define _INT32 int
  288. float sqrtf(float x)
  289. {
  290. const _UINT32 numbits = (sizeof(sqrt_guess))/(4*64) + 5;
  291. /* calculated at compile time(hopefully)--assumes minimal # of
  292. elements in sqrt_guess is 32 or an integral (power of two)*32
  293. */
  294. _UINT32 *u32tmp1 = (_UINT32*)&x;
  295. _INT32 *s32tmp1 = (_INT32*)&x;
  296. const _UINT32 bit_shift=23-numbits;
  297. const _UINT32 bit_mask=0x007fffff&(~(sizeof(sqrt_guess)>>2)<<bit_shift);
  298. const _UINT32 first_several_sig_bits_of_x=(*u32tmp1) & bit_mask;
  299. const _INT32 biased_exp=(*u32tmp1) & 0x7f800000;
  300. float guess;
  301. float scaled_x;
  302. _UINT32 *u32tmp2 = (_UINT32*)&guess;
  303. _UINT32 *u32tmp3 = (_UINT32*)&scaled_x;
  304. //if(*(_UINT32*)&x & 0x80000000) /* either < 0 or -0 */
  305. // {
  306. // if((*(_UINT32*)&x) & 0x7fffffff) return NAN;
  307. // else return x; /* x = -0 */
  308. // }
  309. //if(!biased_exp) return 0.0f; //flush denormal to 0.0
  310. /* 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)
  311. since sqrt is monotonically increasing --> ||sqrt(x)-sqrt(guess)|| <= ||sqrt(x)-sqrt(y)||
  312. we look at the remaining low order significant bits of x below the bit_mask.
  313. */
  314. #if 0
  315. #if _EWL_C99
  316. if ((x < 0) && (math_errhandling & MATH_ERRNO))
  317. {
  318. _EWL_LOCALDATA(errno) = EDOM;
  319. return(NAN);
  320. }
  321. #endif
  322. #endif
  323. if( biased_exp & 0x00800000) // if biased_exp is odd then the sqrt of the exponent is 2^^intsqrt(2)
  324. {
  325. (*u32tmp3)=0x3E800000 + ((*u32tmp1)&0x007fffff); //scaled_x in [.25,.5)
  326. (*u32tmp2)=sqrt_guess2[(first_several_sig_bits_of_x>>bit_shift)];
  327. }
  328. else
  329. {
  330. (*u32tmp3)=0x3f000000 + ((*s32tmp1)&0x007fffff); //scaled_x in [.5,1.0)
  331. (*u32tmp2)=sqrt_guess[(first_several_sig_bits_of_x>>bit_shift)];
  332. }
  333. guess += scaled_x/guess; // now have 12 sig bits
  334. guess =.25f*guess + (scaled_x/guess); // now we have about 24 sig bits
  335. /* we now reduce x to 2^^n*y where y is in [.5,1) we then calculate sqrt(x)=sqrt(2^^n)*sqrt(y)
  336. where if n is even we simply shift the exponent of guess appropriately or if n is odd we shift
  337. and multiply by sqrt(2) if n > 0 and 1/sqrt(2) if n > 0
  338. */
  339. s32tmp1 = (_INT32*)&guess;
  340. if(biased_exp > 0x3f000000)
  341. (*s32tmp1)+=(((biased_exp - 0x3e800000)>>1)&0xffbfffff) ; // this subtracts off bias(127=0x3f80...) // from biased_exp and one more which divides by two
  342. else
  343. (*s32tmp1)-=((0x3f000000 - biased_exp)>>1)&0xffbfffff;
  344. return guess;
  345. }
  346. /*
  347. * ====================================================
  348. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  349. *
  350. * Developed at SunPro, a Sun Microsystems, Inc. business.
  351. * Permission to use, copy, modify, and distribute this
  352. * software is freely granted, provided that this notice
  353. * is preserved.
  354. * ====================================================
  355. */
  356. /*
  357. * from: @(#)fdlibm.h 5.1 93/09/24
  358. */
  359. /* A union which permits us to convert between a float and a 32 bit
  360. int. */
  361. typedef union
  362. {
  363. float value;
  364. u32 word;
  365. } ieee_float_shape_type;
  366. /* Get a 32 bit int from a float. */
  367. #ifndef GET_FLOAT_WORD
  368. # define GET_FLOAT_WORD(i,d) \
  369. do { \
  370. ieee_float_shape_type gf_u; \
  371. gf_u.value = (d); \
  372. (i) = gf_u.word; \
  373. } while (0)
  374. #endif
  375. /* Set a float from a 32 bit int. */
  376. #ifndef SET_FLOAT_WORD
  377. # define SET_FLOAT_WORD(d,i) \
  378. do { \
  379. ieee_float_shape_type sf_u; \
  380. sf_u.word = (i); \
  381. (d) = sf_u.value; \
  382. } while (0)
  383. #endif
  384. /*
  385. * fabsf(x) returns the absolute value of x.
  386. */
  387. inline float fabsf(float x)
  388. {
  389. #if 1
  390. if(x < 0.0)
  391. {
  392. x = -x;
  393. }
  394. return x;
  395. #else
  396. u32 ix;
  397. GET_FLOAT_UWORD(ix, x);
  398. SET_FLOAT_UWORD(x, ix & 0x7fffffffU);
  399. return x;
  400. #endif
  401. }
  402. /* s_atanf.c -- float version of s_atan.c.
  403. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  404. */
  405. /*
  406. * ====================================================
  407. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  408. *
  409. * Developed at SunPro, a Sun Microsystems, Inc. business.
  410. * Permission to use, copy, modify, and distribute this
  411. * software is freely granted, provided that this notice
  412. * is preserved.
  413. * ====================================================
  414. */
  415. //#include <math.h>
  416. //#include <math_private.h>
  417. static const float atanhi[] = {
  418. 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
  419. 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */
  420. 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */
  421. 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */
  422. };
  423. static const float atanlo[] = {
  424. 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */
  425. 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */
  426. 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */
  427. 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
  428. };
  429. static const float aT[] = {
  430. 3.3333334327e-01, /* 0x3eaaaaaa */
  431. -2.0000000298e-01, /* 0xbe4ccccd */
  432. 1.4285714924e-01, /* 0x3e124925 */
  433. -1.1111110449e-01, /* 0xbde38e38 */
  434. 9.0908870101e-02, /* 0x3dba2e6e */
  435. -7.6918758452e-02, /* 0xbd9d8795 */
  436. 6.6610731184e-02, /* 0x3d886b35 */
  437. -5.8335702866e-02, /* 0xbd6ef16b */
  438. 4.9768779427e-02, /* 0x3d4bda59 */
  439. -3.6531571299e-02, /* 0xbd15a221 */
  440. 1.6285819933e-02, /* 0x3c8569d7 */
  441. };
  442. static const float
  443. one = 1.0,
  444. huge = 1.0e30;
  445. float __atanf(float x)
  446. {
  447. float w,s1,s2,z;
  448. int ix,hx,id;
  449. GET_FLOAT_WORD(hx,x);
  450. ix = hx&0x7fffffff;
  451. if(ix>=0x50800000) { /* if |x| >= 2^34 */
  452. if(ix>0x7f800000)
  453. return x+x; /* NaN */
  454. if(hx>0) return atanhi[3]+atanlo[3];
  455. else return -atanhi[3]-atanlo[3];
  456. } if (ix < 0x3ee00000) { /* |x| < 0.4375 */
  457. if (ix < 0x31000000) { /* |x| < 2^-29 */
  458. if(huge+x>one) return x; /* raise inexact */
  459. }
  460. id = -1;
  461. } else {
  462. x = fabsf(x);
  463. if (ix < 0x3f980000) { /* |x| < 1.1875 */
  464. if (ix < 0x3f300000) { /* 7/16 <=|x|<11/16 */
  465. id = 0; x = ((float)2.0*x-one)/((float)2.0+x);
  466. } else { /* 11/16<=|x|< 19/16 */
  467. id = 1; x = (x-one)/(x+one);
  468. }
  469. } else {
  470. if (ix < 0x401c0000) { /* |x| < 2.4375 */
  471. id = 2; x = (x-(float)1.5)/(one+(float)1.5*x);
  472. } else { /* 2.4375 <= |x| < 2^66 */
  473. id = 3; x = -(float)1.0/x;
  474. }
  475. }}
  476. /* end of argument reduction */
  477. z = x*x;
  478. w = z*z;
  479. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  480. s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10])))));
  481. s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9]))));
  482. if (id<0) return x - x*(s1+s2);
  483. else {
  484. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  485. return (hx<0)? -z:z;
  486. }
  487. }
  488. /* e_atan2f.c -- float version of e_atan2.c.
  489. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  490. */
  491. /*
  492. * ====================================================
  493. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  494. *
  495. * Developed at SunPro, a Sun Microsystems, Inc. business.
  496. * Permission to use, copy, modify, and distribute this
  497. * software is freely granted, provided that this notice
  498. * is preserved.
  499. * ====================================================
  500. */
  501. //#include <math.h>
  502. //#include <math_private.h>
  503. static const float
  504. tiny = 1.0e-30,
  505. zero = 0.0,
  506. pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */
  507. pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */
  508. pi = 3.1415927410e+00, /* 0x40490fdb */
  509. pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */
  510. float atan2f (float y, float x)
  511. {
  512. float z;
  513. int32_t k,m,hx,hy,ix,iy;
  514. GET_FLOAT_WORD(hx,x);
  515. ix = hx&0x7fffffff;
  516. GET_FLOAT_WORD(hy,y);
  517. iy = hy&0x7fffffff;
  518. if((ix>0x7f800000)||
  519. (iy>0x7f800000)) /* x or y is NaN */
  520. return x+y;
  521. if(hx==0x3f800000) return __atanf(y); /* x=1.0 */
  522. m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
  523. /* when y = 0 */
  524. if(iy==0) {
  525. switch(m) {
  526. case 0:
  527. case 1: return y; /* atan(+-0,+anything)=+-0 */
  528. case 2: return pi+tiny;/* atan(+0,-anything) = pi */
  529. case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
  530. }
  531. }
  532. /* when x = 0 */
  533. if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  534. /* when x is INF */
  535. if(ix==0x7f800000) {
  536. if(iy==0x7f800000) {
  537. switch(m) {
  538. case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
  539. case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
  540. case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
  541. case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
  542. }
  543. } else {
  544. switch(m) {
  545. case 0: return zero ; /* atan(+...,+INF) */
  546. case 1: return -zero ; /* atan(-...,+INF) */
  547. case 2: return pi+tiny ; /* atan(+...,-INF) */
  548. case 3: return -pi-tiny ; /* atan(-...,-INF) */
  549. }
  550. }
  551. }
  552. /* when y is INF */
  553. if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  554. /* compute y/x */
  555. k = (iy-ix)>>23;
  556. if(k > 60) z=pi_o_2+(float)0.5*pi_lo; /* |y/x| > 2**60 */
  557. else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */
  558. else z=__atanf(fabsf(y/x)); /* safe to do y/x */
  559. switch (m) {
  560. case 0: return z ; /* atan(+,+) */
  561. case 1: {
  562. u_int32_t zh;
  563. GET_FLOAT_WORD(zh,z);
  564. SET_FLOAT_WORD(z,zh ^ 0x80000000);
  565. }
  566. return z ; /* atan(-,+) */
  567. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  568. default: /* case 3 */
  569. return (z-pi_lo)-pi;/* atan(-,-) */
  570. }
  571. }
  572. long atol(const char *str)
  573. {
  574. return strtol(str, (char **)0, 10);
  575. }
  576. #if 0
  577. long atoi(const char *str)
  578. {
  579. return strtol(str, (char **)0, 10);
  580. }
  581. #endif
  582. void swap32(void *p)
  583. {
  584. char *ptr,c;
  585. if(!p)
  586. {
  587. return;
  588. }
  589. ptr=(char *)p;
  590. //swap 1,4
  591. c=*ptr;
  592. *ptr=*(ptr+3);
  593. *(ptr+3)=c;
  594. //swap 2,3
  595. c=*(ptr+1);
  596. *(ptr+1)=*(ptr+2);
  597. *(ptr+2)=c;
  598. return ;
  599. }
  600. void swap16(void *p)
  601. {
  602. char *ptr,c;
  603. if(!p)
  604. {
  605. return;
  606. }
  607. ptr=(char *)p;
  608. //swap 1,2
  609. c=*ptr;
  610. *ptr=*(ptr+1);
  611. *(ptr+1)=c;
  612. return ;
  613. }
  614. int test_round(void)
  615. {
  616. int n;
  617. float f;
  618. for(f=-2; f<2; f+= 0.1)
  619. {
  620. n = (int)rt_round(f);
  621. rt_printf("f=%f,n=%d.\r\n",f,n);
  622. }
  623. return 0;
  624. }
  625. /*------------------------------ 内部函数 -------------------------------------
  626. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  627. */
  628. /*------------------------------ 测试函数 -------------------------------------
  629. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  630. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  631. */
  632. /*------------------------------ 文件结束 -------------------------------------
  633. */