You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3129 lines
85 KiB

Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #line 18 "decimal.c"
  13. /*
  14. =======================================================================
  15. NOTE: this library implements SQL standard "exact numeric" type
  16. and is not at all generic, but rather intentinally crippled to
  17. follow the standard :)
  18. =======================================================================
  19. Quoting the standard
  20. (SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
  21. 4.4.2 Characteristics of numbers, page 27:
  22. An exact numeric type has a precision P and a scale S. P is a positive
  23. integer that determines the number of significant digits in a
  24. particular radix R, where R is either 2 or 10. S is a non-negative
  25. integer. Every value of an exact numeric type of scale S is of the
  26. form n*10^{-S}, where n is an integer such that -R^P <= n <= R^P.
  27. [...]
  28. If an assignment of some number would result in a loss of its most
  29. significant digit, an exception condition is raised. If least
  30. significant digits are lost, implementation-defined rounding or
  31. truncating occurs, with no exception condition being raised.
  32. [...]
  33. Whenever an exact or approximate numeric value is assigned to an exact
  34. numeric value site, an approximation of its value that preserves
  35. leading significant digits after rounding or truncating is represented
  36. in the declared type of the target. The value is converted to have the
  37. precision and scale of the target. The choice of whether to truncate
  38. or round is implementation-defined.
  39. [...]
  40. All numeric values between the smallest and the largest value,
  41. inclusive, in a given exact numeric type have an approximation
  42. obtained by rounding or truncation for that type; it is
  43. implementation-defined which other numeric values have such
  44. approximations.
  45. 5.3 <literal>, page 143
  46. <exact numeric literal> ::=
  47. <unsigned integer> [ <period> [ <unsigned integer> ] ]
  48. | <period> <unsigned integer>
  49. 6.1 <data type>, page 165:
  50. 19) The <scale> of an <exact numeric type> shall not be greater than
  51. the <precision> of the <exact numeric type>.
  52. 20) For the <exact numeric type>s DECIMAL and NUMERIC:
  53. a) The maximum value of <precision> is implementation-defined.
  54. <precision> shall not be greater than this value.
  55. b) The maximum value of <scale> is implementation-defined. <scale>
  56. shall not be greater than this maximum value.
  57. 21) NUMERIC specifies the data type exact numeric, with the decimal
  58. precision and scale specified by the <precision> and <scale>.
  59. 22) DECIMAL specifies the data type exact numeric, with the decimal
  60. scale specified by the <scale> and the implementation-defined
  61. decimal precision equal to or greater than the value of the
  62. specified <precision>.
  63. 6.26 <numeric value expression>, page 241:
  64. 1) If the declared type of both operands of a dyadic arithmetic
  65. operator is exact numeric, then the declared type of the result is
  66. an implementation-defined exact numeric type, with precision and
  67. scale determined as follows:
  68. a) Let S1 and S2 be the scale of the first and second operands
  69. respectively.
  70. b) The precision of the result of addition and subtraction is
  71. implementation-defined, and the scale is the maximum of S1 and S2.
  72. c) The precision of the result of multiplication is
  73. implementation-defined, and the scale is S1 + S2.
  74. d) The precision and scale of the result of division are
  75. implementation-defined.
  76. */
  77. #include <my_global.h>
  78. #include <m_ctype.h>
  79. #include <myisampack.h>
  80. #include <my_sys.h> /* for my_alloca */
  81. #include <m_string.h>
  82. #include <decimal.h>
  83. /*
  84. Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
  85. So one variable of type decimal_digit_t is limited:
  86. 0 < decimal_digit <= DIG_MAX < DIG_BASE
  87. in the struct st_decimal_t:
  88. intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
  89. before the point
  90. frac - number of decimal digits after the point
  91. buf is an array of decimal_digit_t's
  92. len is the length of buf (length of allocated space) in decimal_digit_t's,
  93. not in bytes
  94. */
  95. typedef decimal_digit_t dec1;
  96. typedef longlong dec2;
  97. #define DIG_PER_DEC1 9
  98. #define DIG_MASK 100000000
  99. #define DIG_BASE 1000000000
  100. #define DIG_MAX (DIG_BASE-1)
  101. #define DIG_BASE2 ((dec2)DIG_BASE * (dec2)DIG_BASE)
  102. #define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
  103. static const dec1 powers10[DIG_PER_DEC1+1]={
  104. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  105. static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
  106. static const dec1 frac_max[DIG_PER_DEC1-1]={
  107. 900000000, 990000000, 999000000,
  108. 999900000, 999990000, 999999000,
  109. 999999900, 999999990 };
  110. static double scaler10[]= {
  111. 1.0, 1e10, 1e20, 1e30, 1e40, 1e50, 1e60, 1e70, 1e80, 1e90
  112. };
  113. static double scaler1[]= {
  114. 1.0, 10.0, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9
  115. };
  116. #ifdef HAVE_purify
  117. #define sanity(d) DBUG_ASSERT((d)->len > 0)
  118. #else
  119. #define sanity(d) DBUG_ASSERT((d)->len >0 && ((d)->buf[0] | \
  120. (d)->buf[(d)->len-1] | 1))
  121. #endif
  122. #define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \
  123. do \
  124. { \
  125. if (unlikely(intg1+frac1 > (len))) \
  126. { \
  127. if (unlikely(intg1 > (len))) \
  128. { \
  129. intg1=(len); \
  130. frac1=0; \
  131. error=E_DEC_OVERFLOW; \
  132. } \
  133. else \
  134. { \
  135. frac1=(len)-intg1; \
  136. error=E_DEC_TRUNCATED; \
  137. } \
  138. } \
  139. else \
  140. error=E_DEC_OK; \
  141. } while(0)
  142. #define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \
  143. do \
  144. { \
  145. dec1 a=(from1)+(from2)+(carry); \
  146. DBUG_ASSERT((carry) <= 1); \
  147. if (((carry)= a >= DIG_BASE)) /* no division here! */ \
  148. a-=DIG_BASE; \
  149. (to)=a; \
  150. } while(0)
  151. #define ADD2(to, from1, from2, carry) \
  152. do \
  153. { \
  154. dec2 a=((dec2)(from1))+(from2)+(carry); \
  155. if (((carry)= a >= DIG_BASE)) \
  156. a-=DIG_BASE; \
  157. if (unlikely(a >= DIG_BASE)) \
  158. { \
  159. a-=DIG_BASE; \
  160. carry++; \
  161. } \
  162. (to)=(dec1) a; \
  163. } while(0)
  164. #define SUB(to, from1, from2, carry) /* to=from1-from2 */ \
  165. do \
  166. { \
  167. dec1 a=(from1)-(from2)-(carry); \
  168. if (((carry)= a < 0)) \
  169. a+=DIG_BASE; \
  170. (to)=a; \
  171. } while(0)
  172. #define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \
  173. do \
  174. { \
  175. dec1 a=(from1)-(from2)-(carry); \
  176. if (((carry)= a < 0)) \
  177. a+=DIG_BASE; \
  178. if (unlikely(a < 0)) \
  179. { \
  180. a+=DIG_BASE; \
  181. carry++; \
  182. } \
  183. (to)=a; \
  184. } while(0)
  185. /*
  186. Get maximum value for given precision and scale
  187. SYNOPSIS
  188. max_decimal()
  189. precision/scale - see decimal_bin_size() below
  190. to - decimal where where the result will be stored
  191. to->buf and to->len must be set.
  192. */
  193. void max_decimal(int precision, int frac, decimal_t *to)
  194. {
  195. int intpart;
  196. dec1 *buf= to->buf;
  197. DBUG_ASSERT(precision && precision >= frac);
  198. to->sign= 0;
  199. if ((intpart= to->intg= (precision - frac)))
  200. {
  201. int firstdigits= intpart % DIG_PER_DEC1;
  202. if (firstdigits)
  203. *buf++= powers10[firstdigits] - 1; /* get 9 99 999 ... */
  204. for(intpart/= DIG_PER_DEC1; intpart; intpart--)
  205. *buf++= DIG_MAX;
  206. }
  207. if ((to->frac= frac))
  208. {
  209. int lastdigits= frac % DIG_PER_DEC1;
  210. for(frac/= DIG_PER_DEC1; frac; frac--)
  211. *buf++= DIG_MAX;
  212. if (lastdigits)
  213. *buf= frac_max[lastdigits - 1];
  214. }
  215. }
  216. static dec1 *remove_leading_zeroes(decimal_t *from, int *intg_result)
  217. {
  218. int intg= from->intg, i;
  219. dec1 *buf0= from->buf;
  220. i= ((intg - 1) % DIG_PER_DEC1) + 1;
  221. while (intg > 0 && *buf0 == 0)
  222. {
  223. intg-= i;
  224. i= DIG_PER_DEC1;
  225. buf0++;
  226. }
  227. if (intg > 0)
  228. {
  229. for (i= (intg - 1) % DIG_PER_DEC1; *buf0 < powers10[i--]; intg--) ;
  230. DBUG_ASSERT(intg > 0);
  231. }
  232. else
  233. intg=0;
  234. *intg_result= intg;
  235. return buf0;
  236. }
  237. /*
  238. Count actual length of fraction part (without ending zeroes)
  239. SYNOPSIS
  240. decimal_actual_fraction()
  241. from number for processing
  242. */
  243. int decimal_actual_fraction(decimal_t *from)
  244. {
  245. int frac= from->frac, i;
  246. dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
  247. if (frac == 0)
  248. return 0;
  249. i= ((frac - 1) % DIG_PER_DEC1 + 1);
  250. while (frac > 0 && *buf0 == 0)
  251. {
  252. frac-= i;
  253. i= DIG_PER_DEC1;
  254. buf0--;
  255. }
  256. if (frac > 0)
  257. {
  258. for (i= DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1);
  259. *buf0 % powers10[i++] == 0;
  260. frac--);
  261. }
  262. return frac;
  263. }
  264. /*
  265. Convert decimal to its printable string representation
  266. SYNOPSIS
  267. decimal2string()
  268. from - value to convert
  269. to - points to buffer where string representation
  270. should be stored
  271. *to_len - in: size of to buffer
  272. out: length of the actually written string
  273. fixed_precision - 0 if representation can be variable length and
  274. fixed_decimals will not be checked in this case.
  275. Put number as with fixed point position with this
  276. number of digits (sign counted and decimal point is
  277. counted)
  278. fixed_decimals - number digits after point.
  279. filler - character to fill gaps in case of fixed_precision > 0
  280. RETURN VALUE
  281. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  282. */
  283. int decimal2string(decimal_t *from, char *to, int *to_len,
  284. int fixed_precision, int fixed_decimals,
  285. char filler)
  286. {
  287. int len, intg, frac= from->frac, i, intg_len, frac_len, fill;
  288. /* number digits before decimal point */
  289. int fixed_intg= (fixed_precision ?
  290. (fixed_precision - fixed_decimals) : 0);
  291. int error=E_DEC_OK;
  292. char *s=to;
  293. dec1 *buf, *buf0=from->buf, tmp;
  294. DBUG_ASSERT(*to_len >= 2+from->sign);
  295. /* removing leading zeroes */
  296. buf0= remove_leading_zeroes(from, &intg);
  297. if (unlikely(intg+frac==0))
  298. {
  299. intg=1;
  300. tmp=0;
  301. buf0=&tmp;
  302. }
  303. if (!(intg_len= fixed_precision ? fixed_intg : intg))
  304. intg_len= 1;
  305. frac_len= fixed_precision ? fixed_decimals : frac;
  306. len= from->sign + intg_len + test(frac) + frac_len;
  307. if (fixed_precision)
  308. {
  309. if (frac > fixed_decimals)
  310. {
  311. error= E_DEC_TRUNCATED;
  312. frac= fixed_decimals;
  313. }
  314. if (intg > fixed_intg)
  315. {
  316. error= E_DEC_OVERFLOW;
  317. intg= fixed_intg;
  318. }
  319. }
  320. else if (unlikely(len > --*to_len)) /* reserve one byte for \0 */
  321. {
  322. int j= len-*to_len;
  323. error= (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
  324. if (frac && j >= frac + 1) j--;
  325. if (j > frac)
  326. {
  327. intg-= j-frac;
  328. frac= 0;
  329. }
  330. else
  331. frac-=j;
  332. len= from->sign + intg_len + test(frac) + frac_len;
  333. }
  334. *to_len=len;
  335. s[len]=0;
  336. if (from->sign)
  337. *s++='-';
  338. if (frac)
  339. {
  340. char *s1= s + intg_len;
  341. fill= frac_len - frac;
  342. buf=buf0+ROUND_UP(intg);
  343. *s1++='.';
  344. for (; frac>0; frac-=DIG_PER_DEC1)
  345. {
  346. dec1 x=*buf++;
  347. for (i=min(frac, DIG_PER_DEC1); i; i--)
  348. {
  349. dec1 y=x/DIG_MASK;
  350. *s1++='0'+(uchar)y;
  351. x-=y*DIG_MASK;
  352. x*=10;
  353. }
  354. }
  355. for(; fill; fill--)
  356. *s1++=filler;
  357. }
  358. fill= intg_len - intg;
  359. if (intg == 0)
  360. fill--; /* symbol 0 before digital point */
  361. for(; fill; fill--)
  362. *s++=filler;
  363. if (intg)
  364. {
  365. s+=intg;
  366. for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
  367. {
  368. dec1 x=*--buf;
  369. for (i=min(intg, DIG_PER_DEC1); i; i--)
  370. {
  371. dec1 y=x/10;
  372. *--s='0'+(uchar)(x-y*10);
  373. x=y;
  374. }
  375. }
  376. }
  377. else
  378. *s= '0';
  379. return error;
  380. }
  381. /*
  382. Return bounds of decimal digits in the number
  383. SYNOPSIS
  384. digits_bounds()
  385. from - decimal number for processing
  386. start_result - index (from 0 ) of first decimal digits will
  387. be written by this address
  388. end_result - index of position just after last decimal digit
  389. be written by this address
  390. */
  391. static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
  392. {
  393. int start, stop, i;
  394. dec1 *buf_beg= from->buf;
  395. dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
  396. dec1 *buf_end= end - 1;
  397. /* find non-zero digit from number begining */
  398. while (buf_beg < end && *buf_beg == 0)
  399. buf_beg++;
  400. if (buf_beg >= end)
  401. {
  402. /* it is zero */
  403. *start_result= *end_result= 0;
  404. return;
  405. }
  406. /* find non-zero decimal digit from number begining */
  407. if (buf_beg == from->buf && from->intg)
  408. {
  409. start= DIG_PER_DEC1 - (i= ((from->intg-1) % DIG_PER_DEC1 + 1));
  410. i--;
  411. }
  412. else
  413. {
  414. i= DIG_PER_DEC1 - 1;
  415. start= (int) ((buf_beg - from->buf) * DIG_PER_DEC1);
  416. }
  417. if (buf_beg < end)
  418. for (; *buf_beg < powers10[i--]; start++) ;
  419. *start_result= start; /* index of first decimal digit (from 0) */
  420. /* find non-zero digit at the end */
  421. while (buf_end > buf_beg && *buf_end == 0)
  422. buf_end--;
  423. /* find non-zero decimal digit from the end */
  424. if (buf_end == end - 1 && from->frac)
  425. {
  426. stop= (int) (((buf_end - from->buf) * DIG_PER_DEC1 +
  427. (i= ((from->frac - 1) % DIG_PER_DEC1 + 1))));
  428. i= DIG_PER_DEC1 - i + 1;
  429. }
  430. else
  431. {
  432. stop= (int) ((buf_end - from->buf + 1) * DIG_PER_DEC1);
  433. i= 1;
  434. }
  435. for (; *buf_end % powers10[i++] == 0; stop--);
  436. *end_result= stop; /* index of position after last decimal digit (from 0) */
  437. }
  438. /*
  439. Left shift for alignment of data in buffer
  440. SYNOPSIS
  441. do_mini_left_shift()
  442. dec pointer to decimal number which have to be shifted
  443. shift number of decimal digits on which it should be shifted
  444. beg/end bounds of decimal digits (see digits_bounds())
  445. NOTE
  446. Result fitting in the buffer should be garanted.
  447. 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
  448. */
  449. void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
  450. {
  451. dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
  452. dec1 *end= dec->buf + ROUND_UP(last) - 1;
  453. int c_shift= DIG_PER_DEC1 - shift;
  454. DBUG_ASSERT(from >= dec->buf);
  455. DBUG_ASSERT(end < dec->buf + dec->len);
  456. if (beg % DIG_PER_DEC1 < shift)
  457. *(from - 1)= (*from) / powers10[c_shift];
  458. for(; from < end; from++)
  459. *from= ((*from % powers10[c_shift]) * powers10[shift] +
  460. (*(from + 1)) / powers10[c_shift]);
  461. *from= (*from % powers10[c_shift]) * powers10[shift];
  462. }
  463. /*
  464. Right shift for alignment of data in buffer
  465. SYNOPSIS
  466. do_mini_left_shift()
  467. dec pointer to decimal number which have to be shifted
  468. shift number of decimal digits on which it should be shifted
  469. beg/end bounds of decimal digits (see digits_bounds())
  470. NOTE
  471. Result fitting in the buffer should be garanted.
  472. 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
  473. */
  474. void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
  475. {
  476. dec1 *from= dec->buf + ROUND_UP(last) - 1;
  477. dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
  478. int c_shift= DIG_PER_DEC1 - shift;
  479. DBUG_ASSERT(from < dec->buf + dec->len);
  480. DBUG_ASSERT(end >= dec->buf);
  481. if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
  482. *(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
  483. for(; from > end; from--)
  484. *from= (*from / powers10[shift] +
  485. (*(from - 1) % powers10[shift]) * powers10[c_shift]);
  486. *from= *from / powers10[shift];
  487. }
  488. /*
  489. Shift of decimal digits in given number (with rounding if it need)
  490. SYNOPSIS
  491. decimal_shift()
  492. dec number to be shifted
  493. shift number of decimal positions
  494. shift > 0 means shift to left shift
  495. shift < 0 meand right shift
  496. NOTE
  497. In fact it is multipling on 10^shift.
  498. RETURN
  499. E_DEC_OK OK
  500. E_DEC_OVERFLOW operation lead to overflow, number is untoched
  501. E_DEC_TRUNCATED number was rounded to fit into buffer
  502. */
  503. int decimal_shift(decimal_t *dec, int shift)
  504. {
  505. /* index of first non zero digit (all indexes from 0) */
  506. int beg;
  507. /* index of position after last decimal digit */
  508. int end;
  509. /* index of digit position just after point */
  510. int point= ROUND_UP(dec->intg) * DIG_PER_DEC1;
  511. /* new point position */
  512. int new_point= point + shift;
  513. /* number of digits in result */
  514. int digits_int, digits_frac;
  515. /* length of result and new fraction in big digits*/
  516. int new_len, new_frac_len;
  517. /* return code */
  518. int err= E_DEC_OK;
  519. int new_front;
  520. if (shift == 0)
  521. return E_DEC_OK;
  522. digits_bounds(dec, &beg, &end);
  523. if (beg == end)
  524. {
  525. decimal_make_zero(dec);
  526. return E_DEC_OK;
  527. }
  528. digits_int= new_point - beg;
  529. set_if_bigger(digits_int, 0);
  530. digits_frac= end - new_point;
  531. set_if_bigger(digits_frac, 0);
  532. if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) >
  533. dec->len)
  534. {
  535. int lack= new_len - dec->len;
  536. int diff;
  537. if (new_frac_len < lack)
  538. return E_DEC_OVERFLOW; /* lack more then we have in fraction */
  539. /* cat off fraction part to allow new number to fit in our buffer */
  540. err= E_DEC_TRUNCATED;
  541. new_frac_len-= lack;
  542. diff= digits_frac - (new_frac_len * DIG_PER_DEC1);
  543. /* Make rounding method as parameter? */
  544. decimal_round(dec, dec, end - point - diff, HALF_UP);
  545. end-= diff;
  546. digits_frac= new_frac_len * DIG_PER_DEC1;
  547. if (end <= beg)
  548. {
  549. /*
  550. we lost all digits (they will be shifted out of buffer), so we can
  551. just return 0
  552. */
  553. decimal_make_zero(dec);
  554. return E_DEC_TRUNCATED;
  555. }
  556. }
  557. if (shift % DIG_PER_DEC1)
  558. {
  559. int l_mini_shift, r_mini_shift, mini_shift;
  560. int do_left;
  561. /*
  562. Calculate left/right shift to align decimal digits inside our bug
  563. digits correctly
  564. */
  565. if (shift > 0)
  566. {
  567. l_mini_shift= shift % DIG_PER_DEC1;
  568. r_mini_shift= DIG_PER_DEC1 - l_mini_shift;
  569. /*
  570. It is left shift so prefer left shift, but if we have not place from
  571. left, we have to have it from right, because we checked length of
  572. result
  573. */
  574. do_left= l_mini_shift <= beg;
  575. DBUG_ASSERT(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
  576. }
  577. else
  578. {
  579. r_mini_shift= (-shift) % DIG_PER_DEC1;
  580. l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
  581. /* see comment above */
  582. do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
  583. DBUG_ASSERT(!do_left || l_mini_shift <= beg);
  584. }
  585. if (do_left)
  586. {
  587. do_mini_left_shift(dec, l_mini_shift, beg, end);
  588. mini_shift=- l_mini_shift;
  589. }
  590. else
  591. {
  592. do_mini_right_shift(dec, r_mini_shift, beg, end);
  593. mini_shift= r_mini_shift;
  594. }
  595. new_point+= mini_shift;
  596. /*
  597. If number is shifted and correctly aligned in buffer we can
  598. finish
  599. */
  600. if (!(shift+= mini_shift) && (new_point - digits_int) < DIG_PER_DEC1)
  601. {
  602. dec->intg= digits_int;
  603. dec->frac= digits_frac;
  604. return err; /* already shifted as it should be */
  605. }
  606. beg+= mini_shift;
  607. end+= mini_shift;
  608. }
  609. /* if new 'decimal front' is in first digit, we do not need move digits */
  610. if ((new_front= (new_point - digits_int)) >= DIG_PER_DEC1 ||
  611. new_front < 0)
  612. {
  613. /* need to move digits */
  614. int d_shift;
  615. dec1 *to, *barier;
  616. if (new_front > 0)
  617. {
  618. /* move left */
  619. d_shift= new_front / DIG_PER_DEC1;
  620. to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
  621. barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
  622. DBUG_ASSERT(to >= dec->buf);
  623. DBUG_ASSERT(barier + d_shift < dec->buf + dec->len);
  624. for(; to <= barier; to++)
  625. *to= *(to + d_shift);
  626. for(barier+= d_shift; to <= barier; to++)
  627. *to= 0;
  628. d_shift= -d_shift;
  629. }
  630. else
  631. {
  632. /* move right */
  633. d_shift= (1 - new_front) / DIG_PER_DEC1;
  634. to= dec->buf + ROUND_UP(end) - 1 + d_shift;
  635. barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
  636. DBUG_ASSERT(to < dec->buf + dec->len);
  637. DBUG_ASSERT(barier - d_shift >= dec->buf);
  638. for(; to >= barier; to--)
  639. *to= *(to - d_shift);
  640. for(barier-= d_shift; to >= barier; to--)
  641. *to= 0;
  642. }
  643. d_shift*= DIG_PER_DEC1;
  644. beg+= d_shift;
  645. end+= d_shift;
  646. new_point+= d_shift;
  647. }
  648. /*
  649. If there are gaps then fill ren with 0.
  650. Only one of following 'for' loops will work becouse beg <= end
  651. */
  652. beg= ROUND_UP(beg + 1) - 1;
  653. end= ROUND_UP(end) - 1;
  654. DBUG_ASSERT(new_point >= 0);
  655. /* We don't want negative new_point below */
  656. if (new_point != 0)
  657. new_point= ROUND_UP(new_point) - 1;
  658. if (new_point > end)
  659. {
  660. do
  661. {
  662. dec->buf[new_point]=0;
  663. } while (--new_point > end);
  664. }
  665. else
  666. {
  667. for (; new_point < beg; new_point++)
  668. dec->buf[new_point]= 0;
  669. }
  670. dec->intg= digits_int;
  671. dec->frac= digits_frac;
  672. return err;
  673. }
  674. /*
  675. Convert string to decimal
  676. SYNOPSIS
  677. internal_str2decl()
  678. from - value to convert. Doesn't have to be \0 terminated!
  679. to - decimal where where the result will be stored
  680. to->buf and to->len must be set.
  681. end - Pointer to pointer to end of string. Will on return be
  682. set to the char after the last used character
  683. fixed - use to->intg, to->frac as limits for input number
  684. NOTE
  685. to->intg and to->frac can be modified even when fixed=1
  686. (but only decreased, in this case)
  687. RETURN VALUE
  688. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_BAD_NUM/E_DEC_OOM
  689. In case of E_DEC_FATAL_ERROR *to is set to decimal zero
  690. (to make error handling easier)
  691. */
  692. int
  693. internal_str2dec(const char *from, decimal_t *to, char **end, my_bool fixed)
  694. {
  695. const char *s= from, *s1, *endp, *end_of_string= *end;
  696. int i, intg, frac, error, intg1, frac1;
  697. dec1 x,*buf;
  698. sanity(to);
  699. error= E_DEC_BAD_NUM; /* In case of bad number */
  700. while (s < end_of_string && my_isspace(&my_charset_latin1, *s))
  701. s++;
  702. if (s == end_of_string)
  703. goto fatal_error;
  704. if ((to->sign= (*s == '-')))
  705. s++;
  706. else if (*s == '+')
  707. s++;
  708. s1=s;
  709. while (s < end_of_string && my_isdigit(&my_charset_latin1, *s))
  710. s++;
  711. intg= (int) (s-s1);
  712. if (s < end_of_string && *s=='.')
  713. {
  714. endp= s+1;
  715. while (endp < end_of_string && my_isdigit(&my_charset_latin1, *endp))
  716. endp++;
  717. frac= (int) (endp - s - 1);
  718. }
  719. else
  720. {
  721. frac= 0;
  722. endp= s;
  723. }
  724. *end= (char*) endp;
  725. if (frac+intg == 0)
  726. goto fatal_error;
  727. error= 0;
  728. if (fixed)
  729. {
  730. if (frac > to->frac)
  731. {
  732. error=E_DEC_TRUNCATED;
  733. frac=to->frac;
  734. }
  735. if (intg > to->intg)
  736. {
  737. error=E_DEC_OVERFLOW;
  738. intg=to->intg;
  739. }
  740. intg1=ROUND_UP(intg);
  741. frac1=ROUND_UP(frac);
  742. if (intg1+frac1 > to->len)
  743. {
  744. error= E_DEC_OOM;
  745. goto fatal_error;
  746. }
  747. }
  748. else
  749. {
  750. intg1=ROUND_UP(intg);
  751. frac1=ROUND_UP(frac);
  752. FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
  753. if (unlikely(error))
  754. {
  755. frac=frac1*DIG_PER_DEC1;
  756. if (error == E_DEC_OVERFLOW)
  757. intg=intg1*DIG_PER_DEC1;
  758. }
  759. }
  760. /* Error is guranteed to be set here */
  761. to->intg=intg;
  762. to->frac=frac;
  763. buf=to->buf+intg1;
  764. s1=s;
  765. for (x=0, i=0; intg; intg--)
  766. {
  767. x+= (*--s - '0')*powers10[i];
  768. if (unlikely(++i == DIG_PER_DEC1))
  769. {
  770. *--buf=x;
  771. x=0;
  772. i=0;
  773. }
  774. }
  775. if (i)
  776. *--buf=x;
  777. buf=to->buf+intg1;
  778. for (x=0, i=0; frac; frac--)
  779. {
  780. x= (*++s1 - '0') + x*10;
  781. if (unlikely(++i == DIG_PER_DEC1))
  782. {
  783. *buf++=x;
  784. x=0;
  785. i=0;
  786. }
  787. }
  788. if (i)
  789. *buf=x*powers10[DIG_PER_DEC1-i];
  790. /* Handle exponent */
  791. if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
  792. {
  793. int str_error;
  794. longlong exponent= my_strtoll10(endp+1, (char**) &end_of_string,
  795. &str_error);
  796. if (end_of_string != endp +1) /* If at least one digit */
  797. {
  798. *end= (char*) end_of_string;
  799. if (str_error > 0)
  800. {
  801. error= E_DEC_BAD_NUM;
  802. goto fatal_error;
  803. }
  804. if (exponent > INT_MAX/2 || (str_error == 0 && exponent < 0))
  805. {
  806. error= E_DEC_OVERFLOW;
  807. goto fatal_error;
  808. }
  809. if (exponent < INT_MIN/2 && error != E_DEC_OVERFLOW)
  810. {
  811. error= E_DEC_TRUNCATED;
  812. goto fatal_error;
  813. }
  814. if (error != E_DEC_OVERFLOW)
  815. error= decimal_shift(to, (int) exponent);
  816. }
  817. }
  818. return error;
  819. fatal_error:
  820. decimal_make_zero(to);
  821. return error;
  822. }
  823. /*
  824. Convert decimal to double
  825. SYNOPSIS
  826. decimal2double()
  827. from - value to convert
  828. to - result will be stored there
  829. RETURN VALUE
  830. E_DEC_OK
  831. */
  832. int decimal2double(decimal_t *from, double *to)
  833. {
  834. double result= 0.0;
  835. int i, exp= 0;
  836. dec1 *buf= from->buf;
  837. for (i= from->intg; i > 0; i-= DIG_PER_DEC1)
  838. result= result * DIG_BASE + *buf++;
  839. for (i= from->frac; i > 0; i-= DIG_PER_DEC1) {
  840. result= result * DIG_BASE + *buf++;
  841. exp+= DIG_PER_DEC1;
  842. }
  843. DBUG_PRINT("info", ("interm.: %f %d %f", result, exp,
  844. scaler10[exp / 10] * scaler1[exp % 10]));
  845. result/= scaler10[exp / 10] * scaler1[exp % 10];
  846. *to= from->sign ? -result : result;
  847. DBUG_PRINT("info", ("result: %f (%lx)", *to, *(ulong *)to));
  848. return E_DEC_OK;
  849. }
  850. /*
  851. Convert double to decimal
  852. SYNOPSIS
  853. double2decimal()
  854. from - value to convert
  855. to - result will be stored there
  856. RETURN VALUE
  857. E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
  858. */
  859. int double2decimal(double from, decimal_t *to)
  860. {
  861. /* TODO: fix it, when we'll have dtoa */
  862. char buff[400], *end;
  863. int length, res;
  864. DBUG_ENTER("double2decimal");
  865. length= my_sprintf(buff, (buff, "%.16G", from));
  866. DBUG_PRINT("info",("from: %g from_as_str: %s", from, buff));
  867. end= buff+length;
  868. res= string2decimal(buff, to, &end);
  869. DBUG_PRINT("exit", ("res: %d", res));
  870. DBUG_RETURN(res);
  871. }
  872. static int ull2dec(ulonglong from, decimal_t *to)
  873. {
  874. int intg1, error=E_DEC_OK;
  875. ulonglong x=from;
  876. dec1 *buf;
  877. sanity(to);
  878. for (intg1=1; from >= DIG_BASE; intg1++, from/=DIG_BASE);
  879. if (unlikely(intg1 > to->len))
  880. {
  881. intg1=to->len;
  882. error=E_DEC_OVERFLOW;
  883. }
  884. to->frac=0;
  885. to->intg=intg1*DIG_PER_DEC1;
  886. for (buf=to->buf+intg1; intg1; intg1--)
  887. {
  888. ulonglong y=x/DIG_BASE;
  889. *--buf=(dec1)(x-y*DIG_BASE);
  890. x=y;
  891. }
  892. return error;
  893. }
  894. int ulonglong2decimal(ulonglong from, decimal_t *to)
  895. {
  896. to->sign=0;
  897. return ull2dec(from, to);
  898. }
  899. int longlong2decimal(longlong from, decimal_t *to)
  900. {
  901. if ((to->sign= from < 0))
  902. return ull2dec(-from, to);
  903. return ull2dec(from, to);
  904. }
  905. int decimal2ulonglong(decimal_t *from, ulonglong *to)
  906. {
  907. dec1 *buf=from->buf;
  908. ulonglong x=0;
  909. int intg, frac;
  910. if (from->sign)
  911. {
  912. *to=ULL(0);
  913. return E_DEC_OVERFLOW;
  914. }
  915. for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
  916. {
  917. ulonglong y=x;
  918. x=x*DIG_BASE + *buf++;
  919. if (unlikely(y > ((ulonglong) ULONGLONG_MAX/DIG_BASE) || x < y))
  920. {
  921. *to=ULONGLONG_MAX;
  922. return E_DEC_OVERFLOW;
  923. }
  924. }
  925. *to=x;
  926. for (frac=from->frac; unlikely(frac > 0); frac-=DIG_PER_DEC1)
  927. if (*buf++)
  928. return E_DEC_TRUNCATED;
  929. return E_DEC_OK;
  930. }
  931. int decimal2longlong(decimal_t *from, longlong *to)
  932. {
  933. dec1 *buf=from->buf;
  934. longlong x=0;
  935. int intg, frac;
  936. for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
  937. {
  938. longlong y=x;
  939. /*
  940. Attention: trick!
  941. we're calculating -|from| instead of |from| here
  942. because |LONGLONG_MIN| > LONGLONG_MAX
  943. so we can convert -9223372036854775808 correctly
  944. */
  945. x=x*DIG_BASE - *buf++;
  946. if (unlikely(y < (LONGLONG_MIN/DIG_BASE) || x > y))
  947. {
  948. /*
  949. the decimal is bigger than any possible integer
  950. return border integer depending on the sign
  951. */
  952. *to= from->sign ? LONGLONG_MIN : LONGLONG_MAX;
  953. return E_DEC_OVERFLOW;
  954. }
  955. }
  956. /* boundary case: 9223372036854775808 */
  957. if (unlikely(from->sign==0 && x == LONGLONG_MIN))
  958. {
  959. *to= LONGLONG_MAX;
  960. return E_DEC_OVERFLOW;
  961. }
  962. *to=from->sign ? x : -x;
  963. for (frac=from->frac; unlikely(frac > 0); frac-=DIG_PER_DEC1)
  964. if (*buf++)
  965. return E_DEC_TRUNCATED;
  966. return E_DEC_OK;
  967. }
  968. /*
  969. Convert decimal to its binary fixed-length representation
  970. two representations of the same length can be compared with memcmp
  971. with the correct -1/0/+1 result
  972. SYNOPSIS
  973. decimal2bin()
  974. from - value to convert
  975. to - points to buffer where string representation should be stored
  976. precision/scale - see decimal_bin_size() below
  977. NOTE
  978. the buffer is assumed to be of the size decimal_bin_size(precision, scale)
  979. RETURN VALUE
  980. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  981. DESCRIPTION
  982. for storage decimal numbers are converted to the "binary" format.
  983. This format has the following properties:
  984. 1. length of the binary representation depends on the {precision, scale}
  985. as provided by the caller and NOT on the intg/frac of the decimal to
  986. convert.
  987. 2. binary representations of the same {precision, scale} can be compared
  988. with memcmp - with the same result as decimal_cmp() of the original
  989. decimals (not taking into account possible precision loss during
  990. conversion).
  991. This binary format is as follows:
  992. 1. First the number is converted to have a requested precision and scale.
  993. 2. Every full DIG_PER_DEC1 digits of intg part are stored in 4 bytes
  994. as is
  995. 3. The first intg % DIG_PER_DEC1 digits are stored in the reduced
  996. number of bytes (enough bytes to store this number of digits -
  997. see dig2bytes)
  998. 4. same for frac - full decimal_digit_t's are stored as is,
  999. the last frac % DIG_PER_DEC1 digits - in the reduced number of bytes.
  1000. 5. If the number is negative - every byte is inversed.
  1001. 5. The very first bit of the resulting byte array is inverted (because
  1002. memcmp compares unsigned bytes, see property 2 above)
  1003. Example:
  1004. 1234567890.1234
  1005. internally is represented as 3 decimal_digit_t's
  1006. 1 234567890 123400000
  1007. (assuming we want a binary representation with precision=14, scale=4)
  1008. in hex it's
  1009. 00-00-00-01 0D-FB-38-D2 07-5A-EF-40
  1010. now, middle decimal_digit_t is full - it stores 9 decimal digits. It goes
  1011. into binary representation as is:
  1012. ........... 0D-FB-38-D2 ............
  1013. First decimal_digit_t has only one decimal digit. We can store one digit in
  1014. one byte, no need to waste four:
  1015. 01 0D-FB-38-D2 ............
  1016. now, last digit. It's 123400000. We can store 1234 in two bytes:
  1017. 01 0D-FB-38-D2 04-D2
  1018. So, we've packed 12 bytes number in 7 bytes.
  1019. And now we invert the highest bit to get the final result:
  1020. 81 0D FB 38 D2 04 D2
  1021. And for -1234567890.1234 it would be
  1022. 7E F2 04 37 2D FB 2D
  1023. */
  1024. int decimal2bin(decimal_t *from, char *to, int precision, int frac)
  1025. {
  1026. dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
  1027. int error=E_DEC_OK, intg=precision-frac,
  1028. isize1, intg1, intg1x, from_intg,
  1029. intg0=intg/DIG_PER_DEC1,
  1030. frac0=frac/DIG_PER_DEC1,
  1031. intg0x=intg-intg0*DIG_PER_DEC1,
  1032. frac0x=frac-frac0*DIG_PER_DEC1,
  1033. frac1=from->frac/DIG_PER_DEC1,
  1034. frac1x=from->frac-frac1*DIG_PER_DEC1,
  1035. isize0=intg0*sizeof(dec1)+dig2bytes[intg0x],
  1036. fsize0=frac0*sizeof(dec1)+dig2bytes[frac0x],
  1037. fsize1=frac1*sizeof(dec1)+dig2bytes[frac1x];
  1038. const int orig_isize0= isize0;
  1039. const int orig_fsize0= fsize0;
  1040. char *orig_to= to;
  1041. buf1= remove_leading_zeroes(from, &from_intg);
  1042. if (unlikely(from_intg+fsize1==0))
  1043. {
  1044. mask=0; /* just in case */
  1045. intg=1;
  1046. buf1=&mask;
  1047. }
  1048. intg1=from_intg/DIG_PER_DEC1;
  1049. intg1x=from_intg-intg1*DIG_PER_DEC1;
  1050. isize1=intg1*sizeof(dec1)+dig2bytes[intg1x];
  1051. if (intg < from_intg)
  1052. {
  1053. buf1+=intg1-intg0+(intg1x>0)-(intg0x>0);
  1054. intg1=intg0; intg1x=intg0x;
  1055. error=E_DEC_OVERFLOW;
  1056. }
  1057. else if (isize0 > isize1)
  1058. {
  1059. while (isize0-- > isize1)
  1060. *to++= (char)mask;
  1061. }
  1062. if (fsize0 < fsize1)
  1063. {
  1064. frac1=frac0; frac1x=frac0x;
  1065. error=E_DEC_TRUNCATED;
  1066. }
  1067. else if (fsize0 > fsize1 && frac1x)
  1068. {
  1069. if (frac0 == frac1)
  1070. {
  1071. frac1x=frac0x;
  1072. fsize0= fsize1;
  1073. }
  1074. else
  1075. {
  1076. frac1++;
  1077. frac1x=0;
  1078. }
  1079. }
  1080. /* intg1x part */
  1081. if (intg1x)
  1082. {
  1083. int i=dig2bytes[intg1x];
  1084. dec1 x=(*buf1++ % powers10[intg1x]) ^ mask;
  1085. switch (i)
  1086. {
  1087. case 1: mi_int1store(to, x); break;
  1088. case 2: mi_int2store(to, x); break;
  1089. case 3: mi_int3store(to, x); break;
  1090. case 4: mi_int4store(to, x); break;
  1091. default: DBUG_ASSERT(0);
  1092. }
  1093. to+=i;
  1094. }
  1095. /* intg1+frac1 part */
  1096. for (stop1=buf1+intg1+frac1; buf1 < stop1; to+=sizeof(dec1))
  1097. {
  1098. dec1 x=*buf1++ ^ mask;
  1099. DBUG_ASSERT(sizeof(dec1) == 4);
  1100. mi_int4store(to, x);
  1101. }
  1102. /* frac1x part */
  1103. if (frac1x)
  1104. {
  1105. dec1 x;
  1106. int i=dig2bytes[frac1x],
  1107. lim=(frac1 < frac0 ? DIG_PER_DEC1 : frac0x);
  1108. while (frac1x < lim && dig2bytes[frac1x] == i)
  1109. frac1x++;
  1110. x=(*buf1 / powers10[DIG_PER_DEC1 - frac1x]) ^ mask;
  1111. switch (i)
  1112. {
  1113. case 1: mi_int1store(to, x); break;
  1114. case 2: mi_int2store(to, x); break;
  1115. case 3: mi_int3store(to, x); break;
  1116. case 4: mi_int4store(to, x); break;
  1117. default: DBUG_ASSERT(0);
  1118. }
  1119. to+=i;
  1120. }
  1121. if (fsize0 > fsize1)
  1122. {
  1123. char *to_end= orig_to + orig_fsize0 + orig_isize0;
  1124. while (fsize0-- > fsize1 && to < to_end)
  1125. *to++=(uchar)mask;
  1126. }
  1127. orig_to[0]^= 0x80;
  1128. /* Check that we have written the whole decimal and nothing more */
  1129. DBUG_ASSERT(to == orig_to + orig_fsize0 + orig_isize0);
  1130. return error;
  1131. }
  1132. /*
  1133. Restores decimal from its binary fixed-length representation
  1134. SYNOPSIS
  1135. bin2decimal()
  1136. from - value to convert
  1137. to - result
  1138. precision/scale - see decimal_bin_size() below
  1139. NOTE
  1140. see decimal2bin()
  1141. the buffer is assumed to be of the size decimal_bin_size(precision, scale)
  1142. RETURN VALUE
  1143. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  1144. */
  1145. int bin2decimal(char *from, decimal_t *to, int precision, int scale)
  1146. {
  1147. int error=E_DEC_OK, intg=precision-scale,
  1148. intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
  1149. intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1,
  1150. intg1=intg0+(intg0x>0), frac1=frac0+(frac0x>0);
  1151. dec1 *buf=to->buf, mask=(*from & 0x80) ? 0 : -1;
  1152. char *stop;
  1153. char *d_copy;
  1154. int bin_size= decimal_bin_size(precision, scale);
  1155. sanity(to);
  1156. d_copy= (char *)my_alloca(bin_size);
  1157. memcpy(d_copy, from, bin_size);
  1158. d_copy[0]^= 0x80;
  1159. from= d_copy;
  1160. FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
  1161. if (unlikely(error))
  1162. {
  1163. if (intg1 < intg0+(intg0x>0))
  1164. {
  1165. from+=dig2bytes[intg0x]+sizeof(dec1)*(intg0-intg1);
  1166. frac0=frac0x=intg0x=0;
  1167. intg0=intg1;
  1168. }
  1169. else
  1170. {
  1171. frac0x=0;
  1172. frac0=frac1;
  1173. }
  1174. }
  1175. to->sign=(mask != 0);
  1176. to->intg=intg0*DIG_PER_DEC1+intg0x;
  1177. to->frac=frac0*DIG_PER_DEC1+frac0x;
  1178. if (intg0x)
  1179. {
  1180. int i=dig2bytes[intg0x];
  1181. dec1 x;
  1182. LINT_INIT(x);
  1183. switch (i)
  1184. {
  1185. case 1: x=mi_sint1korr(from); break;
  1186. case 2: x=mi_sint2korr(from); break;
  1187. case 3: x=mi_sint3korr(from); break;
  1188. case 4: x=mi_sint4korr(from); break;
  1189. default: DBUG_ASSERT(0);
  1190. }
  1191. from+=i;
  1192. *buf=x ^ mask;
  1193. if (((ulonglong)*buf) >= (ulonglong) powers10[intg0x+1])
  1194. goto err;
  1195. if (buf > to->buf || *buf != 0)
  1196. buf++;
  1197. else
  1198. to->intg-=intg0x;
  1199. }
  1200. for (stop=from+intg0*sizeof(dec1); from < stop; from+=sizeof(dec1))
  1201. {
  1202. DBUG_ASSERT(sizeof(dec1) == 4);
  1203. *buf=mi_sint4korr(from) ^ mask;
  1204. if (((uint32)*buf) > DIG_MAX)
  1205. goto err;
  1206. if (buf > to->buf || *buf != 0)
  1207. buf++;
  1208. else
  1209. to->intg-=DIG_PER_DEC1;
  1210. }
  1211. DBUG_ASSERT(to->intg >=0);
  1212. for (stop=from+frac0*sizeof(dec1); from < stop; from+=sizeof(dec1))
  1213. {
  1214. DBUG_ASSERT(sizeof(dec1) == 4);
  1215. *buf=mi_sint4korr(from) ^ mask;
  1216. if (((uint32)*buf) > DIG_MAX)
  1217. goto err;
  1218. buf++;
  1219. }
  1220. if (frac0x)
  1221. {
  1222. int i=dig2bytes[frac0x];
  1223. dec1 x;
  1224. LINT_INIT(x);
  1225. switch (i)
  1226. {
  1227. case 1: x=mi_sint1korr(from); break;
  1228. case 2: x=mi_sint2korr(from); break;
  1229. case 3: x=mi_sint3korr(from); break;
  1230. case 4: x=mi_sint4korr(from); break;
  1231. default: DBUG_ASSERT(0);
  1232. }
  1233. *buf=(x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
  1234. if (((uint32)*buf) > DIG_MAX)
  1235. goto err;
  1236. buf++;
  1237. }
  1238. my_afree(d_copy);
  1239. return error;
  1240. err:
  1241. my_afree(d_copy);
  1242. decimal_make_zero(((decimal_t*) to));
  1243. return(E_DEC_BAD_NUM);
  1244. }
  1245. /*
  1246. Returns the size of array to hold a decimal with given precision and scale
  1247. RETURN VALUE
  1248. size in dec1
  1249. (multiply by sizeof(dec1) to get the size if bytes)
  1250. */
  1251. int decimal_size(int precision, int scale)
  1252. {
  1253. DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
  1254. return ROUND_UP(precision-scale)+ROUND_UP(scale);
  1255. }
  1256. /*
  1257. Returns the size of array to hold a binary representation of a decimal
  1258. RETURN VALUE
  1259. size in bytes
  1260. */
  1261. int decimal_bin_size(int precision, int scale)
  1262. {
  1263. int intg=precision-scale,
  1264. intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
  1265. intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1;
  1266. DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
  1267. return intg0*sizeof(dec1)+dig2bytes[intg0x]+
  1268. frac0*sizeof(dec1)+dig2bytes[frac0x];
  1269. }
  1270. /*
  1271. Rounds the decimal to "scale" digits
  1272. SYNOPSIS
  1273. decimal_round()
  1274. from - decimal to round,
  1275. to - result buffer. from==to is allowed
  1276. scale - to what position to round. can be negative!
  1277. mode - round to nearest even or truncate
  1278. NOTES
  1279. scale can be negative !
  1280. one TRUNCATED error (line XXX below) isn't treated very logical :(
  1281. RETURN VALUE
  1282. E_DEC_OK/E_DEC_TRUNCATED
  1283. */
  1284. int
  1285. decimal_round(decimal_t *from, decimal_t *to, int scale,
  1286. decimal_round_mode mode)
  1287. {
  1288. int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1,
  1289. frac1=ROUND_UP(from->frac), round_digit,
  1290. intg0=ROUND_UP(from->intg), error=E_DEC_OK, len=to->len,
  1291. intg1=ROUND_UP(from->intg +
  1292. (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX)));
  1293. dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
  1294. int first_dig;
  1295. sanity(to);
  1296. LINT_INIT(round_digit);
  1297. switch (mode) {
  1298. case HALF_UP:
  1299. case HALF_EVEN: round_digit=5; break;
  1300. case CEILING: round_digit= from->sign ? 10 : 0; break;
  1301. case FLOOR: round_digit= from->sign ? 0 : 10; break;
  1302. case TRUNCATE: round_digit=10; break;
  1303. default: DBUG_ASSERT(0);
  1304. }
  1305. if (unlikely(frac0+intg0 > len))
  1306. {
  1307. frac0=len-intg0;
  1308. scale=frac0*DIG_PER_DEC1;
  1309. error=E_DEC_TRUNCATED;
  1310. }
  1311. if (scale+from->intg < 0)
  1312. {
  1313. decimal_make_zero(to);
  1314. return E_DEC_OK;
  1315. }
  1316. if (to != from || intg1>intg0)
  1317. {
  1318. dec1 *p0= buf0+intg0+max(frac1, frac0);
  1319. dec1 *p1= buf1+intg1+max(frac1, frac0);
  1320. to->buf[0]= 0;
  1321. while (buf0 < p0)
  1322. *(--p1) = *(--p0);
  1323. intg0= intg1;
  1324. buf0=to->buf;
  1325. buf1=to->buf;
  1326. to->sign=from->sign;
  1327. to->intg=min(intg0, len)*DIG_PER_DEC1;
  1328. }
  1329. if (frac0 > frac1)
  1330. {
  1331. buf1+=intg0+frac1;
  1332. while (frac0-- > frac1)
  1333. *buf1++=0;
  1334. goto done;
  1335. }
  1336. if (scale >= from->frac)
  1337. goto done; /* nothing to do */
  1338. buf0+=intg0+frac0-1;
  1339. buf1+=intg0+frac0-1;
  1340. if (scale == frac0*DIG_PER_DEC1)
  1341. {
  1342. int do_inc= FALSE;
  1343. DBUG_ASSERT(frac0+intg0 >= 0);
  1344. switch (round_digit) {
  1345. case 0:
  1346. {
  1347. dec1 *p0= buf0 + (frac1-frac0);
  1348. for (; p0 > buf0; p0--)
  1349. {
  1350. if (*p0)
  1351. {
  1352. do_inc= TRUE;
  1353. break;
  1354. }
  1355. }
  1356. break;
  1357. }
  1358. case 5:
  1359. {
  1360. x= buf0[1]/DIG_MASK;
  1361. do_inc= (x>5) || ((x == 5) &&
  1362. (mode == HALF_UP || (frac0+intg0 > 0 && *buf0 & 1)));
  1363. break;
  1364. }
  1365. default:
  1366. break;
  1367. }
  1368. if (do_inc)
  1369. {
  1370. if (frac0+intg0>0)
  1371. (*buf1)++;
  1372. else
  1373. *(++buf1)=DIG_BASE;
  1374. }
  1375. else if (frac0+intg0==0)
  1376. {
  1377. decimal_make_zero(to);
  1378. return E_DEC_OK;
  1379. }
  1380. }
  1381. else
  1382. {
  1383. /* TODO - fix this code as it won't work for CEILING mode */
  1384. int pos=frac0*DIG_PER_DEC1-scale-1;
  1385. DBUG_ASSERT(frac0+intg0 > 0);
  1386. x=*buf1 / powers10[pos];
  1387. y=x % 10;
  1388. if (y > round_digit ||
  1389. (round_digit == 5 && y == 5 && (mode == HALF_UP || (x/10) & 1)))
  1390. x+=10;
  1391. *buf1=powers10[pos]*(x-y);
  1392. }
  1393. if (frac0 < 0)
  1394. {
  1395. dec1 *end=to->buf+intg0, *buf=buf1+1;
  1396. while (buf < end)
  1397. *buf++=0;
  1398. }
  1399. if (*buf1 >= DIG_BASE)
  1400. {
  1401. carry=1;
  1402. *buf1-=DIG_BASE;
  1403. while (carry && --buf1 >= to->buf)
  1404. ADD(*buf1, *buf1, 0, carry);
  1405. if (unlikely(carry))
  1406. {
  1407. /* shifting the number to create space for new digit */
  1408. if (frac0+intg0 >= len)
  1409. {
  1410. frac0--;
  1411. scale=frac0*DIG_PER_DEC1;
  1412. error=E_DEC_TRUNCATED; /* XXX */
  1413. }
  1414. for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
  1415. {
  1416. buf1[0]=buf1[-1];
  1417. }
  1418. *buf1=1;
  1419. to->intg++;
  1420. }
  1421. }
  1422. else
  1423. {
  1424. for (;;)
  1425. {
  1426. if (likely(*buf1))
  1427. break;
  1428. if (buf1-- == to->buf)
  1429. {
  1430. /* making 'zero' with the proper scale */
  1431. dec1 *p0= to->buf + frac0 + 1;
  1432. to->intg=1;
  1433. to->frac= max(scale, 0);
  1434. to->sign= 0;
  1435. for (buf1= to->buf; buf1<p0; buf1++)
  1436. *buf1= 0;
  1437. return E_DEC_OK;
  1438. }
  1439. }
  1440. }
  1441. /* Here we check 999.9 -> 1000 case when we need to increase intg */
  1442. first_dig= to->intg % DIG_PER_DEC1;
  1443. if (first_dig && (*buf1 >= powers10[first_dig]))
  1444. to->intg++;
  1445. if (scale<0)
  1446. scale=0;
  1447. done:
  1448. to->frac=scale;
  1449. return error;
  1450. }
  1451. /*
  1452. Returns the size of the result of the operation
  1453. SYNOPSIS
  1454. decimal_result_size()
  1455. from1 - operand of the unary operation or first operand of the
  1456. binary operation
  1457. from2 - second operand of the binary operation
  1458. op - operation. one char '+', '-', '*', '/' are allowed
  1459. others may be added later
  1460. param - extra param to the operation. unused for '+', '-', '*'
  1461. scale increment for '/'
  1462. NOTE
  1463. returned valued may be larger than the actual buffer requred
  1464. in the operation, as decimal_result_size, by design, operates on
  1465. precision/scale values only and not on the actual decimal number
  1466. RETURN VALUE
  1467. size of to->buf array in dec1 elements. to get size in bytes
  1468. multiply by sizeof(dec1)
  1469. */
  1470. int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param)
  1471. {
  1472. switch (op) {
  1473. case '-':
  1474. return ROUND_UP(max(from1->intg, from2->intg)) +
  1475. ROUND_UP(max(from1->frac, from2->frac));
  1476. case '+':
  1477. return ROUND_UP(max(from1->intg, from2->intg)+1) +
  1478. ROUND_UP(max(from1->frac, from2->frac));
  1479. case '*':
  1480. return ROUND_UP(from1->intg+from2->intg)+
  1481. ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
  1482. case '/':
  1483. return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param);
  1484. default: DBUG_ASSERT(0);
  1485. }
  1486. return -1; /* shut up the warning */
  1487. }
  1488. static int do_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
  1489. {
  1490. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1491. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
  1492. frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
  1493. dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
  1494. sanity(to);
  1495. /* is there a need for extra word because of carry ? */
  1496. x=intg1 > intg2 ? from1->buf[0] :
  1497. intg2 > intg1 ? from2->buf[0] :
  1498. from1->buf[0] + from2->buf[0] ;
  1499. if (unlikely(x > DIG_MAX-1)) /* yes, there is */
  1500. {
  1501. intg0++;
  1502. to->buf[0]=0; /* safety */
  1503. }
  1504. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
  1505. if (unlikely(error == E_DEC_OVERFLOW))
  1506. {
  1507. max_decimal(to->len * DIG_PER_DEC1, 0, to);
  1508. return error;
  1509. }
  1510. buf0=to->buf+intg0+frac0;
  1511. to->sign=from1->sign;
  1512. to->frac=max(from1->frac, from2->frac);
  1513. to->intg=intg0*DIG_PER_DEC1;
  1514. if (unlikely(error))
  1515. {
  1516. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1517. set_if_smaller(frac1, frac0);
  1518. set_if_smaller(frac2, frac0);
  1519. set_if_smaller(intg1, intg0);
  1520. set_if_smaller(intg2, intg0);
  1521. }
  1522. /* part 1 - max(frac) ... min (frac) */
  1523. if (frac1 > frac2)
  1524. {
  1525. buf1=from1->buf+intg1+frac1;
  1526. stop=from1->buf+intg1+frac2;
  1527. buf2=from2->buf+intg2+frac2;
  1528. stop2=from1->buf+(intg1 > intg2 ? intg1-intg2 : 0);
  1529. }
  1530. else
  1531. {
  1532. buf1=from2->buf+intg2+frac2;
  1533. stop=from2->buf+intg2+frac1;
  1534. buf2=from1->buf+intg1+frac1;
  1535. stop2=from2->buf+(intg2 > intg1 ? intg2-intg1 : 0);
  1536. }
  1537. while (buf1 > stop)
  1538. *--buf0=*--buf1;
  1539. /* part 2 - min(frac) ... min(intg) */
  1540. carry=0;
  1541. while (buf1 > stop2)
  1542. {
  1543. ADD(*--buf0, *--buf1, *--buf2, carry);
  1544. }
  1545. /* part 3 - min(intg) ... max(intg) */
  1546. buf1= intg1 > intg2 ? ((stop=from1->buf)+intg1-intg2) :
  1547. ((stop=from2->buf)+intg2-intg1) ;
  1548. while (buf1 > stop)
  1549. {
  1550. ADD(*--buf0, *--buf1, 0, carry);
  1551. }
  1552. if (unlikely(carry))
  1553. *--buf0=1;
  1554. DBUG_ASSERT(buf0 == to->buf || buf0 == to->buf+1);
  1555. return error;
  1556. }
  1557. /* to=from1-from2.
  1558. if to==0, return -1/0/+1 - the result of the comparison */
  1559. static int do_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
  1560. {
  1561. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1562. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
  1563. int frac0=max(frac1, frac2), error;
  1564. dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
  1565. /* let carry:=1 if from2 > from1 */
  1566. start1=buf1=from1->buf; stop1=buf1+intg1;
  1567. start2=buf2=from2->buf; stop2=buf2+intg2;
  1568. if (unlikely(*buf1 == 0))
  1569. {
  1570. while (buf1 < stop1 && *buf1 == 0)
  1571. buf1++;
  1572. start1=buf1;
  1573. intg1= (int) (stop1-buf1);
  1574. }
  1575. if (unlikely(*buf2 == 0))
  1576. {
  1577. while (buf2 < stop2 && *buf2 == 0)
  1578. buf2++;
  1579. start2=buf2;
  1580. intg2= (int) (stop2-buf2);
  1581. }
  1582. if (intg2 > intg1)
  1583. carry=1;
  1584. else if (intg2 == intg1)
  1585. {
  1586. dec1 *end1= stop1 + (frac1 - 1);
  1587. dec1 *end2= stop2 + (frac2 - 1);
  1588. while (unlikely((buf1 <= end1) && (*end1 == 0)))
  1589. end1--;
  1590. while (unlikely((buf2 <= end2) && (*end2 == 0)))
  1591. end2--;
  1592. frac1= (int) (end1 - stop1) + 1;
  1593. frac2= (int) (end2 - stop2) + 1;
  1594. while (buf1 <=end1 && buf2 <= end2 && *buf1 == *buf2)
  1595. buf1++, buf2++;
  1596. if (buf1 <= end1)
  1597. {
  1598. if (buf2 <= end2)
  1599. carry= *buf2 > *buf1;
  1600. else
  1601. carry= 0;
  1602. }
  1603. else
  1604. {
  1605. if (buf2 <= end2)
  1606. carry=1;
  1607. else /* short-circuit everything: from1 == from2 */
  1608. {
  1609. if (to == 0) /* decimal_cmp() */
  1610. return 0;
  1611. decimal_make_zero(to);
  1612. return E_DEC_OK;
  1613. }
  1614. }
  1615. }
  1616. if (to == 0) /* decimal_cmp() */
  1617. return carry == from1->sign ? 1 : -1;
  1618. sanity(to);
  1619. to->sign=from1->sign;
  1620. /* ensure that always from1 > from2 (and intg1 >= intg2) */
  1621. if (carry)
  1622. {
  1623. swap_variables(decimal_t *,from1,from1);
  1624. swap_variables(dec1 *,start1, start2);
  1625. swap_variables(int,intg1,intg2);
  1626. swap_variables(int,frac1,frac2);
  1627. to->sign= 1 - to->sign;
  1628. }
  1629. FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
  1630. buf0=to->buf+intg1+frac0;
  1631. to->frac=max(from1->frac, from2->frac);
  1632. to->intg=intg1*DIG_PER_DEC1;
  1633. if (unlikely(error))
  1634. {
  1635. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1636. set_if_smaller(frac1, frac0);
  1637. set_if_smaller(frac2, frac0);
  1638. set_if_smaller(intg2, intg1);
  1639. }
  1640. carry=0;
  1641. /* part 1 - max(frac) ... min (frac) */
  1642. if (frac1 > frac2)
  1643. {
  1644. buf1=start1+intg1+frac1;
  1645. stop1=start1+intg1+frac2;
  1646. buf2=start2+intg2+frac2;
  1647. while (frac0-- > frac1)
  1648. *--buf0=0;
  1649. while (buf1 > stop1)
  1650. *--buf0=*--buf1;
  1651. }
  1652. else
  1653. {
  1654. buf1=start1+intg1+frac1;
  1655. buf2=start2+intg2+frac2;
  1656. stop2=start2+intg2+frac1;
  1657. while (frac0-- > frac2)
  1658. *--buf0=0;
  1659. while (buf2 > stop2)
  1660. {
  1661. SUB(*--buf0, 0, *--buf2, carry);
  1662. }
  1663. }
  1664. /* part 2 - min(frac) ... intg2 */
  1665. while (buf2 > start2)
  1666. {
  1667. SUB(*--buf0, *--buf1, *--buf2, carry);
  1668. }
  1669. /* part 3 - intg2 ... intg1 */
  1670. while (carry && buf1 > start1)
  1671. {
  1672. SUB(*--buf0, *--buf1, 0, carry);
  1673. }
  1674. while (buf1 > start1)
  1675. *--buf0=*--buf1;
  1676. while (buf0 > to->buf)
  1677. *--buf0=0;
  1678. return error;
  1679. }
  1680. int decimal_intg(decimal_t *from)
  1681. {
  1682. int res;
  1683. dec1 *tmp_res;
  1684. tmp_res= remove_leading_zeroes(from, &res);
  1685. return res;
  1686. }
  1687. int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
  1688. {
  1689. if (likely(from1->sign == from2->sign))
  1690. return do_add(from1, from2, to);
  1691. return do_sub(from1, from2, to);
  1692. }
  1693. int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
  1694. {
  1695. if (likely(from1->sign == from2->sign))
  1696. return do_sub(from1, from2, to);
  1697. return do_add(from1, from2, to);
  1698. }
  1699. int decimal_cmp(decimal_t *from1, decimal_t *from2)
  1700. {
  1701. if (likely(from1->sign == from2->sign))
  1702. return do_sub(from1, from2, 0);
  1703. return from1->sign > from2->sign ? -1 : 1;
  1704. }
  1705. int decimal_is_zero(decimal_t *from)
  1706. {
  1707. dec1 *buf1=from->buf,
  1708. *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac);
  1709. while (buf1 < end)
  1710. if (*buf1++)
  1711. return 0;
  1712. return 1;
  1713. }
  1714. /*
  1715. multiply two decimals
  1716. SYNOPSIS
  1717. decimal_mul()
  1718. from1, from2 - factors
  1719. to - product
  1720. RETURN VALUE
  1721. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
  1722. NOTES
  1723. in this implementation, with sizeof(dec1)=4 we have DIG_PER_DEC1=9,
  1724. and 63-digit number will take only 7 dec1 words (basically a 7-digit
  1725. "base 999999999" number). Thus there's no need in fast multiplication
  1726. algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
  1727. method.
  1728. XXX if this library is to be used with huge numbers of thousands of
  1729. digits, fast multiplication must be implemented.
  1730. */
  1731. int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to)
  1732. {
  1733. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1734. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
  1735. intg0=ROUND_UP(from1->intg+from2->intg),
  1736. frac0=frac1+frac2, error, i, j, d_to_move;
  1737. dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
  1738. *start2, *stop2, *stop1, *start0, carry;
  1739. sanity(to);
  1740. i=intg0;
  1741. j=frac0;
  1742. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
  1743. to->sign=from1->sign != from2->sign;
  1744. to->frac=from1->frac+from2->frac;
  1745. to->intg=intg0*DIG_PER_DEC1;
  1746. if (unlikely(error))
  1747. {
  1748. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1749. set_if_smaller(to->intg, intg0*DIG_PER_DEC1);
  1750. if (unlikely(i > intg0))
  1751. {
  1752. i-=intg0;
  1753. j=i >> 1;
  1754. intg1-= j;
  1755. intg2-=i-j;
  1756. frac1=frac2=0; /* frac0 is already 0 here */
  1757. }
  1758. else
  1759. {
  1760. j-=frac0;
  1761. i=j >> 1;
  1762. frac1-= i;
  1763. frac2-=j-i;
  1764. }
  1765. }
  1766. start0=to->buf+intg0+frac0-1;
  1767. start2=buf2+frac2-1;
  1768. stop1=buf1-intg1;
  1769. stop2=buf2-intg2;
  1770. bzero(to->buf, (intg0+frac0)*sizeof(dec1));
  1771. for (buf1+=frac1-1; buf1 >= stop1; buf1--, start0--)
  1772. {
  1773. carry=0;
  1774. for (buf0=start0, buf2=start2; buf2 >= stop2; buf2--, buf0--)
  1775. {
  1776. dec1 hi, lo;
  1777. dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
  1778. hi=(dec1)(p/DIG_BASE);
  1779. lo=(dec1)(p-((dec2)hi)*DIG_BASE);
  1780. ADD2(*buf0, *buf0, lo, carry);
  1781. carry+=hi;
  1782. }
  1783. if (carry)
  1784. {
  1785. if (buf0 < to->buf)
  1786. return E_DEC_OVERFLOW;
  1787. ADD2(*buf0, *buf0, 0, carry);
  1788. }
  1789. for (buf0--; carry; buf0--)
  1790. {
  1791. if (buf0 < to->buf)
  1792. return E_DEC_OVERFLOW;
  1793. ADD(*buf0, *buf0, 0, carry);
  1794. }
  1795. }
  1796. /* Now we have to check for -0.000 case */
  1797. if (to->sign)
  1798. {
  1799. dec1 *buf= to->buf;
  1800. dec1 *end= to->buf + intg0 + frac0;
  1801. DBUG_ASSERT(buf != end);
  1802. for (;;)
  1803. {
  1804. if (*buf)
  1805. break;
  1806. if (++buf == end)
  1807. {
  1808. /* We got decimal zero */
  1809. decimal_make_zero(to);
  1810. break;
  1811. }
  1812. }
  1813. }
  1814. buf1= to->buf;
  1815. d_to_move= intg0 + ROUND_UP(to->frac);
  1816. while (!*buf1 && (to->intg > DIG_PER_DEC1))
  1817. {
  1818. buf1++;
  1819. to->intg-= DIG_PER_DEC1;
  1820. d_to_move--;
  1821. }
  1822. if (to->buf < buf1)
  1823. {
  1824. dec1 *cur_d= to->buf;
  1825. for (; d_to_move--; cur_d++, buf1++)
  1826. *cur_d= *buf1;
  1827. }
  1828. return error;
  1829. }
  1830. /*
  1831. naive division algorithm (Knuth's Algorithm D in 4.3.1) -
  1832. it's ok for short numbers
  1833. also we're using alloca() to allocate a temporary buffer
  1834. XXX if this library is to be used with huge numbers of thousands of
  1835. digits, fast division must be implemented and alloca should be
  1836. changed to malloc (or at least fallback to malloc if alloca() fails)
  1837. but then, decimal_mul() should be rewritten too :(
  1838. */
  1839. static int do_div_mod(decimal_t *from1, decimal_t *from2,
  1840. decimal_t *to, decimal_t *mod, int scale_incr)
  1841. {
  1842. int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
  1843. frac2=ROUND_UP(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
  1844. error, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
  1845. dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
  1846. *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
  1847. dec2 norm_factor, x, guess, y;
  1848. LINT_INIT(error);
  1849. if (mod)
  1850. to=mod;
  1851. sanity(to);
  1852. /* removing all the leading zeroes */
  1853. i= ((prec2 - 1) % DIG_PER_DEC1) + 1;
  1854. while (prec2 > 0 && *buf2 == 0)
  1855. {
  1856. prec2-= i;
  1857. i= DIG_PER_DEC1;
  1858. buf2++;
  1859. }
  1860. if (prec2 <= 0) /* short-circuit everything: from2 == 0 */
  1861. return E_DEC_DIV_ZERO;
  1862. for (i= (prec2 - 1) % DIG_PER_DEC1; *buf2 < powers10[i--]; prec2--) ;
  1863. DBUG_ASSERT(prec2 > 0);
  1864. i=((prec1-1) % DIG_PER_DEC1)+1;
  1865. while (prec1 > 0 && *buf1 == 0)
  1866. {
  1867. prec1-=i;
  1868. i=DIG_PER_DEC1;
  1869. buf1++;
  1870. }
  1871. if (prec1 <= 0)
  1872. { /* short-circuit everything: from1 == 0 */
  1873. decimal_make_zero(to);
  1874. return E_DEC_OK;
  1875. }
  1876. for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
  1877. DBUG_ASSERT(prec1 > 0);
  1878. /* let's fix scale_incr, taking into account frac1,frac2 increase */
  1879. if ((scale_incr-= frac1 - from1->frac + frac2 - from2->frac) < 0)
  1880. scale_incr=0;
  1881. dintg=(prec1-frac1)-(prec2-frac2)+(*buf1 >= *buf2);
  1882. if (dintg < 0)
  1883. {
  1884. dintg/=DIG_PER_DEC1;
  1885. intg0=0;
  1886. }
  1887. else
  1888. intg0=ROUND_UP(dintg);
  1889. if (mod)
  1890. {
  1891. /* we're calculating N1 % N2.
  1892. The result will have
  1893. frac=max(frac1, frac2), as for subtraction
  1894. intg=intg2
  1895. */
  1896. to->sign=from1->sign;
  1897. to->frac=max(from1->frac, from2->frac);
  1898. frac0=0;
  1899. }
  1900. else
  1901. {
  1902. /*
  1903. we're calculating N1/N2. N1 is in the buf1, has prec1 digits
  1904. N2 is in the buf2, has prec2 digits. Scales are frac1 and
  1905. frac2 accordingly.
  1906. Thus, the result will have
  1907. frac = ROUND_UP(frac1+frac2+scale_incr)
  1908. and
  1909. intg = (prec1-frac1) - (prec2-frac2) + 1
  1910. prec = intg+frac
  1911. */
  1912. frac0=ROUND_UP(frac1+frac2+scale_incr);
  1913. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
  1914. to->sign=from1->sign != from2->sign;
  1915. to->intg=intg0*DIG_PER_DEC1;
  1916. to->frac=frac0*DIG_PER_DEC1;
  1917. }
  1918. buf0=to->buf;
  1919. stop0=buf0+intg0+frac0;
  1920. if (likely(div_mod))
  1921. while (dintg++ < 0)
  1922. *buf0++=0;
  1923. len1=(i=ROUND_UP(prec1))+ROUND_UP(2*frac2+scale_incr+1) + 1;
  1924. set_if_bigger(len1, 3);
  1925. if (!(tmp1=(dec1 *)my_alloca(len1*sizeof(dec1))))
  1926. return E_DEC_OOM;
  1927. memcpy(tmp1, buf1, i*sizeof(dec1));
  1928. bzero(tmp1+i, (len1-i)*sizeof(dec1));
  1929. start1=tmp1;
  1930. stop1=start1+len1;
  1931. start2=buf2;
  1932. stop2=buf2+ROUND_UP(prec2)-1;
  1933. /* removing end zeroes */
  1934. while (*stop2 == 0 && stop2 >= start2)
  1935. stop2--;
  1936. len2= (int) (stop2++ - start2);
  1937. /*
  1938. calculating norm2 (normalized *start2) - we need *start2 to be large
  1939. (at least > DIG_BASE/2), but unlike Knuth's Alg. D we don't want to
  1940. normalize input numbers (as we don't make a copy of the divisor).
  1941. Thus we normalize first dec1 of buf2 only, and we'll normalize *start1
  1942. on the fly for the purpose of guesstimation only.
  1943. It's also faster, as we're saving on normalization of buf2
  1944. */
  1945. norm_factor=DIG_BASE/(*start2+1);
  1946. norm2=(dec1)(norm_factor*start2[0]);
  1947. if (likely(len2>0))
  1948. norm2+=(dec1)(norm_factor*start2[1]/DIG_BASE);
  1949. if (*start1 < *start2)
  1950. dcarry=*start1++;
  1951. else
  1952. dcarry=0;
  1953. /* main loop */
  1954. for (; buf0 < stop0; buf0++)
  1955. {
  1956. /* short-circuit, if possible */
  1957. if (unlikely(dcarry == 0 && *start1 < *start2))
  1958. guess=0;
  1959. else
  1960. {
  1961. /* D3: make a guess */
  1962. x=start1[0]+((dec2)dcarry)*DIG_BASE;
  1963. y=start1[1];
  1964. guess=(norm_factor*x+norm_factor*y/DIG_BASE)/norm2;
  1965. if (unlikely(guess >= DIG_BASE))
  1966. guess=DIG_BASE-1;
  1967. if (likely(len2>0))
  1968. {
  1969. /* hmm, this is a suspicious trick - I removed normalization here */
  1970. if (start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y)
  1971. guess--;
  1972. if (unlikely(start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y))
  1973. guess--;
  1974. DBUG_ASSERT(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
  1975. }
  1976. /* D4: multiply and subtract */
  1977. buf2=stop2;
  1978. buf1=start1+len2;
  1979. DBUG_ASSERT(buf1 < stop1);
  1980. for (carry=0; buf2 > start2; buf1--)
  1981. {
  1982. dec1 hi, lo;
  1983. x=guess * (*--buf2);
  1984. hi=(dec1)(x/DIG_BASE);
  1985. lo=(dec1)(x-((dec2)hi)*DIG_BASE);
  1986. SUB2(*buf1, *buf1, lo, carry);
  1987. carry+=hi;
  1988. }
  1989. carry= dcarry < carry;
  1990. /* D5: check the remainder */
  1991. if (unlikely(carry))
  1992. {
  1993. /* D6: correct the guess */
  1994. guess--;
  1995. buf2=stop2;
  1996. buf1=start1+len2;
  1997. for (carry=0; buf2 > start2; buf1--)
  1998. {
  1999. ADD(*buf1, *buf1, *--buf2, carry);
  2000. }
  2001. }
  2002. }
  2003. if (likely(div_mod))
  2004. *buf0=(dec1)guess;
  2005. dcarry= *start1;
  2006. start1++;
  2007. }
  2008. if (mod)
  2009. {
  2010. /*
  2011. now the result is in tmp1, it has
  2012. intg=prec1-frac1
  2013. frac=max(frac1, frac2)=to->frac
  2014. */
  2015. if (dcarry)
  2016. *--start1=dcarry;
  2017. buf0=to->buf;
  2018. intg0=(int) (ROUND_UP(prec1-frac1)-(start1-tmp1));
  2019. frac0=ROUND_UP(to->frac);
  2020. error=E_DEC_OK;
  2021. if (unlikely(frac0==0 && intg0==0))
  2022. {
  2023. decimal_make_zero(to);
  2024. goto done;
  2025. }
  2026. if (intg0<=0)
  2027. {
  2028. if (unlikely(-intg0 >= to->len))
  2029. {
  2030. decimal_make_zero(to);
  2031. error=E_DEC_TRUNCATED;
  2032. goto done;
  2033. }
  2034. stop1=start1+frac0;
  2035. frac0+=intg0;
  2036. to->intg=0;
  2037. while (intg0++ < 0)
  2038. *buf0++=0;
  2039. }
  2040. else
  2041. {
  2042. if (unlikely(intg0 > to->len))
  2043. {
  2044. frac0=0;
  2045. intg0=to->len;
  2046. error=E_DEC_OVERFLOW;
  2047. goto done;
  2048. }
  2049. DBUG_ASSERT(intg0 <= ROUND_UP(from2->intg));
  2050. stop1=start1+frac0+intg0;
  2051. to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
  2052. }
  2053. if (unlikely(intg0+frac0 > to->len))
  2054. {
  2055. stop1-=to->len-frac0-intg0;
  2056. frac0=to->len-intg0;
  2057. to->frac=frac0*DIG_PER_DEC1;
  2058. error=E_DEC_TRUNCATED;
  2059. }
  2060. while (start1 < stop1)
  2061. *buf0++=*start1++;
  2062. }
  2063. done:
  2064. my_afree(tmp1);
  2065. return error;
  2066. }
  2067. /*
  2068. division of two decimals
  2069. SYNOPSIS
  2070. decimal_div()
  2071. from1 - dividend
  2072. from2 - divisor
  2073. to - quotient
  2074. RETURN VALUE
  2075. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
  2076. NOTES
  2077. see do_div_mod()
  2078. */
  2079. int
  2080. decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to, int scale_incr)
  2081. {
  2082. return do_div_mod(from1, from2, to, 0, scale_incr);
  2083. }
  2084. /*
  2085. modulus
  2086. SYNOPSIS
  2087. decimal_mod()
  2088. from1 - dividend
  2089. from2 - divisor
  2090. to - modulus
  2091. RETURN VALUE
  2092. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
  2093. NOTES
  2094. see do_div_mod()
  2095. DESCRIPTION
  2096. the modulus R in R = M mod N
  2097. is defined as
  2098. 0 <= |R| < |M|
  2099. sign R == sign M
  2100. R = M - k*N, where k is integer
  2101. thus, there's no requirement for M or N to be integers
  2102. */
  2103. int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to)
  2104. {
  2105. return do_div_mod(from1, from2, 0, to, 0);
  2106. }
  2107. #ifdef MAIN
  2108. int full= 0;
  2109. decimal_t a, b, c;
  2110. char buf1[100], buf2[100], buf3[100];
  2111. void dump_decimal(decimal_t *d)
  2112. {
  2113. int i;
  2114. printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
  2115. for (i=0; i < ROUND_UP(d->frac)+ROUND_UP(d->intg)-1; i++)
  2116. printf("%09d, ", d->buf[i]);
  2117. printf("%09d} */ ", d->buf[i]);
  2118. }
  2119. void check_result_code(int actual, int want)
  2120. {
  2121. if (actual != want)
  2122. {
  2123. printf("\n^^^^^^^^^^^^^ must return %d\n", want);
  2124. exit(1);
  2125. }
  2126. }
  2127. void print_decimal(decimal_t *d, const char *orig, int actual, int want)
  2128. {
  2129. char s[100];
  2130. int slen=sizeof(s);
  2131. if (full) dump_decimal(d);
  2132. decimal2string(d, s, &slen, 0, 0, 0);
  2133. printf("'%s'", s);
  2134. check_result_code(actual, want);
  2135. if (orig && strcmp(orig, s))
  2136. {
  2137. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2138. exit(1);
  2139. }
  2140. }
  2141. void test_d2s()
  2142. {
  2143. char s[100];
  2144. int slen, res;
  2145. /***********************************/
  2146. printf("==== decimal2string ====\n");
  2147. a.buf[0]=12345; a.intg=5; a.frac=0; a.sign=0;
  2148. slen=sizeof(s);
  2149. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2150. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2151. a.buf[1]=987000000; a.frac=3;
  2152. slen=sizeof(s);
  2153. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2154. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2155. a.sign=1;
  2156. slen=sizeof(s);
  2157. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2158. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2159. slen=8;
  2160. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2161. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2162. slen=5;
  2163. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2164. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2165. a.buf[0]=987000000; a.frac=3; a.intg=0;
  2166. slen=sizeof(s);
  2167. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2168. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2169. }
  2170. void test_s2d(const char *s, const char *orig, int ex)
  2171. {
  2172. char s1[100], *end;
  2173. int res;
  2174. sprintf(s1, "'%s'", s);
  2175. end= strend(s);
  2176. printf("len=%2d %-30s => res=%d ", a.len, s1,
  2177. (res= string2decimal(s, &a, &end)));
  2178. print_decimal(&a, orig, res, ex);
  2179. printf("\n");
  2180. }
  2181. void test_d2f(const char *s, int ex)
  2182. {
  2183. char s1[100], *end;
  2184. double x;
  2185. int res;
  2186. sprintf(s1, "'%s'", s);
  2187. end= strend(s);
  2188. string2decimal(s, &a, &end);
  2189. res=decimal2double(&a, &x);
  2190. if (full) dump_decimal(&a);
  2191. printf("%-40s => res=%d %.*g\n", s1, res, a.intg+a.frac, x);
  2192. check_result_code(res, ex);
  2193. }
  2194. void test_d2b2d(const char *str, int p, int s, const char *orig, int ex)
  2195. {
  2196. char s1[100], buf[100], *end;
  2197. int res, i, size=decimal_bin_size(p, s);
  2198. sprintf(s1, "'%s'", str);
  2199. end= strend(str);
  2200. string2decimal(str, &a, &end);
  2201. res=decimal2bin(&a, buf, p, s);
  2202. printf("%-31s {%2d, %2d} => res=%d size=%-2d ", s1, p, s, res, size);
  2203. if (full)
  2204. {
  2205. printf("0x");
  2206. for (i=0; i < size; i++)
  2207. printf("%02x", ((uchar *)buf)[i]);
  2208. }
  2209. res=bin2decimal(buf, &a, p, s);
  2210. printf(" => res=%d ", res);
  2211. print_decimal(&a, orig, res, ex);
  2212. printf("\n");
  2213. }
  2214. void test_f2d(double from, int ex)
  2215. {
  2216. int res;
  2217. res=double2decimal(from, &a);
  2218. printf("%-40.*f => res=%d ", DBL_DIG-2, from, res);
  2219. print_decimal(&a, 0, res, ex);
  2220. printf("\n");
  2221. }
  2222. void test_ull2d(ulonglong from, const char *orig, int ex)
  2223. {
  2224. char s[100];
  2225. int res;
  2226. res=ulonglong2decimal(from, &a);
  2227. longlong10_to_str(from,s,10);
  2228. printf("%-40s => res=%d ", s, res);
  2229. print_decimal(&a, orig, res, ex);
  2230. printf("\n");
  2231. }
  2232. void test_ll2d(longlong from, const char *orig, int ex)
  2233. {
  2234. char s[100];
  2235. int res;
  2236. res=longlong2decimal(from, &a);
  2237. longlong10_to_str(from,s,-10);
  2238. printf("%-40s => res=%d ", s, res);
  2239. print_decimal(&a, orig, res, ex);
  2240. printf("\n");
  2241. }
  2242. void test_d2ull(const char *s, const char *orig, int ex)
  2243. {
  2244. char s1[100], *end;
  2245. ulonglong x;
  2246. int res;
  2247. end= strend(s);
  2248. string2decimal(s, &a, &end);
  2249. res=decimal2ulonglong(&a, &x);
  2250. if (full) dump_decimal(&a);
  2251. longlong10_to_str(x,s1,10);
  2252. printf("%-40s => res=%d %s\n", s, res, s1);
  2253. check_result_code(res, ex);
  2254. if (orig && strcmp(orig, s1))
  2255. {
  2256. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2257. exit(1);
  2258. }
  2259. }
  2260. void test_d2ll(const char *s, const char *orig, int ex)
  2261. {
  2262. char s1[100], *end;
  2263. longlong x;
  2264. int res;
  2265. end= strend(s);
  2266. string2decimal(s, &a, &end);
  2267. res=decimal2longlong(&a, &x);
  2268. if (full) dump_decimal(&a);
  2269. longlong10_to_str(x,s1,-10);
  2270. printf("%-40s => res=%d %s\n", s, res, s1);
  2271. check_result_code(res, ex);
  2272. if (orig && strcmp(orig, s1))
  2273. {
  2274. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2275. exit(1);
  2276. }
  2277. }
  2278. void test_da(const char *s1, const char *s2, const char *orig, int ex)
  2279. {
  2280. char s[100], *end;
  2281. int res;
  2282. sprintf(s, "'%s' + '%s'", s1, s2);
  2283. end= strend(s1);
  2284. string2decimal(s1, &a, &end);
  2285. end= strend(s2);
  2286. string2decimal(s2, &b, &end);
  2287. res=decimal_add(&a, &b, &c);
  2288. printf("%-40s => res=%d ", s, res);
  2289. print_decimal(&c, orig, res, ex);
  2290. printf("\n");
  2291. }
  2292. void test_ds(const char *s1, const char *s2, const char *orig, int ex)
  2293. {
  2294. char s[100], *end;
  2295. int res;
  2296. sprintf(s, "'%s' - '%s'", s1, s2);
  2297. end= strend(s1);
  2298. string2decimal(s1, &a, &end);
  2299. end= strend(s2);
  2300. string2decimal(s2, &b, &end);
  2301. res=decimal_sub(&a, &b, &c);
  2302. printf("%-40s => res=%d ", s, res);
  2303. print_decimal(&c, orig, res, ex);
  2304. printf("\n");
  2305. }
  2306. void test_dc(const char *s1, const char *s2, int orig)
  2307. {
  2308. char s[100], *end;
  2309. int res;
  2310. sprintf(s, "'%s' <=> '%s'", s1, s2);
  2311. end= strend(s1);
  2312. string2decimal(s1, &a, &end);
  2313. end= strend(s2);
  2314. string2decimal(s2, &b, &end);
  2315. res=decimal_cmp(&a, &b);
  2316. printf("%-40s => res=%d\n", s, res);
  2317. if (orig != res)
  2318. {
  2319. printf("\n^^^^^^^^^^^^^ must've been %d\n", orig);
  2320. exit(1);
  2321. }
  2322. }
  2323. void test_dm(const char *s1, const char *s2, const char *orig, int ex)
  2324. {
  2325. char s[100], *end;
  2326. int res;
  2327. sprintf(s, "'%s' * '%s'", s1, s2);
  2328. end= strend(s1);
  2329. string2decimal(s1, &a, &end);
  2330. end= strend(s2);
  2331. string2decimal(s2, &b, &end);
  2332. res=decimal_mul(&a, &b, &c);
  2333. printf("%-40s => res=%d ", s, res);
  2334. print_decimal(&c, orig, res, ex);
  2335. printf("\n");
  2336. }
  2337. void test_dv(const char *s1, const char *s2, const char *orig, int ex)
  2338. {
  2339. char s[100], *end;
  2340. int res;
  2341. sprintf(s, "'%s' / '%s'", s1, s2);
  2342. end= strend(s1);
  2343. string2decimal(s1, &a, &end);
  2344. end= strend(s2);
  2345. string2decimal(s2, &b, &end);
  2346. res=decimal_div(&a, &b, &c, 5);
  2347. printf("%-40s => res=%d ", s, res);
  2348. check_result_code(res, ex);
  2349. if (res == E_DEC_DIV_ZERO)
  2350. printf("E_DEC_DIV_ZERO");
  2351. else
  2352. print_decimal(&c, orig, res, ex);
  2353. printf("\n");
  2354. }
  2355. void test_md(const char *s1, const char *s2, const char *orig, int ex)
  2356. {
  2357. char s[100], *end;
  2358. int res;
  2359. sprintf(s, "'%s' %% '%s'", s1, s2);
  2360. end= strend(s1);
  2361. string2decimal(s1, &a, &end);
  2362. end= strend(s2);
  2363. string2decimal(s2, &b, &end);
  2364. res=decimal_mod(&a, &b, &c);
  2365. printf("%-40s => res=%d ", s, res);
  2366. check_result_code(res, ex);
  2367. if (res == E_DEC_DIV_ZERO)
  2368. printf("E_DEC_DIV_ZERO");
  2369. else
  2370. print_decimal(&c, orig, res, ex);
  2371. printf("\n");
  2372. }
  2373. const char *round_mode[]=
  2374. {"TRUNCATE", "HALF_EVEN", "HALF_UP", "CEILING", "FLOOR"};
  2375. void test_ro(const char *s1, int n, decimal_round_mode mode, const char *orig,
  2376. int ex)
  2377. {
  2378. char s[100], *end;
  2379. int res;
  2380. sprintf(s, "'%s', %d, %s", s1, n, round_mode[mode]);
  2381. end= strend(s1);
  2382. string2decimal(s1, &a, &end);
  2383. res=decimal_round(&a, &b, n, mode);
  2384. printf("%-40s => res=%d ", s, res);
  2385. print_decimal(&b, orig, res, ex);
  2386. printf("\n");
  2387. }
  2388. void test_mx(int precision, int frac, const char *orig)
  2389. {
  2390. char s[100];
  2391. sprintf(s, "%d, %d", precision, frac);
  2392. max_decimal(precision, frac, &a);
  2393. printf("%-40s => ", s);
  2394. print_decimal(&a, orig, 0, 0);
  2395. printf("\n");
  2396. }
  2397. void test_pr(const char *s1, int prec, int dec, char filler, const char *orig,
  2398. int ex)
  2399. {
  2400. char s[100], *end;
  2401. char s2[100];
  2402. int slen= sizeof(s2);
  2403. int res;
  2404. sprintf(s, filler ? "'%s', %d, %d, '%c'" : "'%s', %d, %d, '\\0'",
  2405. s1, prec, dec, filler);
  2406. end= strend(s1);
  2407. string2decimal(s1, &a, &end);
  2408. res= decimal2string(&a, s2, &slen, prec, dec, filler);
  2409. printf("%-40s => res=%d '%s'", s, res, s2);
  2410. check_result_code(res, ex);
  2411. if (orig && strcmp(orig, s2))
  2412. {
  2413. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2414. exit(1);
  2415. }
  2416. printf("\n");
  2417. }
  2418. void test_sh(const char *s1, int shift, const char *orig, int ex)
  2419. {
  2420. char s[100], *end;
  2421. int res;
  2422. sprintf(s, "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift));
  2423. end= strend(s1);
  2424. string2decimal(s1, &a, &end);
  2425. res= decimal_shift(&a, shift);
  2426. printf("%-40s => res=%d ", s, res);
  2427. print_decimal(&a, orig, res, ex);
  2428. printf("\n");
  2429. }
  2430. void test_fr(const char *s1, const char *orig)
  2431. {
  2432. char s[100], *end;
  2433. sprintf(s, "'%s'", s1);
  2434. printf("%-40s => ", s);
  2435. end= strend(s1);
  2436. string2decimal(s1, &a, &end);
  2437. a.frac= decimal_actual_fraction(&a);
  2438. print_decimal(&a, orig, 0, 0);
  2439. printf("\n");
  2440. }
  2441. int main()
  2442. {
  2443. a.buf=(void*)buf1;
  2444. a.len=sizeof(buf1)/sizeof(dec1);
  2445. b.buf=(void*)buf2;
  2446. b.len=sizeof(buf2)/sizeof(dec1);
  2447. c.buf=(void*)buf3;
  2448. c.len=sizeof(buf3)/sizeof(dec1);
  2449. if (full)
  2450. test_d2s();
  2451. printf("==== string2decimal ====\n");
  2452. test_s2d("12345", "12345", 0);
  2453. test_s2d("12345.", "12345", 0);
  2454. test_s2d("123.45", "123.45", 0);
  2455. test_s2d("-123.45", "-123.45", 0);
  2456. test_s2d(".00012345000098765", "0.00012345000098765", 0);
  2457. test_s2d(".12345000098765", "0.12345000098765", 0);
  2458. test_s2d("-.000000012345000098765", "-0.000000012345000098765", 0);
  2459. test_s2d("1234500009876.5", "1234500009876.5", 0);
  2460. a.len=1;
  2461. test_s2d("123450000098765", "98765", 2);
  2462. test_s2d("123450.000098765", "123450", 1);
  2463. a.len=sizeof(buf1)/sizeof(dec1);
  2464. test_s2d("123E5", "12300000", 0);
  2465. test_s2d("123E-2", "1.23", 0);
  2466. printf("==== decimal2double ====\n");
  2467. test_d2f("12345", 0);
  2468. test_d2f("123.45", 0);
  2469. test_d2f("-123.45", 0);
  2470. test_d2f("0.00012345000098765", 0);
  2471. test_d2f("1234500009876.5", 0);
  2472. printf("==== double2decimal ====\n");
  2473. test_f2d(12345, 0);
  2474. test_f2d(1.0/3, 0);
  2475. test_f2d(-123.45, 0);
  2476. test_f2d(0.00012345000098765, 0);
  2477. test_f2d(1234500009876.5, 0);
  2478. printf("==== ulonglong2decimal ====\n");
  2479. test_ull2d(ULL(12345), "12345", 0);
  2480. test_ull2d(ULL(0), "0", 0);
  2481. test_ull2d(ULL(18446744073709551615), "18446744073709551615", 0);
  2482. printf("==== decimal2ulonglong ====\n");
  2483. test_d2ull("12345", "12345", 0);
  2484. test_d2ull("0", "0", 0);
  2485. test_d2ull("18446744073709551615", "18446744073709551615", 0);
  2486. test_d2ull("18446744073709551616", "18446744073", 2);
  2487. test_d2ull("-1", "0", 2);
  2488. test_d2ull("1.23", "1", 1);
  2489. test_d2ull("9999999999999999999999999.000", "9999999999999999", 2);
  2490. printf("==== longlong2decimal ====\n");
  2491. test_ll2d(LL(-12345), "-12345", 0);
  2492. test_ll2d(LL(-1), "-1", 0);
  2493. test_ll2d(LL(-9223372036854775807), "-9223372036854775807", 0);
  2494. test_ll2d(ULL(9223372036854775808), "-9223372036854775808", 0);
  2495. printf("==== decimal2longlong ====\n");
  2496. test_d2ll("18446744073709551615", "18446744073", 2);
  2497. test_d2ll("-1", "-1", 0);
  2498. test_d2ll("-1.23", "-1", 1);
  2499. test_d2ll("-9223372036854775807", "-9223372036854775807", 0);
  2500. test_d2ll("-9223372036854775808", "-9223372036854775808", 0);
  2501. test_d2ll("9223372036854775808", "9223372036854775807", 2);
  2502. printf("==== do_add ====\n");
  2503. test_da(".00012345000098765" ,"123.45", "123.45012345000098765", 0);
  2504. test_da(".1" ,".45", "0.55", 0);
  2505. test_da("1234500009876.5" ,".00012345000098765", "1234500009876.50012345000098765", 0);
  2506. test_da("9999909999999.5" ,".555", "9999910000000.055", 0);
  2507. test_da("99999999" ,"1", "100000000", 0);
  2508. test_da("989999999" ,"1", "990000000", 0);
  2509. test_da("999999999" ,"1", "1000000000", 0);
  2510. test_da("12345" ,"123.45", "12468.45", 0);
  2511. test_da("-12345" ,"-123.45", "-12468.45", 0);
  2512. test_ds("-12345" ,"123.45", "-12468.45", 0);
  2513. test_ds("12345" ,"-123.45", "12468.45", 0);
  2514. printf("==== do_sub ====\n");
  2515. test_ds(".00012345000098765", "123.45","-123.44987654999901235", 0);
  2516. test_ds("1234500009876.5", ".00012345000098765","1234500009876.49987654999901235", 0);
  2517. test_ds("9999900000000.5", ".555","9999899999999.945", 0);
  2518. test_ds("1111.5551", "1111.555","0.0001", 0);
  2519. test_ds(".555", ".555","0", 0);
  2520. test_ds("10000000", "1","9999999", 0);
  2521. test_ds("1000001000", ".1","1000000999.9", 0);
  2522. test_ds("1000000000", ".1","999999999.9", 0);
  2523. test_ds("12345", "123.45","12221.55", 0);
  2524. test_ds("-12345", "-123.45","-12221.55", 0);
  2525. test_da("-12345", "123.45","-12221.55", 0);
  2526. test_da("12345", "-123.45","12221.55", 0);
  2527. test_ds("123.45", "12345","-12221.55", 0);
  2528. test_ds("-123.45", "-12345","12221.55", 0);
  2529. test_da("123.45", "-12345","-12221.55", 0);
  2530. test_da("-123.45", "12345","12221.55", 0);
  2531. test_da("5", "-6.0","-1.0", 0);
  2532. printf("==== decimal_mul ====\n");
  2533. test_dm("12", "10","120", 0);
  2534. test_dm("-123.456", "98765.4321","-12193185.1853376", 0);
  2535. test_dm("-123456000000", "98765432100000","-12193185185337600000000000", 0);
  2536. test_dm("123456", "987654321","121931851853376", 0);
  2537. test_dm("123456", "9876543210","1219318518533760", 0);
  2538. test_dm("123", "0.01","1.23", 0);
  2539. test_dm("123", "0","0", 0);
  2540. printf("==== decimal_div ====\n");
  2541. test_dv("120", "10","12.000000000", 0);
  2542. test_dv("123", "0.01","12300.000000000", 0);
  2543. test_dv("120", "100000000000.00000","0.000000001200000000", 0);
  2544. test_dv("123", "0","", 4);
  2545. test_dv("0", "0", "", 4);
  2546. test_dv("-12193185.1853376", "98765.4321","-123.456000000000000000", 0);
  2547. test_dv("121931851853376", "987654321","123456.000000000", 0);
  2548. test_dv("0", "987","0", 0);
  2549. test_dv("1", "3","0.333333333", 0);
  2550. test_dv("1.000000000000", "3","0.333333333333333333", 0);
  2551. test_dv("1", "1","1.000000000", 0);
  2552. test_dv("0.0123456789012345678912345", "9999999999","0.000000000001234567890246913578148141", 0);
  2553. test_dv("10.333000000", "12.34500","0.837019036046982584042122316", 0);
  2554. test_dv("10.000000000060", "2","5.000000000030000000", 0);
  2555. printf("==== decimal_mod ====\n");
  2556. test_md("234","10","4", 0);
  2557. test_md("234.567","10.555","2.357", 0);
  2558. test_md("-234.567","10.555","-2.357", 0);
  2559. test_md("234.567","-10.555","2.357", 0);
  2560. c.buf[1]=0x3ABECA;
  2561. test_md("99999999999999999999999999999999999999","3","0", 0);
  2562. if (c.buf[1] != 0x3ABECA)
  2563. {
  2564. printf("%X - overflow\n", c.buf[1]);
  2565. exit(1);
  2566. }
  2567. printf("==== decimal2bin/bin2decimal ====\n");
  2568. test_d2b2d("-10.55", 4, 2,"-10.55", 0);
  2569. test_d2b2d("0.0123456789012345678912345", 30, 25,"0.0123456789012345678912345", 0);
  2570. test_d2b2d("12345", 5, 0,"12345", 0);
  2571. test_d2b2d("12345", 10, 3,"12345.000", 0);
  2572. test_d2b2d("123.45", 10, 3,"123.450", 0);
  2573. test_d2b2d("-123.45", 20, 10,"-123.4500000000", 0);
  2574. test_d2b2d(".00012345000098765", 15, 14,"0.00012345000098", 0);
  2575. test_d2b2d(".00012345000098765", 22, 20,"0.00012345000098765000", 0);
  2576. test_d2b2d(".12345000098765", 30, 20,"0.12345000098765000000", 0);
  2577. test_d2b2d("-.000000012345000098765", 30, 20,"-0.00000001234500009876", 0);
  2578. test_d2b2d("1234500009876.5", 30, 5,"1234500009876.50000", 0);
  2579. test_d2b2d("111111111.11", 10, 2,"11111111.11", 0);
  2580. test_d2b2d("000000000.01", 7, 3,"0.010", 0);
  2581. test_d2b2d("123.4", 10, 2, "123.40", 0);
  2582. printf("==== decimal_cmp ====\n");
  2583. test_dc("12","13",-1);
  2584. test_dc("13","12",1);
  2585. test_dc("-10","10",-1);
  2586. test_dc("10","-10",1);
  2587. test_dc("-12","-13",1);
  2588. test_dc("0","12",-1);
  2589. test_dc("-10","0",-1);
  2590. test_dc("4","4",0);
  2591. printf("==== decimal_round ====\n");
  2592. test_ro("5678.123451",-4,TRUNCATE,"0", 0);
  2593. test_ro("5678.123451",-3,TRUNCATE,"5000", 0);
  2594. test_ro("5678.123451",-2,TRUNCATE,"5600", 0);
  2595. test_ro("5678.123451",-1,TRUNCATE,"5670", 0);
  2596. test_ro("5678.123451",0,TRUNCATE,"5678", 0);
  2597. test_ro("5678.123451",1,TRUNCATE,"5678.1", 0);
  2598. test_ro("5678.123451",2,TRUNCATE,"5678.12", 0);
  2599. test_ro("5678.123451",3,TRUNCATE,"5678.123", 0);
  2600. test_ro("5678.123451",4,TRUNCATE,"5678.1234", 0);
  2601. test_ro("5678.123451",5,TRUNCATE,"5678.12345", 0);
  2602. test_ro("5678.123451",6,TRUNCATE,"5678.123451", 0);
  2603. test_ro("-5678.123451",-4,TRUNCATE,"0", 0);
  2604. memset(buf2, 33, sizeof(buf2));
  2605. test_ro("99999999999999999999999999999999999999",-31,TRUNCATE,"99999990000000000000000000000000000000", 0);
  2606. test_ro("15.1",0,HALF_UP,"15", 0);
  2607. test_ro("15.5",0,HALF_UP,"16", 0);
  2608. test_ro("15.9",0,HALF_UP,"16", 0);
  2609. test_ro("-15.1",0,HALF_UP,"-15", 0);
  2610. test_ro("-15.5",0,HALF_UP,"-16", 0);
  2611. test_ro("-15.9",0,HALF_UP,"-16", 0);
  2612. test_ro("15.1",1,HALF_UP,"15.1", 0);
  2613. test_ro("-15.1",1,HALF_UP,"-15.1", 0);
  2614. test_ro("15.17",1,HALF_UP,"15.2", 0);
  2615. test_ro("15.4",-1,HALF_UP,"20", 0);
  2616. test_ro("-15.4",-1,HALF_UP,"-20", 0);
  2617. test_ro("5.4",-1,HALF_UP,"10", 0);
  2618. test_ro(".999", 0, HALF_UP, "1", 0);
  2619. memset(buf2, 33, sizeof(buf2));
  2620. test_ro("999999999", -9, HALF_UP, "1000000000", 0);
  2621. test_ro("15.1",0,HALF_EVEN,"15", 0);
  2622. test_ro("15.5",0,HALF_EVEN,"16", 0);
  2623. test_ro("14.5",0,HALF_EVEN,"14", 0);
  2624. test_ro("15.9",0,HALF_EVEN,"16", 0);
  2625. test_ro("15.1",0,CEILING,"16", 0);
  2626. test_ro("-15.1",0,CEILING,"-15", 0);
  2627. test_ro("15.1",0,FLOOR,"15", 0);
  2628. test_ro("-15.1",0,FLOOR,"-16", 0);
  2629. test_ro("999999999999999999999.999", 0, CEILING,"1000000000000000000000", 0);
  2630. test_ro("-999999999999999999999.999", 0, FLOOR,"-1000000000000000000000", 0);
  2631. b.buf[0]=DIG_BASE+1;
  2632. b.buf++;
  2633. test_ro(".3", 0, HALF_UP, "0", 0);
  2634. b.buf--;
  2635. if (b.buf[0] != DIG_BASE+1)
  2636. {
  2637. printf("%d - underflow\n", b.buf[0]);
  2638. exit(1);
  2639. }
  2640. printf("==== max_decimal ====\n");
  2641. test_mx(1,1,"0.9");
  2642. test_mx(1,0,"9");
  2643. test_mx(2,1,"9.9");
  2644. test_mx(4,2,"99.99");
  2645. test_mx(6,3,"999.999");
  2646. test_mx(8,4,"9999.9999");
  2647. test_mx(10,5,"99999.99999");
  2648. test_mx(12,6,"999999.999999");
  2649. test_mx(14,7,"9999999.9999999");
  2650. test_mx(16,8,"99999999.99999999");
  2651. test_mx(18,9,"999999999.999999999");
  2652. test_mx(20,10,"9999999999.9999999999");
  2653. test_mx(20,20,"0.99999999999999999999");
  2654. test_mx(20,0,"99999999999999999999");
  2655. test_mx(40,20,"99999999999999999999.99999999999999999999");
  2656. printf("==== decimal2string ====\n");
  2657. test_pr("123.123", 0, 0, 0, "123.123", 0);
  2658. test_pr("123.123", 7, 3, '0', "123.123", 0);
  2659. test_pr("123.123", 9, 3, '0', "00123.123", 0);
  2660. test_pr("123.123", 9, 4, '0', "0123.1230", 0);
  2661. test_pr("123.123", 9, 5, '0', "123.12300", 0);
  2662. test_pr("123.123", 9, 2, '0', "000123.12", 1);
  2663. test_pr("123.123", 9, 6, '0', "23.123000", 2);
  2664. printf("==== decimal_shift ====\n");
  2665. test_sh("123.123", 1, "1231.23", 0);
  2666. test_sh("123457189.123123456789000", 1, "1234571891.23123456789", 0);
  2667. test_sh("123457189.123123456789000", 4, "1234571891231.23456789", 0);
  2668. test_sh("123457189.123123456789000", 8, "12345718912312345.6789", 0);
  2669. test_sh("123457189.123123456789000", 9, "123457189123123456.789", 0);
  2670. test_sh("123457189.123123456789000", 10, "1234571891231234567.89", 0);
  2671. test_sh("123457189.123123456789000", 17, "12345718912312345678900000", 0);
  2672. test_sh("123457189.123123456789000", 18, "123457189123123456789000000", 0);
  2673. test_sh("123457189.123123456789000", 19, "1234571891231234567890000000", 0);
  2674. test_sh("123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
  2675. test_sh("123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
  2676. test_sh("123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
  2677. test_sh("000000000000000000000000123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
  2678. test_sh("00000000123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
  2679. test_sh("00000000000000000123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
  2680. test_sh("123", 1, "1230", 0);
  2681. test_sh("123", 10, "1230000000000", 0);
  2682. test_sh(".123", 1, "1.23", 0);
  2683. test_sh(".123", 10, "1230000000", 0);
  2684. test_sh(".123", 14, "12300000000000", 0);
  2685. test_sh("000.000", 1000, "0", 0);
  2686. test_sh("000.", 1000, "0", 0);
  2687. test_sh(".000", 1000, "0", 0);
  2688. test_sh("1", 1000, "1", 2);
  2689. test_sh("123.123", -1, "12.3123", 0);
  2690. test_sh("123987654321.123456789000", -1, "12398765432.1123456789", 0);
  2691. test_sh("123987654321.123456789000", -2, "1239876543.21123456789", 0);
  2692. test_sh("123987654321.123456789000", -3, "123987654.321123456789", 0);
  2693. test_sh("123987654321.123456789000", -8, "1239.87654321123456789", 0);
  2694. test_sh("123987654321.123456789000", -9, "123.987654321123456789", 0);
  2695. test_sh("123987654321.123456789000", -10, "12.3987654321123456789", 0);
  2696. test_sh("123987654321.123456789000", -11, "1.23987654321123456789", 0);
  2697. test_sh("123987654321.123456789000", -12, "0.123987654321123456789", 0);
  2698. test_sh("123987654321.123456789000", -13, "0.0123987654321123456789", 0);
  2699. test_sh("123987654321.123456789000", -14, "0.00123987654321123456789", 0);
  2700. test_sh("00000087654321.123456789000", -14, "0.00000087654321123456789", 0);
  2701. a.len= 2;
  2702. test_sh("123.123", -2, "1.23123", 0);
  2703. test_sh("123.123", -3, "0.123123", 0);
  2704. test_sh("123.123", -6, "0.000123123", 0);
  2705. test_sh("123.123", -7, "0.0000123123", 0);
  2706. test_sh("123.123", -15, "0.000000000000123123", 0);
  2707. test_sh("123.123", -16, "0.000000000000012312", 1);
  2708. test_sh("123.123", -17, "0.000000000000001231", 1);
  2709. test_sh("123.123", -18, "0.000000000000000123", 1);
  2710. test_sh("123.123", -19, "0.000000000000000012", 1);
  2711. test_sh("123.123", -20, "0.000000000000000001", 1);
  2712. test_sh("123.123", -21, "0", 1);
  2713. test_sh(".000000000123", -1, "0.0000000000123", 0);
  2714. test_sh(".000000000123", -6, "0.000000000000000123", 0);
  2715. test_sh(".000000000123", -7, "0.000000000000000012", 1);
  2716. test_sh(".000000000123", -8, "0.000000000000000001", 1);
  2717. test_sh(".000000000123", -9, "0", 1);
  2718. test_sh(".000000000123", 1, "0.00000000123", 0);
  2719. test_sh(".000000000123", 8, "0.0123", 0);
  2720. test_sh(".000000000123", 9, "0.123", 0);
  2721. test_sh(".000000000123", 10, "1.23", 0);
  2722. test_sh(".000000000123", 17, "12300000", 0);
  2723. test_sh(".000000000123", 18, "123000000", 0);
  2724. test_sh(".000000000123", 19, "1230000000", 0);
  2725. test_sh(".000000000123", 20, "12300000000", 0);
  2726. test_sh(".000000000123", 21, "123000000000", 0);
  2727. test_sh(".000000000123", 22, "1230000000000", 0);
  2728. test_sh(".000000000123", 23, "12300000000000", 0);
  2729. test_sh(".000000000123", 24, "123000000000000", 0);
  2730. test_sh(".000000000123", 25, "1230000000000000", 0);
  2731. test_sh(".000000000123", 26, "12300000000000000", 0);
  2732. test_sh(".000000000123", 27, "123000000000000000", 0);
  2733. test_sh(".000000000123", 28, "0.000000000123", 2);
  2734. test_sh("123456789.987654321", -1, "12345678.998765432", 1);
  2735. test_sh("123456789.987654321", -2, "1234567.899876543", 1);
  2736. test_sh("123456789.987654321", -8, "1.234567900", 1);
  2737. test_sh("123456789.987654321", -9, "0.123456789987654321", 0);
  2738. test_sh("123456789.987654321", -10, "0.012345678998765432", 1);
  2739. test_sh("123456789.987654321", -17, "0.000000001234567900", 1);
  2740. test_sh("123456789.987654321", -18, "0.000000000123456790", 1);
  2741. test_sh("123456789.987654321", -19, "0.000000000012345679", 1);
  2742. test_sh("123456789.987654321", -26, "0.000000000000000001", 1);
  2743. test_sh("123456789.987654321", -27, "0", 1);
  2744. test_sh("123456789.987654321", 1, "1234567900", 1);
  2745. test_sh("123456789.987654321", 2, "12345678999", 1);
  2746. test_sh("123456789.987654321", 4, "1234567899877", 1);
  2747. test_sh("123456789.987654321", 8, "12345678998765432", 1);
  2748. test_sh("123456789.987654321", 9, "123456789987654321", 0);
  2749. test_sh("123456789.987654321", 10, "123456789.987654321", 2);
  2750. test_sh("123456789.987654321", 0, "123456789.987654321", 0);
  2751. a.len= sizeof(buf1)/sizeof(dec1);
  2752. printf("==== decimal_actual_fraction ====\n");
  2753. test_fr("1.123456789000000000", "1.123456789");
  2754. test_fr("1.12345678000000000", "1.12345678");
  2755. test_fr("1.1234567000000000", "1.1234567");
  2756. test_fr("1.123456000000000", "1.123456");
  2757. test_fr("1.12345000000000", "1.12345");
  2758. test_fr("1.1234000000000", "1.1234");
  2759. test_fr("1.123000000000", "1.123");
  2760. test_fr("1.12000000000", "1.12");
  2761. test_fr("1.1000000000", "1.1");
  2762. test_fr("1.000000000", "1");
  2763. test_fr("1.0", "1");
  2764. test_fr("10000000000000000000.0", "10000000000000000000");
  2765. return 0;
  2766. }
  2767. #endif