~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/decimal.cc

  • Committer: Stewart Smith
  • Date: 2009-03-11 06:37:19 UTC
  • mto: (910.4.19 sparc) (937.2.1 sparc)
  • mto: This revision was merged to the branch mainline in revision 931.
  • Revision ID: stewart@flamingspork.com-20090311063719-v9iqjd00ts6260vv
batch up more INSERTs into transactions to help tests run quicker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
/** @file
17
 
 *
18
 
 * @brief  SQL standard-compliant decimal number handling
19
 
 *
20
 
 * @note
21
 
 * This library implements SQL standard "exact numeric" type
22
 
 * and is not at all generic, but rather intentinally crippled to
23
 
 * follow the standard :) 
24
 
 */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
25
15
 
26
16
/*
27
17
=======================================================================
 
18
  NOTE: this library implements SQL standard "exact numeric" type
 
19
  and is not at all generic, but rather intentinally crippled to
 
20
  follow the standard :)
 
21
=======================================================================
28
22
  Quoting the standard
29
23
  (SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
30
24
 
103
97
      implementation-defined.
104
98
*/
105
99
 
106
 
#include "config.h"
107
 
 
108
 
#include "drizzled/definitions.h"
109
 
#include "drizzled/internal/m_string.h"
110
 
#include "drizzled/charset_info.h"
111
 
#include "drizzled/type/decimal.h"
112
 
 
113
 
#include <plugin/myisam/myisampack.h>
 
100
#include <drizzled/global.h>
 
101
 
 
102
#include "m_string.h"
 
103
#include "m_ctype.h"
 
104
#include "decimal.h"
 
105
 
 
106
#include <storage/myisam/myisampack.h>
114
107
#include <drizzled/util/test.h>
115
108
 
116
 
#ifdef HAVE_ALLOCA_H
117
109
#include <alloca.h>
118
 
#endif
119
 
 
120
 
#include <algorithm>
121
 
#include <time.h>
122
 
#include "drizzled/current_session.h"
123
 
#include "drizzled/error.h"
124
 
#include "drizzled/field.h"
125
 
#include "drizzled/internal/my_sys.h"
126
 
 
127
 
using namespace std;
128
 
 
129
 
namespace drizzled
130
 
{
131
 
/**
132
 
  report result of decimal operation.
133
 
 
134
 
  @param result  decimal library return code (E_DEC_* see include/decimal.h)
135
 
 
136
 
  @todo
137
 
    Fix error messages
138
 
 
139
 
  @return
140
 
    result
141
 
*/
142
 
 
143
 
int decimal_operation_results(int result)
144
 
{
145
 
  switch (result) {
146
 
  case E_DEC_OK:
147
 
    break;
148
 
  case E_DEC_TRUNCATED:
149
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
150
 
                        ER_WARN_DATA_TRUNCATED, ER(ER_WARN_DATA_TRUNCATED),
151
 
                        "", (long)-1);
152
 
    break;
153
 
  case E_DEC_OVERFLOW:
154
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
155
 
                        ER_TRUNCATED_WRONG_VALUE,
156
 
                        ER(ER_TRUNCATED_WRONG_VALUE),
157
 
                        "DECIMAL", "");
158
 
    break;
159
 
  case E_DEC_DIV_ZERO:
160
 
    my_error(ER_DIVISION_BY_ZERO, MYF(0));
161
 
    break;
162
 
  case E_DEC_BAD_NUM:
163
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
164
 
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
165
 
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
166
 
                        "decimal", "", "", (long)-1);
167
 
    break;
168
 
  case E_DEC_OOM:
169
 
    my_error(ER_OUT_OF_RESOURCES, MYF(0));
170
 
    break;
171
 
  default:
172
 
    assert(0);
173
 
  }
174
 
  return result;
175
 
}
176
 
 
177
 
 
178
 
/**
179
 
  @brief Converting decimal to string
180
 
 
181
 
  @details Convert given type::Decimal to String; allocate buffer as needed.
182
 
 
183
 
  @param[in]   mask        what problems to warn on (mask of E_DEC_* values)
184
 
  @param[in]   d           the decimal to print
185
 
  @param[in]   fixed_prec  overall number of digits if ZEROFILL, 0 otherwise
186
 
  @param[in]   fixed_dec   number of decimal places (if fixed_prec != 0)
187
 
  @param[in]   filler      what char to pad with (ZEROFILL et al.)
188
 
  @param[out]  *str        where to store the resulting string
189
 
 
190
 
  @return error code
191
 
    @retval E_DEC_OK
192
 
    @retval E_DEC_TRUNCATED
193
 
    @retval E_DEC_OVERFLOW
194
 
    @retval E_DEC_OOM
195
 
*/
196
 
 
197
 
int class_decimal2string(const type::Decimal *d,
198
 
                         uint32_t fixed_dec, String *str)
199
 
{
200
 
  uint32_t mask= E_DEC_FATAL_ERROR;
201
 
 
202
 
  /*
203
 
    Calculate the size of the string: For DECIMAL(a,b), fixed_prec==a
204
 
    holds true iff the type is also ZEROFILL, which in turn implies
205
 
    UNSIGNED. Hence the buffer for a ZEROFILLed value is the length
206
 
    the user requested, plus one for a possible decimal point, plus
207
 
    one if the user only wanted decimal places, but we force a leading
208
 
    zero on them. Because the type is implicitly UNSIGNED, we do not
209
 
    need to reserve a character for the sign. For all other cases,
210
 
    fixed_prec will be 0, and class_decimal_string_length() will be called
211
 
    instead to calculate the required size of the buffer.
212
 
  */
213
 
  int length= (int)(0
214
 
                    ? (uint32_t)(((0 == fixed_dec) ? 1 : 0) + 1)
215
 
                    : (uint32_t)d->string_length());
216
 
  int result;
217
 
  if (str->alloc(length))
218
 
    return check_result(mask, E_DEC_OOM);
219
 
 
220
 
  result= decimal2string((decimal_t*) d, (char*) str->ptr(),
221
 
                         &length, (int)0, fixed_dec,
222
 
                         '0');
223
 
  str->length(length);
224
 
  return check_result(mask, result);
225
 
}
226
 
 
227
 
 
228
 
/**
229
 
  @brief  Convert from decimal to binary representation
230
 
 
231
 
  @param[in]   mask        error processing mask
232
 
  @param[in]   d           number for conversion
233
 
  @param[out]  bin         pointer to buffer where to write result
234
 
  @param[in]   prec        overall number of decimal digits
235
 
  @param[in]   scale       number of decimal digits after decimal point
236
 
 
237
 
  @note
238
 
    Before conversion we round number if it need but produce truncation
239
 
    error in this case
240
 
 
241
 
  @return error code
242
 
   @retval E_DEC_OK
243
 
   @retval E_DEC_TRUNCATED
244
 
   @retval E_DEC_OVERFLOW
245
 
*/
246
 
 
247
 
namespace type {
248
 
 
249
 
int Decimal::val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const
250
 
{
251
 
  int err1= E_DEC_OK, err2;
252
 
  type::Decimal rounded;
253
 
  class_decimal2decimal(this, &rounded);
254
 
  rounded.frac= decimal_actual_fraction(&rounded);
255
 
  if (scale < rounded.frac)
256
 
  {
257
 
    err1= E_DEC_TRUNCATED;
258
 
    /* decimal_round can return only E_DEC_TRUNCATED */
259
 
    decimal_round(&rounded, &rounded, scale, HALF_UP);
260
 
  }
261
 
  err2= decimal2bin(&rounded, bin, prec, scale);
262
 
  if (!err2)
263
 
    err2= err1;
264
 
  return check_result(mask, err2);
265
 
}
266
 
 
267
 
} // namespace type
268
 
 
269
 
 
270
 
/**
271
 
  @brief Convert string for decimal when string can be in some multibyte charset
272
 
 
273
 
  @param  mask            error processing mask
274
 
  @param  from            string to process
275
 
  @param  length          length of given string
276
 
  @param  charset         charset of given string
277
 
 
278
 
  @return Error code
279
 
   @retval E_DEC_OK
280
 
   @retval E_DEC_TRUNCATED
281
 
   @retval E_DEC_OVERFLOW
282
 
   @retval E_DEC_BAD_NUM
283
 
   @retval E_DEC_OOM
284
 
*/
285
 
 
286
 
int type::Decimal::store(uint32_t mask, const char *from, uint32_t length, const CHARSET_INFO * charset)
287
 
{
288
 
  char *end, *from_end;
289
 
  int err;
290
 
  char buff[STRING_BUFFER_USUAL_SIZE];
291
 
  String tmp(buff, sizeof(buff), &my_charset_bin);
292
 
  if (charset->mbminlen > 1)
293
 
  {
294
 
    size_t dummy_errors;
295
 
    tmp.copy(from, length, charset, &my_charset_utf8_general_ci, &dummy_errors);
296
 
    from= tmp.ptr();
297
 
    length=  tmp.length();
298
 
    charset= &my_charset_bin;
299
 
  }
300
 
  from_end= end= (char*) from+length;
301
 
  err= string2decimal((char *)from, (decimal_t*) this, &end);
302
 
  if (end != from_end && !err)
303
 
  {
304
 
    /* Give warning if there is something other than end space */
305
 
    for ( ; end < from_end; end++)
306
 
    {
307
 
      if (!my_isspace(&my_charset_utf8_general_ci, *end))
308
 
      {
309
 
        err= E_DEC_TRUNCATED;
310
 
        break;
311
 
      }
312
 
    }
313
 
  }
314
 
  check_result_and_overflow(mask, err);
315
 
  return err;
316
 
}
317
 
 
318
 
void type::Decimal::convert(double &result) const
319
 
{
320
 
  decimal2double(static_cast<const decimal_t*>(this), &result);
321
 
}
322
 
 
323
 
type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec)
324
 
{
325
 
  int64_t date;
326
 
  date = (ltime->year*100L + ltime->month)*100L + ltime->day;
327
 
  if (ltime->time_type > type::DRIZZLE_TIMESTAMP_DATE)
328
 
    date= ((date*100L + ltime->hour)*100L+ ltime->minute)*100L + ltime->second;
329
 
 
330
 
  if (int2_class_decimal(E_DEC_FATAL_ERROR, date, false, dec))
331
 
    return dec;
332
 
 
333
 
  if (ltime->second_part)
334
 
  {
335
 
    dec->buf[(dec->intg-1) / 9 + 1]= ltime->second_part * 1000;
336
 
    dec->frac= 6;
337
 
  }
338
 
 
339
 
  return dec;
340
 
}
341
 
 
342
 
 
343
 
void class_decimal_trim(uint32_t *precision, uint32_t *scale)
344
 
{
345
 
  if (!(*precision) && !(*scale))
346
 
  {
347
 
    *precision= 10;
348
 
    *scale= 0;
349
 
    return;
350
 
  }
351
 
}
352
 
 
353
 
 
354
110
/*
355
111
  Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
356
112
  So one variable of type decimal_digit_t is limited:
373
129
#define DIG_MASK     100000000
374
130
#define DIG_BASE     1000000000
375
131
#define DIG_MAX      (DIG_BASE-1)
376
 
 
377
 
template<typename T> 
378
 
inline static T round_up(const T &x)
379
 
{
380
 
  return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1;
381
 
}
382
 
 
 
132
#define ROUND_UP(X)  (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
383
133
static const dec1 powers10[DIG_PER_DEC1+1]={
384
134
  1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
385
135
static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
388
138
  999900000, 999990000, 999999000,
389
139
  999999900, 999999990 };
390
140
 
391
 
#ifdef HAVE_VALGRIND
 
141
#ifdef HAVE_purify
392
142
#define sanity(d) assert((d)->len > 0)
393
143
#else
394
144
#define sanity(d) assert((d)->len >0 && ((d)->buf[0] | \
395
145
                              (d)->buf[(d)->len-1] | 1))
396
146
#endif
397
147
 
398
 
inline static void fix_intg_frac_error(const int len, int &intg1, int &frac1, int &error)
399
 
{
400
 
  if (unlikely(intg1+frac1 > len))
401
 
  {
402
 
    if (unlikely(intg1 > len))
403
 
    {
404
 
      intg1=(len);
405
 
      frac1=0;
406
 
      error=E_DEC_OVERFLOW;
407
 
    }
408
 
    else
409
 
    {
410
 
      frac1=(len)-intg1;
411
 
      error=E_DEC_TRUNCATED;
412
 
    }
413
 
  }
414
 
  else
415
 
    error=E_DEC_OK;
416
 
}
417
 
 
418
 
/* assume carry <= 1 */
419
 
inline static void add(dec1 &to, const dec1 &from1, const dec1& from2, dec1 &carry)
420
 
{
421
 
  dec1 a=from1+from2+carry;
422
 
  assert(carry <= 1);
423
 
  if ((carry= (a >= DIG_BASE))) /* no division here! */
424
 
    a-=DIG_BASE;
425
 
  to=a;
426
 
}
427
 
 
428
 
inline static void add2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
429
 
{
430
 
  dec2 a=dec2(from1)+from2+carry;
431
 
  if ((carry= (a >= DIG_BASE)))
432
 
    a-=DIG_BASE;
433
 
  if (unlikely(a >= DIG_BASE))
434
 
  {
435
 
    a-=DIG_BASE;
436
 
    carry++;
437
 
  }
438
 
  to=dec1(a);
439
 
}
440
 
 
441
 
/* to=from1-from2 */
442
 
inline static void sub(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
443
 
{
444
 
  dec1 a=from1-from2-carry;
445
 
  if ((carry= (a < 0)))
446
 
    a+=DIG_BASE;
447
 
  to=a;
448
 
}
449
 
 
450
 
/* to=from1-from2 */
451
 
inline static void sub2(dec1 &to, const dec1 &from1, const dec1 &from2, dec1 &carry)
452
 
{
453
 
  dec1 a=from1-from2-carry;
454
 
  if ((carry= (a < 0)))
455
 
    a+=DIG_BASE;
456
 
  if (unlikely(a < 0))
457
 
  {
458
 
    a+=DIG_BASE;
459
 
    carry++;
460
 
  }
461
 
  to=a;
462
 
}
 
148
#define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error)                   \
 
149
        do                                                              \
 
150
        {                                                               \
 
151
          if (unlikely(intg1+frac1 > (len)))                            \
 
152
          {                                                             \
 
153
            if (unlikely(intg1 > (len)))                                \
 
154
            {                                                           \
 
155
              intg1=(len);                                              \
 
156
              frac1=0;                                                  \
 
157
              error=E_DEC_OVERFLOW;                                     \
 
158
            }                                                           \
 
159
            else                                                        \
 
160
            {                                                           \
 
161
              frac1=(len)-intg1;                                        \
 
162
              error=E_DEC_TRUNCATED;                                    \
 
163
            }                                                           \
 
164
          }                                                             \
 
165
          else                                                          \
 
166
            error=E_DEC_OK;                                             \
 
167
        } while(0)
 
168
 
 
169
#define ADD(to, from1, from2, carry)  /* assume carry <= 1 */           \
 
170
        do                                                              \
 
171
        {                                                               \
 
172
          dec1 a=(from1)+(from2)+(carry);                               \
 
173
          assert((carry) <= 1);                                    \
 
174
          if (((carry)= a >= DIG_BASE)) /* no division here! */         \
 
175
            a-=DIG_BASE;                                                \
 
176
          (to)=a;                                                       \
 
177
        } while(0)
 
178
 
 
179
#define ADD2(to, from1, from2, carry)                                   \
 
180
        do                                                              \
 
181
        {                                                               \
 
182
          dec2 a=((dec2)(from1))+(from2)+(carry);                       \
 
183
          if (((carry)= a >= DIG_BASE))                                 \
 
184
            a-=DIG_BASE;                                                \
 
185
          if (unlikely(a >= DIG_BASE))                                  \
 
186
          {                                                             \
 
187
            a-=DIG_BASE;                                                \
 
188
            carry++;                                                    \
 
189
          }                                                             \
 
190
          (to)=(dec1) a;                                                \
 
191
        } while(0)
 
192
 
 
193
#define SUB(to, from1, from2, carry) /* to=from1-from2 */               \
 
194
        do                                                              \
 
195
        {                                                               \
 
196
          dec1 a=(from1)-(from2)-(carry);                               \
 
197
          if (((carry)= a < 0))                                         \
 
198
            a+=DIG_BASE;                                                \
 
199
          (to)=a;                                                       \
 
200
        } while(0)
 
201
 
 
202
#define SUB2(to, from1, from2, carry) /* to=from1-from2 */              \
 
203
        do                                                              \
 
204
        {                                                               \
 
205
          dec1 a=(from1)-(from2)-(carry);                               \
 
206
          if (((carry)= a < 0))                                         \
 
207
            a+=DIG_BASE;                                                \
 
208
          if (unlikely(a < 0))                                          \
 
209
          {                                                             \
 
210
            a+=DIG_BASE;                                                \
 
211
            carry++;                                                    \
 
212
          }                                                             \
 
213
          (to)=a;                                                       \
 
214
        } while(0)
463
215
 
464
216
/**
465
 
  @brief  Get maximum value for given precision and scale
466
 
 
467
 
  @param  precision/scale  see decimal_bin_size() below
468
 
  @param  to              decimal where where the result will be stored
 
217
  Swap the contents of two variables.
 
218
 */
 
219
#define swap_variables(TYPE, a, b) \
 
220
  do {                             \
 
221
    TYPE dummy;                    \
 
222
    dummy= a;                      \
 
223
    a= b;                          \
 
224
    b= dummy;                      \
 
225
  } while (0)
 
226
 
 
227
 
 
228
/*
 
229
  Get maximum value for given precision and scale
 
230
 
 
231
  SYNOPSIS
 
232
    max_decimal()
 
233
    precision/scale - see decimal_bin_size() below
 
234
    to              - decimal where where the result will be stored
469
235
                      to->buf and to->len must be set.
470
236
*/
471
237
 
496
262
}
497
263
 
498
264
 
499
 
static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result)
 
265
static dec1 *remove_leading_zeroes(decimal_t *from, int *intg_result)
500
266
{
501
267
  int intg= from->intg, i;
502
268
  dec1 *buf0= from->buf;
519
285
}
520
286
 
521
287
 
522
 
/**
523
 
 @brief Count actual length of fraction part (without ending zeroes)
 
288
/*
 
289
  Count actual length of fraction part (without ending zeroes)
524
290
 
525
 
 @param from    number for processing
 
291
  SYNOPSIS
 
292
    decimal_actual_fraction()
 
293
    from    number for processing
526
294
*/
527
295
 
528
296
int decimal_actual_fraction(decimal_t *from)
529
297
{
530
298
  int frac= from->frac, i;
531
 
  dec1 *buf0= from->buf + round_up(from->intg) + round_up(frac) - 1;
 
299
  dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
532
300
 
533
301
  if (frac == 0)
534
302
    return 0;
548
316
}
549
317
 
550
318
 
551
 
/**
552
 
 @brief  Convert decimal to its printable string representation
 
319
/*
 
320
  Convert decimal to its printable string representation
553
321
 
554
 
 @param  from       value to convert
555
 
 @param  to         points to buffer where string representation
556
 
                    should be stored
557
 
 @param  to_len     in:  size of to buffer
558
 
                    out: length of the actually written string
559
 
 @param  fixed_precision 0 if representation can be variable length and
 
322
  SYNOPSIS
 
323
    decimal2string()
 
324
      from            - value to convert
 
325
      to              - points to buffer where string representation
 
326
                        should be stored
 
327
      *to_len         - in:  size of to buffer
 
328
                        out: length of the actually written string
 
329
      fixed_precision - 0 if representation can be variable length and
560
330
                        fixed_decimals will not be checked in this case.
561
331
                        Put number as with fixed point position with this
562
332
                        number of digits (sign counted and decimal point is
563
333
                        counted)
564
 
 @param  fixed_decimals  number digits after point.
565
 
 @param  filler          character to fill gaps in case of fixed_precision > 0
 
334
      fixed_decimals  - number digits after point.
 
335
      filler          - character to fill gaps in case of fixed_precision > 0
566
336
 
567
 
 @return error code
568
 
   @retval E_DEC_OK
569
 
   @retval E_DEC_TRUNCATED
570
 
   @retval E_DEC_OVERFLOW
 
337
  RETURN VALUE
 
338
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
571
339
*/
572
 
int decimal2string(const decimal_t *from, char *to, int *to_len,
 
340
 
 
341
int decimal2string(decimal_t *from, char *to, int *to_len,
573
342
                   int fixed_precision, int fixed_decimals,
574
343
                   char filler)
575
344
{
633
402
  {
634
403
    char *s1= s + intg_len;
635
404
    fill= frac_len - frac;
636
 
    buf=buf0+round_up(intg);
 
405
    buf=buf0+ROUND_UP(intg);
637
406
    *s1++='.';
638
407
    for (; frac>0; frac-=DIG_PER_DEC1)
639
408
    {
640
409
      dec1 x=*buf++;
641
 
      for (i=min(frac, DIG_PER_DEC1); i; i--)
 
410
      for (i=cmin(frac, DIG_PER_DEC1); i; i--)
642
411
      {
643
412
        dec1 y=x/DIG_MASK;
644
413
        *s1++='0'+(unsigned char)y;
658
427
  if (intg)
659
428
  {
660
429
    s+=intg;
661
 
    for (buf=buf0+round_up(intg); intg>0; intg-=DIG_PER_DEC1)
 
430
    for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
662
431
    {
663
432
      dec1 x=*--buf;
664
 
      for (i=min(intg, DIG_PER_DEC1); i; i--)
 
433
      for (i=cmin(intg, DIG_PER_DEC1); i; i--)
665
434
      {
666
435
        dec1 y=x/10;
667
436
        *--s='0'+(unsigned char)(x-y*10);
675
444
}
676
445
 
677
446
 
678
 
/**
679
 
 @brief  Return bounds of decimal digits in the number
 
447
/*
 
448
  Return bounds of decimal digits in the number
680
449
 
681
 
 @param  from  decimal number for processing
682
 
 @param  start_result  index (from 0 ) of first decimal digits will
683
 
                       be written by this address
684
 
 @param  end_result   index of position just after last decimal digit
 
450
  SYNOPSIS
 
451
    digits_bounds()
 
452
      from         - decimal number for processing
 
453
      start_result - index (from 0 ) of first decimal digits will
 
454
                     be written by this address
 
455
      end_result   - index of position just after last decimal digit
685
456
                     be written by this address
686
457
*/
 
458
 
687
459
static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
688
460
{
689
461
  int start, stop, i;
690
462
  dec1 *buf_beg= from->buf;
691
 
  dec1 *end= from->buf + round_up(from->intg) + round_up(from->frac);
 
463
  dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
692
464
  dec1 *buf_end= end - 1;
693
465
 
694
466
  /* find non-zero digit from number begining */
737
509
}
738
510
 
739
511
 
740
 
/**
741
 
 @param Left shift for alignment of data in buffer
742
 
 
743
 
 @param  dec     pointer to decimal number which have to be shifted
744
 
 @param  shift   number of decimal digits on which it should be shifted
745
 
 @param  beg     beginning of decimal digits (see digits_bounds())
746
 
 @param  end     end of decimal digits (see digits_bounds())
747
 
 
748
 
 @note
749
 
   Result fitting in the buffer should be garanted.
750
 
   'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
751
 
   
752
 
 @todo  Above note is unclear - is 'garanted' a typo for 'guaranteed'
753
 
 or 'granted'?
 
512
/*
 
513
  Left shift for alignment of data in buffer
 
514
 
 
515
  SYNOPSIS
 
516
    do_mini_left_shift()
 
517
    dec     pointer to decimal number which have to be shifted
 
518
    shift   number of decimal digits on which it should be shifted
 
519
    beg/end bounds of decimal digits (see digits_bounds())
 
520
 
 
521
  NOTE
 
522
    Result fitting in the buffer should be garanted.
 
523
    'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
754
524
*/
 
525
 
755
526
static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
756
527
{
757
 
  dec1 *from= dec->buf + round_up(beg + 1) - 1;
758
 
  dec1 *end= dec->buf + round_up(last) - 1;
 
528
  dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
 
529
  dec1 *end= dec->buf + ROUND_UP(last) - 1;
759
530
  int c_shift= DIG_PER_DEC1 - shift;
760
531
  assert(from >= dec->buf);
761
532
  assert(end < dec->buf + dec->len);
768
539
}
769
540
 
770
541
 
771
 
/**
772
 
  @brief Right shift for alignment of data in buffer
773
 
 
774
 
  @param  dec     pointer to decimal number which have to be shifted
775
 
  @param  shift   number of decimal digits on which it should be shifted
776
 
  @param  beg     beginning of decimal digits (see digits_bounds())
777
 
  @param  end     end of decimal digits (see digits_bounds())
778
 
 
779
 
  @note
 
542
/*
 
543
  Right shift for alignment of data in buffer
 
544
 
 
545
  SYNOPSIS
 
546
    do_mini_left_shift()
 
547
    dec     pointer to decimal number which have to be shifted
 
548
    shift   number of decimal digits on which it should be shifted
 
549
    beg/end bounds of decimal digits (see digits_bounds())
 
550
 
 
551
  NOTE
780
552
    Result fitting in the buffer should be garanted.
781
553
    'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
782
554
*/
 
555
 
783
556
static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
784
557
{
785
 
  dec1 *from= dec->buf + round_up(last) - 1;
786
 
  dec1 *end= dec->buf + round_up(beg + 1) - 1;
 
558
  dec1 *from= dec->buf + ROUND_UP(last) - 1;
 
559
  dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
787
560
  int c_shift= DIG_PER_DEC1 - shift;
788
561
  assert(from < dec->buf + dec->len);
789
562
  assert(end >= dec->buf);
796
569
}
797
570
 
798
571
 
799
 
/**
800
 
  @brief  Shift of decimal digits in given number (with rounding if it need)
 
572
/*
 
573
  Shift of decimal digits in given number (with rounding if it need)
801
574
 
802
 
  @param  dec       number to be shifted
803
 
  @param  shift     number of decimal positions
 
575
  SYNOPSIS
 
576
    decimal_shift()
 
577
    dec       number to be shifted
 
578
    shift     number of decimal positions
804
579
              shift > 0 means shift to left shift
805
580
              shift < 0 meand right shift
806
 
 
807
 
  @note
 
581
  NOTE
808
582
    In fact it is multipling on 10^shift.
 
583
  RETURN
 
584
    E_DEC_OK          OK
 
585
    E_DEC_OVERFLOW    operation lead to overflow, number is untoched
 
586
    E_DEC_TRUNCATED   number was rounded to fit into buffer
 
587
*/
809
588
 
810
 
  @return  Error code
811
 
   @retval E_DEC_OK          OK
812
 
   @retval E_DEC_OVERFLOW    operation lead to overflow, number is untoched
813
 
   @retval E_DEC_TRUNCATED   number was rounded to fit into buffer
814
 
*/
815
589
static int decimal_shift(decimal_t *dec, int shift)
816
590
{
817
591
  /* index of first non zero digit (all indexes from 0) */
819
593
  /* index of position after last decimal digit */
820
594
  int end;
821
595
  /* index of digit position just after point */
822
 
  int point= round_up(dec->intg) * DIG_PER_DEC1;
 
596
  int point= ROUND_UP(dec->intg) * DIG_PER_DEC1;
823
597
  /* new point position */
824
598
  int new_point= point + shift;
825
599
  /* number of digits in result */
837
611
 
838
612
  if (beg == end)
839
613
  {
840
 
    dec->set_zero();
 
614
    decimal_make_zero(dec);
841
615
    return E_DEC_OK;
842
616
  }
843
617
 
846
620
  digits_frac= end - new_point;
847
621
  set_if_bigger(digits_frac, 0);
848
622
 
849
 
  if ((new_len= round_up(digits_int) + (new_frac_len= round_up(digits_frac))) >
 
623
  if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) >
850
624
      dec->len)
851
625
  {
852
626
    int lack= new_len - dec->len;
870
644
        we lost all digits (they will be shifted out of buffer), so we can
871
645
        just return 0
872
646
      */
873
 
      dec->set_zero();
874
 
 
 
647
      decimal_make_zero(dec);
875
648
      return E_DEC_TRUNCATED;
876
649
    }
877
650
  }
907
680
    if (do_left)
908
681
    {
909
682
      do_mini_left_shift(dec, l_mini_shift, beg, end);
910
 
      mini_shift= (-l_mini_shift);
 
683
      mini_shift=- l_mini_shift;
911
684
    }
912
685
    else
913
686
    {
940
713
    {
941
714
      /* move left */
942
715
      d_shift= new_front / DIG_PER_DEC1;
943
 
      to= dec->buf + (round_up(beg + 1) - 1 - d_shift);
944
 
      barier= dec->buf + (round_up(end) - 1 - d_shift);
 
716
      to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
 
717
      barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
945
718
      assert(to >= dec->buf);
946
719
      assert(barier + d_shift < dec->buf + dec->len);
947
720
      for(; to <= barier; to++)
954
727
    {
955
728
      /* move right */
956
729
      d_shift= (1 - new_front) / DIG_PER_DEC1;
957
 
      to= dec->buf + round_up(end) - 1 + d_shift;
958
 
      barier= dec->buf + round_up(beg + 1) - 1 + d_shift;
 
730
      to= dec->buf + ROUND_UP(end) - 1 + d_shift;
 
731
      barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
959
732
      assert(to < dec->buf + dec->len);
960
733
      assert(barier - d_shift >= dec->buf);
961
734
      for(; to >= barier; to--)
974
747
 
975
748
    Only one of following 'for' loops will work becouse beg <= end
976
749
  */
977
 
  beg= round_up(beg + 1) - 1;
978
 
  end= round_up(end) - 1;
 
750
  beg= ROUND_UP(beg + 1) - 1;
 
751
  end= ROUND_UP(end) - 1;
979
752
  assert(new_point >= 0);
980
753
 
981
754
  /* We don't want negative new_point below */
982
755
  if (new_point != 0)
983
 
    new_point= round_up(new_point) - 1;
 
756
    new_point= ROUND_UP(new_point) - 1;
984
757
 
985
758
  if (new_point > end)
986
759
  {
1000
773
}
1001
774
 
1002
775
 
1003
 
/**
1004
 
  @brief  Convert string to decimal
 
776
/*
 
777
  Convert string to decimal
1005
778
 
1006
 
  @param  from    value to convert. Doesn't have to be \0 terminated!
1007
 
  @param  to      decimal where where the result will be stored
 
779
  SYNOPSIS
 
780
    internal_str2decl()
 
781
      from    - value to convert. Doesn't have to be \0 terminated!
 
782
      to      - decimal where where the result will be stored
1008
783
                to->buf and to->len must be set.
1009
 
  @param  end     Pointer to pointer to end of string. Will on return be
 
784
      end     - Pointer to pointer to end of string. Will on return be
1010
785
                set to the char after the last used character
1011
 
  @param  fixed   use to->intg, to->frac as limits for input number
 
786
      fixed   - use to->intg, to->frac as limits for input number
1012
787
 
1013
 
  @note
 
788
  NOTE
1014
789
    to->intg and to->frac can be modified even when fixed=1
1015
790
    (but only decreased, in this case)
1016
791
 
1017
 
  @return
 
792
  RETURN VALUE
1018
793
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_BAD_NUM/E_DEC_OOM
1019
794
    In case of E_DEC_FATAL_ERROR *to is set to decimal zero
1020
795
    (to make error handling easier)
1021
796
*/
 
797
 
1022
798
int
1023
799
internal_str2dec(char *from, decimal_t *to, char **end, bool fixed)
1024
800
{
1075
851
      error=E_DEC_OVERFLOW;
1076
852
      intg=to->intg;
1077
853
    }
1078
 
    intg1=round_up(intg);
1079
 
    frac1=round_up(frac);
 
854
    intg1=ROUND_UP(intg);
 
855
    frac1=ROUND_UP(frac);
1080
856
    if (intg1+frac1 > to->len)
1081
857
    {
1082
858
      error= E_DEC_OOM;
1085
861
  }
1086
862
  else
1087
863
  {
1088
 
    intg1=round_up(intg);
1089
 
    frac1=round_up(frac);
1090
 
    fix_intg_frac_error(to->len, intg1, frac1, error);
 
864
    intg1=ROUND_UP(intg);
 
865
    frac1=ROUND_UP(frac);
 
866
    FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
1091
867
    if (unlikely(error))
1092
868
    {
1093
869
      frac=frac1*DIG_PER_DEC1;
1135
911
  if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
1136
912
  {
1137
913
    int str_error;
1138
 
    const int64_t exponent= internal::my_strtoll10(endp+1, (char**) &end_of_string,
1139
 
                                                   &str_error);
 
914
    const int64_t exponent= my_strtoll10(endp+1, (char**) &end_of_string,
 
915
                                    &str_error);
1140
916
 
1141
917
    if (end_of_string != endp +1)               /* If at least one digit */
1142
918
    {
1163
939
  return error;
1164
940
 
1165
941
fatal_error:
1166
 
  to->set_zero();
 
942
  decimal_make_zero(to);
1167
943
  return error;
1168
944
}
1169
945
 
1170
946
 
1171
 
/**
1172
 
  @param Convert decimal to double
1173
 
 
1174
 
  @param[in]   from   value to convert
1175
 
  @param[out]  to     result will be stored there
1176
 
 
1177
 
  @return
 
947
/*
 
948
  Convert decimal to double
 
949
 
 
950
  SYNOPSIS
 
951
    decimal2double()
 
952
      from    - value to convert
 
953
      to      - result will be stored there
 
954
 
 
955
  RETURN VALUE
1178
956
    E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
1179
957
*/
1180
958
 
1181
 
int decimal2double(const decimal_t *from, double *to)
 
959
int decimal2double(decimal_t *from, double *to)
1182
960
{
1183
961
  char strbuf[FLOATING_POINT_BUFFER], *end;
1184
962
  int len= sizeof(strbuf);
1187
965
  rc = decimal2string(from, strbuf, &len, 0, 0, 0);
1188
966
  end= strbuf + len;
1189
967
 
1190
 
  *to= internal::my_strtod(strbuf, &end, &error);
 
968
  *to= my_strtod(strbuf, &end, &error);
1191
969
 
1192
970
  return (rc != E_DEC_OK) ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
1193
971
}
1194
972
 
1195
 
/**
1196
 
 @param  Convert double to decimal
1197
 
 
1198
 
 @param[in]  from    value to convert
1199
 
 @param[out] to      result will be stored there
1200
 
 
1201
 
 @return
 
973
/*
 
974
  Convert double to decimal
 
975
 
 
976
  SYNOPSIS
 
977
    double2decimal()
 
978
      from    - value to convert
 
979
      to      - result will be stored there
 
980
 
 
981
  RETURN VALUE
1202
982
    E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
1203
983
*/
1204
984
 
1205
 
int double2decimal(const double from, decimal_t *to)
 
985
int double2decimal(double from, decimal_t *to)
1206
986
{
1207
987
  char buff[FLOATING_POINT_BUFFER], *end;
1208
988
  int res;
1209
 
  end= buff + internal::my_gcvt(from,
1210
 
                                internal::MY_GCVT_ARG_DOUBLE,
1211
 
                                sizeof(buff) - 1, buff, NULL);
 
989
  end= buff + my_gcvt(from, MY_GCVT_ARG_DOUBLE, sizeof(buff) - 1, buff, NULL);
1212
990
  res= string2decimal(buff, to, &end);
1213
991
  return(res);
1214
992
}
1240
1018
  return error;
1241
1019
}
1242
1020
 
1243
 
int uint64_t2decimal(const uint64_t from, decimal_t *to)
 
1021
int uint64_t2decimal(uint64_t from, decimal_t *to)
1244
1022
{
1245
1023
  to->sign=0;
1246
1024
  return ull2dec(from, to);
1247
1025
}
1248
1026
 
1249
 
int int64_t2decimal(const int64_t from, decimal_t *to)
 
1027
int int64_t2decimal(int64_t from, decimal_t *to)
1250
1028
{
1251
1029
  if ((to->sign= from < 0))
1252
1030
    return ull2dec(-from, to);
1253
1031
  return ull2dec(from, to);
1254
1032
}
1255
1033
 
1256
 
int decimal2uint64_t(const decimal_t *from, uint64_t *to)
 
1034
int decimal2uint64_t(decimal_t *from, uint64_t *to)
1257
1035
{
1258
1036
  dec1 *buf=from->buf;
1259
1037
  uint64_t x=0;
1282
1060
  return E_DEC_OK;
1283
1061
}
1284
1062
 
1285
 
int decimal2int64_t(const decimal_t *from, int64_t *to)
 
1063
int decimal2int64_t(decimal_t *from, int64_t *to)
1286
1064
{
1287
1065
  dec1 *buf=from->buf;
1288
1066
  int64_t x=0;
1322
1100
  return E_DEC_OK;
1323
1101
}
1324
1102
 
1325
 
/**
1326
 
 @brief
1327
 
  Convert decimal to its binary fixed-length representation (suitable for
1328
 
  comparing with memcmp)
1329
 
 
 
1103
/*
 
1104
  Convert decimal to its binary fixed-length representation
 
1105
  two representations of the same length can be compared with memcmp
 
1106
  with the correct -1/0/+1 result
 
1107
 
 
1108
  SYNOPSIS
 
1109
    decimal2bin()
 
1110
      from    - value to convert
 
1111
      to      - points to buffer where string representation should be stored
 
1112
      precision/scale - see decimal_bin_size() below
 
1113
 
 
1114
  NOTE
 
1115
    the buffer is assumed to be of the size decimal_bin_size(precision, scale)
 
1116
 
 
1117
  RETURN VALUE
 
1118
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
 
1119
 
 
1120
  DESCRIPTION
1330
1121
    for storage decimal numbers are converted to the "binary" format.
1331
1122
 
1332
1123
    This format has the following properties:
1387
1178
    And for -1234567890.1234 it would be
1388
1179
 
1389
1180
                7E F2 04 37 2D FB 2D
1390
 
 
1391
 
 
1392
 
  @param from      value to convert
1393
 
  @param to        points to buffer where string representation should be stored
1394
 
  @param precision see decimal_bin_size() below
1395
 
  @param frac      see decimal_bin_size() below
1396
 
 
1397
 
  @note
1398
 
    The buffer is assumed to be of the size decimal_bin_size(precision, scale)
1399
 
 
1400
 
  @return
1401
 
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
1402
 
 
1403
1181
*/
1404
 
int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int frac)
 
1182
int decimal2bin(decimal_t *from, unsigned char *to, int precision, int frac)
1405
1183
{
1406
1184
  dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
1407
1185
  int error=E_DEC_OK, intg=precision-frac,
1519
1297
  return error;
1520
1298
}
1521
1299
 
1522
 
/**
1523
 
 @brief Restores decimal from its binary fixed-length representation
1524
 
 
1525
 
 @param  from    value to convert
1526
 
 @param  to      result
1527
 
 @param  precision see decimal_bin_size() below
1528
 
 @param  scale     see decimal_bin_size() below
1529
 
 
1530
 
 @note
 
1300
/*
 
1301
  Restores decimal from its binary fixed-length representation
 
1302
 
 
1303
  SYNOPSIS
 
1304
    bin2decimal()
 
1305
      from    - value to convert
 
1306
      to      - result
 
1307
      precision/scale - see decimal_bin_size() below
 
1308
 
 
1309
  NOTE
1531
1310
    see decimal2bin()
1532
1311
    the buffer is assumed to be of the size decimal_bin_size(precision, scale)
1533
1312
 
1534
 
 @return
 
1313
  RETURN VALUE
1535
1314
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
1536
1315
*/
 
1316
 
1537
1317
int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale)
1538
1318
{
1539
1319
  int error=E_DEC_OK, intg=precision-scale,
1551
1331
  d_copy[0]^= 0x80;
1552
1332
  from= d_copy;
1553
1333
 
1554
 
  fix_intg_frac_error(to->len, intg1, frac1, error);
 
1334
  FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
1555
1335
  if (unlikely(error))
1556
1336
  {
1557
1337
    if (intg1 < intg0+(intg0x>0))
1632
1412
  return error;
1633
1413
 
1634
1414
err:
1635
 
  to->set_zero();
 
1415
  decimal_make_zero(((decimal_t*) to));
1636
1416
  return(E_DEC_BAD_NUM);
1637
1417
}
1638
1418
 
1639
 
/**
1640
 
 @brief  Returns the size of array to hold a binary representation of a decimal
1641
 
 
1642
 
 @return  Size in bytes
1643
 
*/
 
1419
/*
 
1420
  Returns the size of array to hold a decimal with given precision and scale
 
1421
 
 
1422
  RETURN VALUE
 
1423
    size in dec1
 
1424
    (multiply by sizeof(dec1) to get the size if bytes)
 
1425
*/
 
1426
 
 
1427
int decimal_size(int precision, int scale)
 
1428
{
 
1429
  assert(scale >= 0 && precision > 0 && scale <= precision);
 
1430
  return ROUND_UP(precision-scale)+ROUND_UP(scale);
 
1431
}
 
1432
 
 
1433
/*
 
1434
  Returns the size of array to hold a binary representation of a decimal
 
1435
 
 
1436
  RETURN VALUE
 
1437
    size in bytes
 
1438
*/
 
1439
 
1644
1440
int decimal_bin_size(int precision, int scale)
1645
1441
{
1646
1442
  int intg=precision-scale,
1652
1448
         frac0*sizeof(dec1)+dig2bytes[frac0x];
1653
1449
}
1654
1450
 
1655
 
/**
1656
 
 @brief  Rounds the decimal to "scale" digits
1657
 
 
1658
 
 @param from    - decimal to round,
1659
 
 @param to      - result buffer. from==to is allowed
1660
 
 @param scale   - to what position to round. can be negative!
1661
 
 @param mode    - round to nearest even or truncate
1662
 
 
1663
 
 @note
 
1451
/*
 
1452
  Rounds the decimal to "scale" digits
 
1453
 
 
1454
  SYNOPSIS
 
1455
    decimal_round()
 
1456
      from    - decimal to round,
 
1457
      to      - result buffer. from==to is allowed
 
1458
      scale   - to what position to round. can be negative!
 
1459
      mode    - round to nearest even or truncate
 
1460
 
 
1461
  NOTES
1664
1462
    scale can be negative !
1665
1463
    one TRUNCATED error (line XXX below) isn't treated very logical :(
1666
1464
 
1667
 
 @return
 
1465
  RETURN VALUE
1668
1466
    E_DEC_OK/E_DEC_TRUNCATED
1669
1467
*/
 
1468
 
1670
1469
int
1671
 
decimal_round(const decimal_t *from, decimal_t *to, int scale,
 
1470
decimal_round(decimal_t *from, decimal_t *to, int scale,
1672
1471
              decimal_round_mode mode)
1673
1472
{
1674
 
  int frac0=scale>0 ? round_up(scale) : scale/DIG_PER_DEC1,
1675
 
      frac1=round_up(from->frac), round_digit= 0,
1676
 
      intg0=round_up(from->intg), error=E_DEC_OK, len=to->len,
1677
 
      intg1=round_up(from->intg +
 
1473
  int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1,
 
1474
      frac1=ROUND_UP(from->frac), round_digit= 0,
 
1475
      intg0=ROUND_UP(from->intg), error=E_DEC_OK, len=to->len,
 
1476
      intg1=ROUND_UP(from->intg +
1678
1477
                     (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX)));
1679
1478
  dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
1680
1479
  int first_dig;
1699
1498
 
1700
1499
  if (scale+from->intg < 0)
1701
1500
  {
1702
 
    to->set_zero();
 
1501
    decimal_make_zero(to);
1703
1502
    return E_DEC_OK;
1704
1503
  }
1705
1504
 
1706
1505
  if (to != from || intg1>intg0)
1707
1506
  {
1708
 
    dec1 *p0= buf0+intg0+max(frac1, frac0);
1709
 
    dec1 *p1= buf1+intg1+max(frac1, frac0);
 
1507
    dec1 *p0= buf0+intg0+cmax(frac1, frac0);
 
1508
    dec1 *p1= buf1+intg1+cmax(frac1, frac0);
1710
1509
 
1711
1510
    while (buf0 < p0)
1712
1511
      *(--p1) = *(--p0);
1717
1516
    buf0=to->buf;
1718
1517
    buf1=to->buf;
1719
1518
    to->sign=from->sign;
1720
 
    to->intg=min(intg0, len)*DIG_PER_DEC1;
 
1519
    to->intg=cmin(intg0, len)*DIG_PER_DEC1;
1721
1520
  }
1722
1521
 
1723
1522
  if (frac0 > frac1)
1770
1569
    }
1771
1570
    else if (frac0+intg0==0)
1772
1571
    {
1773
 
      to->set_zero();
 
1572
      decimal_make_zero(to);
1774
1573
      return E_DEC_OK;
1775
1574
    }
1776
1575
  }
1777
1576
  else
1778
1577
  {
1779
 
  /** @todo fix this code as it won't work for CEILING mode */
 
1578
    /* TODO - fix this code as it won't work for CEILING mode */
1780
1579
    int pos=frac0*DIG_PER_DEC1-scale-1;
1781
1580
    assert(frac0+intg0 > 0);
1782
1581
    x=*buf1 / powers10[pos];
1809
1608
    carry=1;
1810
1609
    *buf1-=DIG_BASE;
1811
1610
    while (carry && --buf1 >= to->buf)
1812
 
      add(*buf1, *buf1, 0, carry);
 
1611
      ADD(*buf1, *buf1, 0, carry);
1813
1612
    if (unlikely(carry))
1814
1613
    {
1815
1614
      /* shifting the number to create space for new digit */
1819
1618
        scale=frac0*DIG_PER_DEC1;
1820
1619
        error=E_DEC_TRUNCATED; /* XXX */
1821
1620
      }
1822
 
      for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
 
1621
      for (buf1=to->buf+intg0+cmax(frac0,0); buf1 > to->buf; buf1--)
1823
1622
      {
1824
1623
        buf1[0]=buf1[-1];
1825
1624
      }
1838
1637
        /* making 'zero' with the proper scale */
1839
1638
        dec1 *p0= to->buf + frac0 + 1;
1840
1639
        to->intg=1;
1841
 
        to->frac= max(scale, 0);
 
1640
        to->frac= cmax(scale, 0);
1842
1641
        to->sign= 0;
1843
1642
        for (buf1= to->buf; buf1<p0; buf1++)
1844
1643
          *buf1= 0;
1860
1659
  return error;
1861
1660
}
1862
1661
 
1863
 
static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1864
 
{
1865
 
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1866
 
      frac1=round_up(from1->frac), frac2=round_up(from2->frac),
1867
 
      frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
 
1662
/*
 
1663
  Returns the size of the result of the operation
 
1664
 
 
1665
  SYNOPSIS
 
1666
    decimal_result_size()
 
1667
      from1   - operand of the unary operation or first operand of the
 
1668
                binary operation
 
1669
      from2   - second operand of the binary operation
 
1670
      op      - operation. one char '+', '-', '*', '/' are allowed
 
1671
                others may be added later
 
1672
      param   - extra param to the operation. unused for '+', '-', '*'
 
1673
                scale increment for '/'
 
1674
 
 
1675
  NOTE
 
1676
    returned valued may be larger than the actual buffer requred
 
1677
    in the operation, as decimal_result_size, by design, operates on
 
1678
    precision/scale values only and not on the actual decimal number
 
1679
 
 
1680
  RETURN VALUE
 
1681
    size of to->buf array in dec1 elements. to get size in bytes
 
1682
    multiply by sizeof(dec1)
 
1683
*/
 
1684
 
 
1685
int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param)
 
1686
{
 
1687
  switch (op) {
 
1688
  case '-':
 
1689
    return ROUND_UP(cmax(from1->intg, from2->intg)) +
 
1690
           ROUND_UP(cmax(from1->frac, from2->frac));
 
1691
  case '+':
 
1692
    return ROUND_UP(cmax(from1->intg, from2->intg)+1) +
 
1693
           ROUND_UP(cmax(from1->frac, from2->frac));
 
1694
  case '*':
 
1695
    return ROUND_UP(from1->intg+from2->intg)+
 
1696
           ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
 
1697
  case '/':
 
1698
    return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param);
 
1699
  default: assert(0);
 
1700
  }
 
1701
  return -1; /* shut up the warning */
 
1702
}
 
1703
 
 
1704
static int do_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1705
{
 
1706
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
 
1707
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
 
1708
      frac0=cmax(frac1, frac2), intg0=cmax(intg1, intg2), error;
1868
1709
  dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
1869
1710
 
1870
1711
  sanity(to);
1879
1720
    to->buf[0]=0; /* safety */
1880
1721
  }
1881
1722
 
1882
 
  fix_intg_frac_error(to->len, intg0, frac0, error);
 
1723
  FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
1883
1724
  if (unlikely(error == E_DEC_OVERFLOW))
1884
1725
  {
1885
1726
    max_decimal(to->len * DIG_PER_DEC1, 0, to);
1889
1730
  buf0=to->buf+intg0+frac0;
1890
1731
 
1891
1732
  to->sign=from1->sign;
1892
 
  to->frac=max(from1->frac, from2->frac);
 
1733
  to->frac=cmax(from1->frac, from2->frac);
1893
1734
  to->intg=intg0*DIG_PER_DEC1;
1894
1735
  if (unlikely(error))
1895
1736
  {
1922
1763
  carry=0;
1923
1764
  while (buf1 > stop2)
1924
1765
  {
1925
 
    add(*--buf0, *--buf1, *--buf2, carry);
 
1766
    ADD(*--buf0, *--buf1, *--buf2, carry);
1926
1767
  }
1927
1768
 
1928
1769
  /* part 3 - cmin(intg) ... cmax(intg) */
1930
1771
                        ((stop=from2->buf)+intg2-intg1) ;
1931
1772
  while (buf1 > stop)
1932
1773
  {
1933
 
    add(*--buf0, *--buf1, 0, carry);
 
1774
    ADD(*--buf0, *--buf1, 0, carry);
1934
1775
  }
1935
1776
 
1936
1777
  if (unlikely(carry))
1942
1783
 
1943
1784
/* to=from1-from2.
1944
1785
   if to==0, return -1/0/+1 - the result of the comparison */
1945
 
static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
1786
static int do_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
1946
1787
{
1947
 
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1948
 
      frac1=round_up(from1->frac), frac2=round_up(from2->frac);
1949
 
  int frac0=max(frac1, frac2), error;
 
1788
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
 
1789
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
 
1790
  int frac0=cmax(frac1, frac2), error;
1950
1791
  dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
1951
1792
 
1952
1793
  /* let carry:=1 if from2 > from1 */
1995
1836
      {
1996
1837
        if (to == 0) /* decimal_cmp() */
1997
1838
          return 0;
1998
 
 
1999
 
        to->set_zero();
2000
 
 
 
1839
        decimal_make_zero(to);
2001
1840
        return E_DEC_OK;
2002
1841
      }
2003
1842
    }
2013
1852
  /* ensure that always from1 > from2 (and intg1 >= intg2) */
2014
1853
  if (carry)
2015
1854
  {
2016
 
    swap(from1, from2);
2017
 
    swap(start1, start2);
2018
 
    swap(intg1, intg2);
2019
 
    swap(frac1, frac2);
 
1855
    swap_variables(decimal_t *,from1,from1);
 
1856
    swap_variables(dec1 *,start1, start2);
 
1857
    swap_variables(int,intg1,intg2);
 
1858
    swap_variables(int,frac1,frac2);
2020
1859
    to->sign= 1 - to->sign;
2021
1860
  }
2022
1861
 
2023
 
  fix_intg_frac_error(to->len, intg1, frac0, error);
 
1862
  FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
2024
1863
  buf0=to->buf+intg1+frac0;
2025
1864
 
2026
 
  to->frac=max(from1->frac, from2->frac);
 
1865
  to->frac=cmax(from1->frac, from2->frac);
2027
1866
  to->intg=intg1*DIG_PER_DEC1;
2028
1867
  if (unlikely(error))
2029
1868
  {
2054
1893
      *--buf0=0;
2055
1894
    while (buf2 > stop2)
2056
1895
    {
2057
 
      sub(*--buf0, 0, *--buf2, carry);
 
1896
      SUB(*--buf0, 0, *--buf2, carry);
2058
1897
    }
2059
1898
  }
2060
1899
 
2061
1900
  /* part 2 - cmin(frac) ... intg2 */
2062
1901
  while (buf2 > start2)
2063
1902
  {
2064
 
    sub(*--buf0, *--buf1, *--buf2, carry);
 
1903
    SUB(*--buf0, *--buf1, *--buf2, carry);
2065
1904
  }
2066
1905
 
2067
1906
  /* part 3 - intg2 ... intg1 */
2068
1907
  while (carry && buf1 > start1)
2069
1908
  {
2070
 
    sub(*--buf0, *--buf1, 0, carry);
 
1909
    SUB(*--buf0, *--buf1, 0, carry);
2071
1910
  }
2072
1911
 
2073
1912
  while (buf1 > start1)
2079
1918
  return error;
2080
1919
}
2081
1920
 
2082
 
int decimal_intg(const decimal_t *from)
 
1921
int decimal_intg(decimal_t *from)
2083
1922
{
2084
1923
  int res;
2085
1924
  dec1 *tmp_res;
2087
1926
  return res;
2088
1927
}
2089
1928
 
2090
 
int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
1929
int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
2091
1930
{
2092
1931
  if (likely(from1->sign == from2->sign))
2093
1932
    return do_add(from1, from2, to);
2094
1933
  return do_sub(from1, from2, to);
2095
1934
}
2096
1935
 
2097
 
int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
1936
int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
2098
1937
{
2099
1938
  if (likely(from1->sign == from2->sign))
2100
1939
    return do_sub(from1, from2, to);
2101
1940
  return do_add(from1, from2, to);
2102
1941
}
2103
1942
 
2104
 
int decimal_cmp(const decimal_t *from1, const decimal_t *from2)
 
1943
int decimal_cmp(decimal_t *from1, decimal_t *from2)
2105
1944
{
2106
1945
  if (likely(from1->sign == from2->sign))
2107
1946
    return do_sub(from1, from2, 0);
2108
1947
  return from1->sign > from2->sign ? -1 : 1;
2109
1948
}
2110
1949
 
2111
 
int decimal_t::isZero() const
 
1950
int decimal_is_zero(decimal_t *from)
2112
1951
{
2113
 
  dec1 *buf1= buf,
2114
 
       *end= buf1 +round_up(intg) +round_up(frac);
2115
 
 
 
1952
  dec1 *buf1=from->buf,
 
1953
       *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac);
2116
1954
  while (buf1 < end)
2117
 
  {
2118
1955
    if (*buf1++)
2119
 
    {
2120
1956
      return 0;
2121
 
    }
2122
 
  }
2123
 
 
2124
1957
  return 1;
2125
1958
}
2126
1959
 
2127
 
/**
2128
 
 @brief multiply two decimals
2129
 
 
2130
 
 @param[in]   from1  First factor
2131
 
 @param[in]   from2  Second factor
2132
 
 @param[out]  to     product
2133
 
 
2134
 
 @return
 
1960
/*
 
1961
  multiply two decimals
 
1962
 
 
1963
  SYNOPSIS
 
1964
    decimal_mul()
 
1965
      from1, from2 - factors
 
1966
      to      - product
 
1967
 
 
1968
  RETURN VALUE
2135
1969
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
2136
1970
 
2137
 
 @note
 
1971
  NOTES
2138
1972
    in this implementation, with sizeof(dec1)=4 we have DIG_PER_DEC1=9,
2139
1973
    and 63-digit number will take only 7 dec1 words (basically a 7-digit
2140
1974
    "base 999999999" number).  Thus there's no need in fast multiplication
2144
1978
    XXX if this library is to be used with huge numbers of thousands of
2145
1979
    digits, fast multiplication must be implemented.
2146
1980
*/
2147
 
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
1981
int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to)
2148
1982
{
2149
 
  int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
2150
 
      frac1=round_up(from1->frac), frac2=round_up(from2->frac),
2151
 
      intg0=round_up(from1->intg+from2->intg),
 
1983
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
 
1984
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
 
1985
      intg0=ROUND_UP(from1->intg+from2->intg),
2152
1986
      frac0=frac1+frac2, error, i, j, d_to_move;
2153
1987
  dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
2154
1988
       *start2, *stop2, *stop1, *start0, carry;
2157
1991
 
2158
1992
  i=intg0;
2159
1993
  j=frac0;
2160
 
  fix_intg_frac_error(to->len, intg0, frac0, error);
 
1994
  FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
2161
1995
  to->sign=from1->sign != from2->sign;
2162
1996
  to->frac=from1->frac+from2->frac;
2163
1997
  to->intg=intg0*DIG_PER_DEC1;
2198
2032
      dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
2199
2033
      hi=(dec1)(p/DIG_BASE);
2200
2034
      lo=(dec1)(p-((dec2)hi)*DIG_BASE);
2201
 
      add2(*buf0, *buf0, lo, carry);
 
2035
      ADD2(*buf0, *buf0, lo, carry);
2202
2036
      carry+=hi;
2203
2037
    }
2204
2038
    if (carry)
2205
2039
    {
2206
2040
      if (buf0 < to->buf)
2207
2041
        return E_DEC_OVERFLOW;
2208
 
      add2(*buf0, *buf0, 0, carry);
 
2042
      ADD2(*buf0, *buf0, 0, carry);
2209
2043
    }
2210
2044
    for (buf0--; carry; buf0--)
2211
2045
    {
2212
2046
      if (buf0 < to->buf)
2213
2047
        return E_DEC_OVERFLOW;
2214
 
      add(*buf0, *buf0, 0, carry);
 
2048
      ADD(*buf0, *buf0, 0, carry);
2215
2049
    }
2216
2050
  }
2217
2051
 
2228
2062
      if (++buf == end)
2229
2063
      {
2230
2064
        /* We got decimal zero */
2231
 
        to->set_zero();
 
2065
        decimal_make_zero(to);
2232
2066
        break;
2233
2067
      }
2234
2068
    }
2235
2069
  }
2236
2070
  buf1= to->buf;
2237
 
  d_to_move= intg0 + round_up(to->frac);
 
2071
  d_to_move= intg0 + ROUND_UP(to->frac);
2238
2072
  while (!*buf1 && (to->intg > DIG_PER_DEC1))
2239
2073
  {
2240
2074
    buf1++;
2250
2084
  return error;
2251
2085
}
2252
2086
 
2253
 
/**
 
2087
/*
2254
2088
  naive division algorithm (Knuth's Algorithm D in 4.3.1) -
2255
2089
  it's ok for short numbers
2256
2090
  also we're using alloca() to allocate a temporary buffer
2257
2091
 
2258
 
  @todo
2259
 
  If this library is to be used with huge numbers of thousands of
 
2092
  XXX if this library is to be used with huge numbers of thousands of
2260
2093
  digits, fast division must be implemented and alloca should be
2261
2094
  changed to malloc (or at least fallback to malloc if alloca() fails)
2262
2095
  but then, decimal_mul() should be rewritten too :(
2263
2096
*/
2264
 
static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
 
2097
static int do_div_mod(decimal_t *from1, decimal_t *from2,
2265
2098
                       decimal_t *to, decimal_t *mod, int scale_incr)
2266
2099
{
2267
 
  int frac1=round_up(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
2268
 
      frac2=round_up(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
 
2100
  int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
 
2101
      frac2=ROUND_UP(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
2269
2102
      error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
2270
2103
  dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
2271
2104
       *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
2298
2131
  }
2299
2132
  if (prec1 <= 0)
2300
2133
  { /* short-circuit everything: from1 == 0 */
2301
 
    to->set_zero();
 
2134
    decimal_make_zero(to);
2302
2135
    return E_DEC_OK;
2303
2136
  }
2304
2137
  for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
2315
2148
    intg0=0;
2316
2149
  }
2317
2150
  else
2318
 
    intg0=round_up(dintg);
 
2151
    intg0=ROUND_UP(dintg);
2319
2152
  if (mod)
2320
2153
  {
2321
2154
    /* we're calculating N1 % N2.
2324
2157
         intg=intg2
2325
2158
    */
2326
2159
    to->sign=from1->sign;
2327
 
    to->frac=max(from1->frac, from2->frac);
 
2160
    to->frac=cmax(from1->frac, from2->frac);
2328
2161
    frac0=0;
2329
2162
  }
2330
2163
  else
2334
2167
      N2 is in the buf2, has prec2 digits. Scales are frac1 and
2335
2168
      frac2 accordingly.
2336
2169
      Thus, the result will have
2337
 
         frac = round_up(frac1+frac2+scale_incr)
 
2170
         frac = ROUND_UP(frac1+frac2+scale_incr)
2338
2171
      and
2339
2172
         intg = (prec1-frac1) - (prec2-frac2) + 1
2340
2173
         prec = intg+frac
2341
2174
    */
2342
 
    frac0=round_up(frac1+frac2+scale_incr);
2343
 
    fix_intg_frac_error(to->len, intg0, frac0, error);
 
2175
    frac0=ROUND_UP(frac1+frac2+scale_incr);
 
2176
    FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
2344
2177
    to->sign=from1->sign != from2->sign;
2345
2178
    to->intg=intg0*DIG_PER_DEC1;
2346
2179
    to->frac=frac0*DIG_PER_DEC1;
2351
2184
    while (dintg++ < 0)
2352
2185
      *buf0++=0;
2353
2186
 
2354
 
  len1=(i=round_up(prec1))+round_up(2*frac2+scale_incr+1) + 1;
 
2187
  len1=(i=ROUND_UP(prec1))+ROUND_UP(2*frac2+scale_incr+1) + 1;
2355
2188
  set_if_bigger(len1, 3);
2356
2189
  if (!(tmp1=(dec1 *)alloca(len1*sizeof(dec1))))
2357
2190
    return E_DEC_OOM;
2361
2194
  start1=tmp1;
2362
2195
  stop1=start1+len1;
2363
2196
  start2=buf2;
2364
 
  stop2=buf2+round_up(prec2)-1;
 
2197
  stop2=buf2+ROUND_UP(prec2)-1;
2365
2198
 
2366
2199
  /* removing end zeroes */
2367
2200
  while (*stop2 == 0 && stop2 >= start2)
2420
2253
        x=guess * (*--buf2);
2421
2254
        hi=(dec1)(x/DIG_BASE);
2422
2255
        lo=(dec1)(x-((dec2)hi)*DIG_BASE);
2423
 
        sub2(*buf1, *buf1, lo, carry);
 
2256
        SUB2(*buf1, *buf1, lo, carry);
2424
2257
        carry+=hi;
2425
2258
      }
2426
2259
      carry= dcarry < carry;
2434
2267
        buf1=start1+len2;
2435
2268
        for (carry=0; buf2 > start2; buf1--)
2436
2269
        {
2437
 
          add(*buf1, *buf1, *--buf2, carry);
 
2270
          ADD(*buf1, *buf1, *--buf2, carry);
2438
2271
        }
2439
2272
      }
2440
2273
    }
2453
2286
    if (dcarry)
2454
2287
      *--start1=dcarry;
2455
2288
    buf0=to->buf;
2456
 
    intg0=(int) (round_up(prec1-frac1)-(start1-tmp1));
2457
 
    frac0=round_up(to->frac);
 
2289
    intg0=(int) (ROUND_UP(prec1-frac1)-(start1-tmp1));
 
2290
    frac0=ROUND_UP(to->frac);
2458
2291
    error=E_DEC_OK;
2459
2292
    if (unlikely(frac0==0 && intg0==0))
2460
2293
    {
2461
 
      to->set_zero();
 
2294
      decimal_make_zero(to);
2462
2295
      goto done;
2463
2296
    }
2464
2297
    if (intg0<=0)
2465
2298
    {
2466
2299
      if (unlikely(-intg0 >= to->len))
2467
2300
      {
2468
 
        to->set_zero();
 
2301
        decimal_make_zero(to);
2469
2302
        error=E_DEC_TRUNCATED;
2470
2303
        goto done;
2471
2304
      }
2484
2317
        error=E_DEC_OVERFLOW;
2485
2318
        goto done;
2486
2319
      }
2487
 
      assert(intg0 <= round_up(from2->intg));
 
2320
      assert(intg0 <= ROUND_UP(from2->intg));
2488
2321
      stop1=start1+frac0+intg0;
2489
 
      to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
 
2322
      to->intg=cmin(intg0*DIG_PER_DEC1, from2->intg);
2490
2323
    }
2491
2324
    if (unlikely(intg0+frac0 > to->len))
2492
2325
    {
2503
2336
  return error;
2504
2337
}
2505
2338
 
2506
 
/**
2507
 
 @brief  division of two decimals
2508
 
 
2509
 
 @param[in]  from1   dividend
2510
 
 @param[in]  from2   divisor
2511
 
 @param[out] to      quotient
2512
 
 
2513
 
 @return
 
2339
/*
 
2340
  division of two decimals
 
2341
 
 
2342
  SYNOPSIS
 
2343
    decimal_div()
 
2344
      from1   - dividend
 
2345
      from2   - divisor
 
2346
      to      - quotient
 
2347
 
 
2348
  RETURN VALUE
2514
2349
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
2515
2350
 
2516
 
 @note
 
2351
  NOTES
2517
2352
    see do_div_mod()
2518
2353
*/
 
2354
 
2519
2355
int
2520
 
decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
 
2356
decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to, int scale_incr)
2521
2357
{
2522
2358
  return do_div_mod(from1, from2, to, 0, scale_incr);
2523
2359
}
2524
2360
 
2525
 
/**
2526
 
 @brief modulus
2527
 
 
2528
 
 the modulus R in    R = M mod N
2529
 
 
2530
 
 is defined as
2531
 
 
2532
 
 0 <= |R| < |M|
2533
 
 sign R == sign M
2534
 
 R = M - k*N, where k is integer
2535
 
 
2536
 
 thus, there's no requirement for M or N to be integers
2537
 
 
2538
 
 
2539
 
 @param from1   dividend
2540
 
 @param from2   divisor
2541
 
 @param to      modulus
2542
 
 
2543
 
 @return
 
2361
/*
 
2362
  modulus
 
2363
 
 
2364
  SYNOPSIS
 
2365
    decimal_mod()
 
2366
      from1   - dividend
 
2367
      from2   - divisor
 
2368
      to      - modulus
 
2369
 
 
2370
  RETURN VALUE
2544
2371
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
2545
2372
 
2546
 
 @note
 
2373
  NOTES
2547
2374
    see do_div_mod()
2548
2375
 
 
2376
  DESCRIPTION
 
2377
    the modulus R in    R = M mod N
 
2378
 
 
2379
   is defined as
 
2380
 
 
2381
     0 <= |R| < |M|
 
2382
     sign R == sign M
 
2383
     R = M - k*N, where k is integer
 
2384
 
 
2385
   thus, there's no requirement for M or N to be integers
2549
2386
*/
2550
 
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
2387
 
 
2388
int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to)
2551
2389
{
2552
2390
  return do_div_mod(from1, from2, 0, to, 0);
2553
2391
}
2554
2392
 
2555
 
std::ostream& operator<<(std::ostream& output, const type::Decimal &dec)
2556
 
{
2557
 
  drizzled::String str;
2558
 
 
2559
 
  class_decimal2string(&dec, 0, &str);
2560
 
 
2561
 
  output << "type::Decimal:(";
2562
 
  output <<  str.c_ptr();
2563
 
  output << ")";
2564
 
 
2565
 
  return output;  // for multiple << operators.
2566
 
}
2567
 
 
2568
 
} /* namespace drizzled */
2569
 
 
2570
2393
#ifdef MAIN
2571
2394
 
2572
2395
int full= 0;
2577
2400
{
2578
2401
  int i;
2579
2402
  printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
2580
 
  for (i=0; i < round_up(d->frac)+round_up(d->intg)-1; i++)
 
2403
  for (i=0; i < ROUND_UP(d->frac)+ROUND_UP(d->intg)-1; i++)
2581
2404
    printf("%09d, ", d->buf[i]);
2582
2405
  printf("%09d} */ ", d->buf[i]);
2583
2406
}
2649
2472
{
2650
2473
  char s1[100], *end;
2651
2474
  int res;
2652
 
  snprintf(s1, sizeof(s1), "'%s'", s);
 
2475
  sprintf(s1, "'%s'", s);
2653
2476
  end= strend(s);
2654
2477
  printf("len=%2d %-30s => res=%d    ", a.len, s1,
2655
2478
         (res= string2decimal(s, &a, &end)));
2663
2486
  double x;
2664
2487
  int res;
2665
2488
 
2666
 
  snprintf(s1, sizeof(s1), "'%s'", s);
 
2489
  sprintf(s1, "'%s'", s);
2667
2490
  end= strend(s);
2668
2491
  string2decimal(s, &a, &end);
2669
2492
  res=decimal2double(&a, &x);
2677
2500
  char s1[100], buf[100], *end;
2678
2501
  int res, i, size=decimal_bin_size(p, s);
2679
2502
 
2680
 
  snprintf(s1, sizeof(s1), "'%s'", str);
 
2503
  sprintf(s1, "'%s'", str);
2681
2504
  end= strend(str);
2682
2505
  string2decimal(str, &a, &end);
2683
2506
  res=decimal2bin(&a, buf, p, s);
2710
2533
  int res;
2711
2534
 
2712
2535
  res=uint64_t2decimal(from, &a);
2713
 
  internal::int64_t10_to_str(from,s,10);
 
2536
  int64_t10_to_str(from,s,10);
2714
2537
  printf("%-40s => res=%d    ", s, res);
2715
2538
  print_decimal(&a, orig, res, ex);
2716
2539
  printf("\n");
2722
2545
  int res;
2723
2546
 
2724
2547
  res=int64_t2decimal(from, &a);
2725
 
  internal::int64_t10_to_str(from,s,-10);
 
2548
  int64_t10_to_str(from,s,-10);
2726
2549
  printf("%-40s => res=%d    ", s, res);
2727
2550
  print_decimal(&a, orig, res, ex);
2728
2551
  printf("\n");
2738
2561
  string2decimal(s, &a, &end);
2739
2562
  res=decimal2uint64_t(&a, &x);
2740
2563
  if (full) dump_decimal(&a);
2741
 
  internal::int64_t10_to_str(x,s1,10);
 
2564
  int64_t10_to_str(x,s1,10);
2742
2565
  printf("%-40s => res=%d    %s\n", s, res, s1);
2743
2566
  check_result_code(res, ex);
2744
2567
  if (orig && strcmp(orig, s1))
2758
2581
  string2decimal(s, &a, &end);
2759
2582
  res=decimal2int64_t(&a, &x);
2760
2583
  if (full) dump_decimal(&a);
2761
 
  internal::int64_t10_to_str(x,s1,-10);
 
2584
  int64_t10_to_str(x,s1,-10);
2762
2585
  printf("%-40s => res=%d    %s\n", s, res, s1);
2763
2586
  check_result_code(res, ex);
2764
2587
  if (orig && strcmp(orig, s1))
2772
2595
{
2773
2596
  char s[100], *end;
2774
2597
  int res;
2775
 
  snprintf(s, sizeof(s), "'%s' + '%s'", s1, s2);
 
2598
  sprintf(s, "'%s' + '%s'", s1, s2);
2776
2599
  end= strend(s1);
2777
2600
  string2decimal(s1, &a, &end);
2778
2601
  end= strend(s2);
2787
2610
{
2788
2611
  char s[100], *end;
2789
2612
  int res;
2790
 
  snprintf(s, sizeof(s), "'%s' - '%s'", s1, s2);
 
2613
  sprintf(s, "'%s' - '%s'", s1, s2);
2791
2614
  end= strend(s1);
2792
2615
  string2decimal(s1, &a, &end);
2793
2616
  end= strend(s2);
2802
2625
{
2803
2626
  char s[100], *end;
2804
2627
  int res;
2805
 
  snprintf(s, sizeof(s), "'%s' <=> '%s'", s1, s2);
 
2628
  sprintf(s, "'%s' <=> '%s'", s1, s2);
2806
2629
  end= strend(s1);
2807
2630
  string2decimal(s1, &a, &end);
2808
2631
  end= strend(s2);
2820
2643
{
2821
2644
  char s[100], *end;
2822
2645
  int res;
2823
 
  snprintf(s, sizeof(s), "'%s' * '%s'", s1, s2);
 
2646
  sprintf(s, "'%s' * '%s'", s1, s2);
2824
2647
  end= strend(s1);
2825
2648
  string2decimal(s1, &a, &end);
2826
2649
  end= strend(s2);
2835
2658
{
2836
2659
  char s[100], *end;
2837
2660
  int res;
2838
 
  snprintf(s, sizeof(s), "'%s' / '%s'", s1, s2);
 
2661
  sprintf(s, "'%s' / '%s'", s1, s2);
2839
2662
  end= strend(s1);
2840
2663
  string2decimal(s1, &a, &end);
2841
2664
  end= strend(s2);
2854
2677
{
2855
2678
  char s[100], *end;
2856
2679
  int res;
2857
 
  snprintf(s, sizeof(s), "'%s' %% '%s'", s1, s2);
 
2680
  sprintf(s, "'%s' %% '%s'", s1, s2);
2858
2681
  end= strend(s1);
2859
2682
  string2decimal(s1, &a, &end);
2860
2683
  end= strend(s2);
2877
2700
{
2878
2701
  char s[100], *end;
2879
2702
  int res;
2880
 
  snprintf(s, sizeof(s), "'%s', %d, %s", s1, n, round_mode[mode]);
 
2703
  sprintf(s, "'%s', %d, %s", s1, n, round_mode[mode]);
2881
2704
  end= strend(s1);
2882
2705
  string2decimal(s1, &a, &end);
2883
2706
  res=decimal_round(&a, &b, n, mode);
2890
2713
void test_mx(int precision, int frac, const char *orig)
2891
2714
{
2892
2715
  char s[100];
2893
 
  snprintf(s, sizeof(s), "%d, %d", precision, frac);
 
2716
  sprintf(s, "%d, %d", precision, frac);
2894
2717
  max_decimal(precision, frac, &a);
2895
2718
  printf("%-40s =>          ", s);
2896
2719
  print_decimal(&a, orig, 0, 0);
2906
2729
  int slen= sizeof(s2);
2907
2730
  int res;
2908
2731
 
2909
 
  snprintf(s, sizeof(s), filler ? "'%s', %d, %d, '%c'" : "'%s', %d, %d, '\\0'",
 
2732
  sprintf(s, filler ? "'%s', %d, %d, '%c'" : "'%s', %d, %d, '\\0'",
2910
2733
          s1, prec, dec, filler);
2911
2734
  end= strend(s1);
2912
2735
  string2decimal(s1, &a, &end);
2926
2749
{
2927
2750
  char s[100], *end;
2928
2751
  int res;
2929
 
  snprintf(s, sizeof(s), "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift));
 
2752
  sprintf(s, "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift));
2930
2753
  end= strend(s1);
2931
2754
  string2decimal(s1, &a, &end);
2932
2755
  res= decimal_shift(&a, shift);
2939
2762
void test_fr(const char *s1, const char *orig)
2940
2763
{
2941
2764
  char s[100], *end;
2942
 
  snprintf(s, sizeof(s), "'%s'", s1);
 
2765
  sprintf(s, "'%s'", s1);
2943
2766
  printf("%-40s =>          ", s);
2944
2767
  end= strend(s1);
2945
2768
  string2decimal(s1, &a, &end);
3298
3121
 
3299
3122
  return 0;
3300
3123
}
3301
 
 
3302
3124
#endif