PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gmp/mpn/generic/get_str.c

https://gitlab.com/MatthewRock/ecl
C | 500 lines | 324 code | 40 blank | 136 comment | 53 complexity | fdd91fcf38efea02aa0797796ed853bc MD5 | raw file
  1. /* mpn_get_str -- Convert a MSIZE long limb vector pointed to by MPTR
  2. to a printable string in STR in base BASE.
  3. Copyright 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002 Free Software
  4. Foundation, Inc.
  5. This file is part of the GNU MP Library.
  6. The GNU MP Library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or (at your
  9. option) any later version.
  10. The GNU MP Library is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with the GNU MP Library; see the file COPYING.LIB. If not, write to
  16. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "gmp.h"
  19. #include "gmp-impl.h"
  20. #include "longlong.h"
  21. /* Conversion of U {up,un} to a string in base b. Internally, we convert to
  22. base B = b^m, the largest power of b that fits a limb. Basic algorithms:
  23. A) Divide U repeatedly by B, generating a quotient and remainder, until the
  24. quotient becomes zero. The remainders hold the converted digits. Digits
  25. come out from right to left. (Used in mpn_sb_get_str.)
  26. B) Divide U by b^g, for g such that 1/b <= U/b^g < 1, generating a fraction.
  27. Then develop digits by multiplying the fraction repeatedly by b. Digits
  28. come out from left to right. (Currently not used herein, except for in
  29. code for converting single limbs to individual digits.)
  30. C) Compute B^1, B^2, B^4, ..., B^(2^s), for s such that B^(2^s) > sqrt(U).
  31. Then divide U by B^(2^k), generating an integer quotient and remainder.
  32. Recursively convert the quotient, then the remainder, using the
  33. precomputed powers. Digits come out from left to right. (Used in
  34. mpn_dc_get_str.)
  35. When using algorithm C, algorithm B might be suitable for basecase code,
  36. since the required b^g power will be readily accessible.
  37. Optimization ideas:
  38. 1. The recursive function of (C) could use less temporary memory. The powtab
  39. allocation could be trimmed with some computation, and the tmp area could
  40. be reduced, or perhaps eliminated if up is reused for both quotient and
  41. remainder (it is currently used just for remainder).
  42. 2. Store the powers of (C) in normalized form, with the normalization count.
  43. Quotients will usually need to be left-shifted before each divide, and
  44. remainders will either need to be left-shifted of right-shifted.
  45. 3. When b is even, the powers will end up with lots of low zero limbs. Could
  46. save significant time in the mpn_tdiv_qr call by stripping these zeros.
  47. 4. In the code for developing digits from a single limb, we could avoid using
  48. a full umul_ppmm except for the first (or first few) digits, provided base
  49. is even. Subsequent digits can be developed using plain multiplication.
  50. (This saves on register-starved machines (read x86) and on all machines
  51. that generate the upper product half using a separate instruction (alpha,
  52. powerpc, IA-64) or lacks such support altogether (sparc64, hppa64).
  53. 5. Separate mpn_dc_get_str basecase code from code for small conversions. The
  54. former code will have the exact right power readily available in the
  55. powtab parameter for dividing the current number into a fraction. Convert
  56. that using algorithm B.
  57. 6. Completely avoid division. Compute the inverses of the powers now in
  58. powtab instead of the actual powers.
  59. Basic structure of (C):
  60. mpn_get_str:
  61. if POW2_P (n)
  62. ...
  63. else
  64. if (un < GET_STR_PRECOMPUTE_THRESHOLD)
  65. mpn_sb_get_str (str, base, up, un);
  66. else
  67. precompute_power_tables
  68. mpn_dc_get_str
  69. mpn_dc_get_str:
  70. mpn_tdiv_qr
  71. if (qn < GET_STR_DC_THRESHOLD)
  72. mpn_sb_get_str
  73. else
  74. mpn_dc_get_str
  75. if (rn < GET_STR_DC_THRESHOLD)
  76. mpn_sb_get_str
  77. else
  78. mpn_dc_get_str
  79. The reason for the two threshold values is the cost of
  80. precompute_power_tables. GET_STR_PRECOMPUTE_THRESHOLD will be considerably
  81. larger than GET_STR_PRECOMPUTE_THRESHOLD. */
  82. /* The x86s and m68020 have a quotient and remainder "div" instruction and
  83. gcc recognises an adjacent "/" and "%" can be combined using that.
  84. Elsewhere "/" and "%" are either separate instructions, or separate
  85. libgcc calls (which unfortunately gcc as of version 3.0 doesn't combine).
  86. A multiply and subtract should be faster than a "%" in those cases. */
  87. #if HAVE_HOST_CPU_FAMILY_x86 \
  88. || HAVE_HOST_CPU_m68020 \
  89. || HAVE_HOST_CPU_m68030 \
  90. || HAVE_HOST_CPU_m68040 \
  91. || HAVE_HOST_CPU_m68060 \
  92. || HAVE_HOST_CPU_m68360 /* CPU32 */
  93. #define udiv_qrnd_unnorm(q,r,n,d) \
  94. do { \
  95. mp_limb_t __q = (n) / (d); \
  96. mp_limb_t __r = (n) % (d); \
  97. (q) = __q; \
  98. (r) = __r; \
  99. } while (0)
  100. #else
  101. #define udiv_qrnd_unnorm(q,r,n,d) \
  102. do { \
  103. mp_limb_t __q = (n) / (d); \
  104. mp_limb_t __r = (n) - __q*(d); \
  105. (q) = __q; \
  106. (r) = __r; \
  107. } while (0)
  108. #endif
  109. /* When to stop divide-and-conquer and call the basecase mpn_get_str. */
  110. #ifndef GET_STR_DC_THRESHOLD
  111. #define GET_STR_DC_THRESHOLD 15
  112. #endif
  113. /* Whether to bother at all with precomputing powers of the base, or go
  114. to the basecase mpn_get_str directly. */
  115. #ifndef GET_STR_PRECOMPUTE_THRESHOLD
  116. #define GET_STR_PRECOMPUTE_THRESHOLD 30
  117. #endif
  118. struct powers
  119. {
  120. size_t digits_in_base;
  121. mp_ptr p;
  122. mp_size_t n; /* mpz_struct uses int for sizes, but not mpn! */
  123. int base;
  124. };
  125. typedef struct powers powers_t;
  126. /* Convert {UP,UN} to a string with a base as represented in POWTAB, and put
  127. the string in STR. Generate LEN characters, possibly padding with zeros to
  128. the left. If LEN is zero, generate as many characters as required.
  129. Return a pointer immediately after the last digit of the result string.
  130. Complexity is O(UN^2); intended for small conversions. */
  131. static unsigned char *
  132. mpn_sb_get_str (unsigned char *str, size_t len,
  133. mp_ptr up, mp_size_t un,
  134. const powers_t *powtab)
  135. {
  136. mp_limb_t rl, ul;
  137. unsigned char *s;
  138. int base;
  139. size_t l;
  140. /* Allocate memory for largest possible string, given that we only get here
  141. for operands with un < GET_STR_PRECOMPUTE_THRESHOLD and that the smallest
  142. base is 3. 7/11 is an approximation to 1/log2(3). */
  143. #if TUNE_PROGRAM_BUILD
  144. #define BUF_ALLOC (GET_STR_THRESHOLD_LIMIT * BITS_PER_MP_LIMB * 7 / 11)
  145. #else
  146. #define BUF_ALLOC (GET_STR_PRECOMPUTE_THRESHOLD * BITS_PER_MP_LIMB * 7 / 11)
  147. #endif
  148. unsigned char buf[BUF_ALLOC];
  149. #if TUNE_PROGRAM_BUILD
  150. mp_limb_t rp[GET_STR_THRESHOLD_LIMIT];
  151. #else
  152. mp_limb_t rp[GET_STR_PRECOMPUTE_THRESHOLD];
  153. #endif
  154. base = powtab->base;
  155. if (base == 10)
  156. {
  157. /* Special case code for base==10 so that the compiler has a chance to
  158. optimize things. */
  159. MPN_COPY (rp + 1, up, un);
  160. s = buf + BUF_ALLOC;
  161. while (un > 1)
  162. {
  163. int i;
  164. mp_limb_t frac, digit;
  165. MPN_DIVREM_OR_PREINV_DIVREM_1 (rp, (mp_size_t) 1, rp + 1, un,
  166. MP_BASES_BIG_BASE_10,
  167. MP_BASES_BIG_BASE_INVERTED_10,
  168. MP_BASES_NORMALIZATION_STEPS_10);
  169. un -= rp[un] == 0;
  170. frac = (rp[0] + 1) << GMP_NAIL_BITS;
  171. s -= MP_BASES_CHARS_PER_LIMB_10;
  172. i = MP_BASES_CHARS_PER_LIMB_10;
  173. #if HAVE_HOST_CPU_FAMILY_x86
  174. /* The code below turns out to be a bit slower for x86 using gcc.
  175. Use plain code. */
  176. do
  177. {
  178. umul_ppmm (digit, frac, frac, 10);
  179. *s++ = digit;
  180. }
  181. while (--i);
  182. #else
  183. /* Use the fact that 10 in binary is 1010, with the lowest bit 0.
  184. After a few umul_ppmm, we will have accumulated enough low zeros
  185. to use a plain multiply. */
  186. if (MP_BASES_NORMALIZATION_STEPS_10 == 0)
  187. {
  188. umul_ppmm (digit, frac, frac, 10);
  189. *s++ = digit;
  190. i--;
  191. }
  192. if (MP_BASES_NORMALIZATION_STEPS_10 <= 1)
  193. {
  194. umul_ppmm (digit, frac, frac, 10);
  195. *s++ = digit;
  196. i--;
  197. }
  198. if (MP_BASES_NORMALIZATION_STEPS_10 <= 2)
  199. {
  200. umul_ppmm (digit, frac, frac, 10);
  201. *s++ = digit;
  202. i--;
  203. }
  204. if (MP_BASES_NORMALIZATION_STEPS_10 <= 3)
  205. {
  206. umul_ppmm (digit, frac, frac, 10);
  207. *s++ = digit;
  208. i--;
  209. }
  210. frac = (frac + 0xf) >> 4;
  211. do
  212. {
  213. frac *= 10;
  214. digit = frac >> (BITS_PER_MP_LIMB - 4);
  215. *s++ = digit;
  216. frac &= (~(mp_limb_t) 0) >> 4;
  217. }
  218. while (--i);
  219. #endif
  220. s -= MP_BASES_CHARS_PER_LIMB_10;
  221. }
  222. ul = rp[1];
  223. while (ul != 0)
  224. {
  225. udiv_qrnd_unnorm (ul, rl, ul, 10);
  226. *--s = rl;
  227. }
  228. }
  229. else /* not base 10 */
  230. {
  231. unsigned chars_per_limb;
  232. mp_limb_t big_base, big_base_inverted;
  233. unsigned normalization_steps;
  234. chars_per_limb = __mp_bases[base].chars_per_limb;
  235. big_base = __mp_bases[base].big_base;
  236. big_base_inverted = __mp_bases[base].big_base_inverted;
  237. count_leading_zeros (normalization_steps, big_base);
  238. MPN_COPY (rp + 1, up, un);
  239. s = buf + BUF_ALLOC;
  240. while (un > 1)
  241. {
  242. int i;
  243. mp_limb_t frac;
  244. MPN_DIVREM_OR_PREINV_DIVREM_1 (rp, (mp_size_t) 1, rp + 1, un,
  245. big_base, big_base_inverted,
  246. normalization_steps);
  247. un -= rp[un] == 0;
  248. frac = (rp[0] + 1) << GMP_NAIL_BITS;
  249. s -= chars_per_limb;
  250. i = chars_per_limb;
  251. do
  252. {
  253. mp_limb_t digit;
  254. umul_ppmm (digit, frac, frac, base);
  255. *s++ = digit;
  256. }
  257. while (--i);
  258. s -= chars_per_limb;
  259. }
  260. ul = rp[1];
  261. while (ul != 0)
  262. {
  263. udiv_qrnd_unnorm (ul, rl, ul, base);
  264. *--s = rl;
  265. }
  266. }
  267. l = buf + BUF_ALLOC - s;
  268. while (l < len)
  269. {
  270. *str++ = 0;
  271. len--;
  272. }
  273. while (l != 0)
  274. {
  275. *str++ = *s++;
  276. l--;
  277. }
  278. return str;
  279. }
  280. /* Convert {UP,UN} to a string with a base as represented in POWTAB, and put
  281. the string in STR. Generate LEN characters, possibly padding with zeros to
  282. the left. If LEN is zero, generate as many characters as required.
  283. Return a pointer immediately after the last digit of the result string.
  284. This uses divide-and-conquer and is intended for large conversions. */
  285. static unsigned char *
  286. mpn_dc_get_str (unsigned char *str, size_t len,
  287. mp_ptr up, mp_size_t un,
  288. const powers_t *powtab, mp_ptr tmp)
  289. {
  290. if (un < GET_STR_DC_THRESHOLD)
  291. {
  292. if (un != 0)
  293. str = mpn_sb_get_str (str, len, up, un, powtab);
  294. else
  295. {
  296. while (len != 0)
  297. {
  298. *str++ = 0;
  299. len--;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. mp_ptr pwp, qp, rp;
  306. mp_size_t pwn, qn;
  307. pwp = powtab->p;
  308. pwn = powtab->n;
  309. if (un < pwn || (un == pwn && mpn_cmp (up, pwp, un) < 0))
  310. {
  311. str = mpn_dc_get_str (str, len, up, un, powtab - 1, tmp);
  312. }
  313. else
  314. {
  315. qp = tmp; /* (un - pwn + 1) limbs for qp */
  316. rp = up; /* pwn limbs for rp; overwrite up area */
  317. mpn_tdiv_qr (qp, rp, 0L, up, un, pwp, pwn);
  318. qn = un - pwn; qn += qp[qn] != 0; /* quotient size */
  319. if (len != 0)
  320. len = len - powtab->digits_in_base;
  321. str = mpn_dc_get_str (str, len, qp, qn, powtab - 1, tmp + un - pwn + 1);
  322. str = mpn_dc_get_str (str, powtab->digits_in_base, rp, pwn, powtab - 1, tmp);
  323. }
  324. }
  325. return str;
  326. }
  327. /* There are no leading zeros on the digits generated at str, but that's not
  328. currently a documented feature. */
  329. size_t
  330. mpn_get_str (unsigned char *str, int base, mp_ptr up, mp_size_t un)
  331. {
  332. mp_ptr powtab_mem, powtab_mem_ptr;
  333. mp_limb_t big_base;
  334. size_t digits_in_base;
  335. powers_t powtab[30];
  336. int pi;
  337. mp_size_t n;
  338. mp_ptr p, t;
  339. size_t out_len;
  340. mp_ptr tmp;
  341. /* Special case zero, as the code below doesn't handle it. */
  342. if (un == 0)
  343. {
  344. str[0] = 0;
  345. return 1;
  346. }
  347. if (POW2_P (base))
  348. {
  349. /* The base is a power of 2. Convert from most significant end. */
  350. mp_limb_t n1, n0;
  351. int bits_per_digit = __mp_bases[base].big_base;
  352. int cnt;
  353. int bit_pos;
  354. mp_size_t i;
  355. unsigned char *s = str;
  356. n1 = up[un - 1];
  357. count_leading_zeros (cnt, n1);
  358. /* BIT_POS should be R when input ends in least significant nibble,
  359. R + bits_per_digit * n when input ends in nth least significant
  360. nibble. */
  361. {
  362. unsigned long bits;
  363. bits = GMP_NUMB_BITS * un - cnt + GMP_NAIL_BITS;
  364. cnt = bits % bits_per_digit;
  365. if (cnt != 0)
  366. bits += bits_per_digit - cnt;
  367. bit_pos = bits - (un - 1) * GMP_NUMB_BITS;
  368. }
  369. /* Fast loop for bit output. */
  370. i = un - 1;
  371. for (;;)
  372. {
  373. bit_pos -= bits_per_digit;
  374. while (bit_pos >= 0)
  375. {
  376. *s++ = (n1 >> bit_pos) & ((1 << bits_per_digit) - 1);
  377. bit_pos -= bits_per_digit;
  378. }
  379. i--;
  380. if (i < 0)
  381. break;
  382. n0 = (n1 << -bit_pos) & ((1 << bits_per_digit) - 1);
  383. n1 = up[i];
  384. bit_pos += GMP_NUMB_BITS;
  385. *s++ = n0 | (n1 >> bit_pos);
  386. }
  387. return s - str;
  388. }
  389. /* General case. The base is not a power of 2. */
  390. if (un < GET_STR_PRECOMPUTE_THRESHOLD)
  391. {
  392. struct powers ptab[1];
  393. ptab[0].base = base;
  394. return mpn_sb_get_str (str, (size_t) 0, up, un, ptab) - str;
  395. }
  396. /* Allocate one large block for the powers of big_base. With the current
  397. scheme, we need to allocate twice as much as would be possible if a
  398. minimal set of powers were generated. */
  399. #define POWTAB_ALLOC_SIZE (2 * un + 30)
  400. #define TMP_ALLOC_SIZE (un + 30)
  401. powtab_mem = __GMP_ALLOCATE_FUNC_LIMBS (POWTAB_ALLOC_SIZE);
  402. powtab_mem_ptr = powtab_mem;
  403. /* Compute a table of powers: big_base^1, big_base^2, big_base^4, ...,
  404. big_base^(2^k), for k such that the biggest power is between U and
  405. sqrt(U). */
  406. big_base = __mp_bases[base].big_base;
  407. digits_in_base = __mp_bases[base].chars_per_limb;
  408. powtab[0].base = base; /* FIXME: hack for getting base to mpn_sb_get_str */
  409. powtab[1].p = &big_base;
  410. powtab[1].n = 1;
  411. powtab[1].digits_in_base = digits_in_base;
  412. powtab[1].base = base;
  413. powtab[2].p = &big_base;
  414. powtab[2].n = 1;
  415. powtab[2].digits_in_base = digits_in_base;
  416. powtab[2].base = base;
  417. n = 1;
  418. pi = 2;
  419. p = &big_base;
  420. for (;;)
  421. {
  422. ++pi;
  423. t = powtab_mem_ptr;
  424. powtab_mem_ptr += 2 * n;
  425. mpn_sqr_n (t, p, n);
  426. n *= 2; n -= t[n - 1] == 0;
  427. digits_in_base *= 2;
  428. p = t;
  429. powtab[pi].p = p;
  430. powtab[pi].n = n;
  431. powtab[pi].digits_in_base = digits_in_base;
  432. powtab[pi].base = base;
  433. if (2 * n > un)
  434. break;
  435. }
  436. ASSERT_ALWAYS (POWTAB_ALLOC_SIZE > powtab_mem_ptr - powtab_mem);
  437. /* Using our precomputed powers, now in powtab[], convert our number. */
  438. tmp = __GMP_ALLOCATE_FUNC_LIMBS (TMP_ALLOC_SIZE);
  439. out_len = mpn_dc_get_str (str, 0, up, un, powtab + pi, tmp) - str;
  440. __GMP_FREE_FUNC_LIMBS (tmp, TMP_ALLOC_SIZE);
  441. __GMP_FREE_FUNC_LIMBS (powtab_mem, POWTAB_ALLOC_SIZE);
  442. return out_len;
  443. }