~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/my_decimal.h

  • Committer: Monty Taylor
  • Date: 2010-01-12 21:34:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1268.
  • Revision ID: mordred@inaugust.com-20100112213424-6mslywtlca49mvnk
Updated to pandora-buld v0.94

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
/**
 
21
  @file
 
22
 
 
23
  It is interface module to fixed precision decimals library.
 
24
 
 
25
  Most functions use 'uint32_t mask' as parameter, if during operation error
 
26
  which fit in this mask is detected then it will be processed automatically
 
27
  here. (errors are E_DEC_* constants, see drizzled/decimal.h)
 
28
 
 
29
  Most function are just inline wrappers around library calls
 
30
*/
 
31
 
 
32
#ifndef DRIZZLED_MY_DECIMAL_H
 
33
#define DRIZZLED_MY_DECIMAL_H
 
34
 
 
35
#include <drizzled/decimal.h>
 
36
#include <drizzled/my_time.h>
 
37
#include <drizzled/definitions.h>
 
38
#include <drizzled/sql_string.h>
 
39
 
 
40
#define DECIMAL_LONGLONG_DIGITS 22
 
41
#define DECIMAL_LONG_DIGITS 10
 
42
#define DECIMAL_LONG3_DIGITS 8
 
43
 
 
44
/** maximum length of buffer in our big digits (uint32_t). */
 
45
#define DECIMAL_BUFF_LENGTH 9
 
46
 
 
47
/* the number of digits that my_decimal can possibly contain */
 
48
#define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
 
49
 
 
50
 
 
51
/**
 
52
  maximum guaranteed precision of number in decimal digits (number of our
 
53
  digits * number of decimal digits in one our big digit - number of decimal
 
54
  digits in one our big digit decreased by 1 (because we always put decimal
 
55
  point on the border of our big digits))
 
56
*/
 
57
#define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
 
58
#define DECIMAL_MAX_SCALE 30
 
59
#define DECIMAL_NOT_SPECIFIED 31
 
60
 
 
61
/**
 
62
  maximum length of string representation (number of maximum decimal
 
63
  digits + 1 position for sign + 1 position for decimal point)
 
64
*/
 
65
#define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
 
66
 
 
67
/**
 
68
  maximum size of packet length.
 
69
*/
 
70
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
 
71
 
 
72
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
 
73
{
 
74
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
 
75
}
 
76
 
 
77
 
 
78
/**
 
79
  my_decimal class limits 'decimal_t' type to what we need in MySQL.
 
80
 
 
81
  It contains internally all necessary space needed by the instance so
 
82
  no extra memory is needed. One should call fix_buffer_pointer() function
 
83
  when he moves my_decimal objects in memory.
 
84
*/
 
85
 
 
86
class my_decimal :public decimal_t
 
87
{
 
88
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
 
89
 
 
90
public:
 
91
 
 
92
  void init()
 
93
  {
 
94
    len= DECIMAL_BUFF_LENGTH;
 
95
    buf= buffer;
 
96
#if !defined (HAVE_purify)
 
97
    /* Set buffer to 'random' value to find wrong buffer usage */
 
98
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
 
99
      buffer[i]= i;
 
100
#endif
 
101
  }
 
102
  my_decimal()
 
103
  {
 
104
    init();
 
105
  }
 
106
  void fix_buffer_pointer() { buf= buffer; }
 
107
 
 
108
  bool sign() const { return decimal_t::sign; }
 
109
  void sign(bool s) { decimal_t::sign= s; }
 
110
  uint32_t precision() const { return intg + frac; }
 
111
};
 
112
 
 
113
int decimal_operation_results(int result);
 
114
 
 
115
inline
 
116
void max_my_decimal(my_decimal *to, int precision, int frac)
 
117
{
 
118
  assert((precision <= DECIMAL_MAX_PRECISION)&&
 
119
              (frac <= DECIMAL_MAX_SCALE));
 
120
  max_decimal(precision, frac, (decimal_t*) to);
 
121
}
 
122
 
 
123
inline void max_internal_decimal(my_decimal *to)
 
124
{
 
125
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
 
126
}
 
127
 
 
128
inline int check_result(uint32_t mask, int result)
 
129
{
 
130
  if (result & mask)
 
131
    decimal_operation_results(result);
 
132
  return result;
 
133
}
 
134
 
 
135
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
 
136
{
 
137
  if (check_result(mask, result) & E_DEC_OVERFLOW)
 
138
  {
 
139
    bool sign= val->sign();
 
140
    val->fix_buffer_pointer();
 
141
    max_internal_decimal(val);
 
142
    val->sign(sign);
 
143
  }
 
144
  return result;
 
145
}
 
146
 
 
147
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
 
148
                                           bool unsigned_flag)
 
149
{
 
150
  return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
 
151
}
 
152
 
 
153
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
 
154
                                             bool unsigned_flag)
 
155
{
 
156
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
 
157
  return static_cast<uint32_t>(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
 
158
}
 
159
 
 
160
inline
 
161
int my_decimal_string_length(const my_decimal *d)
 
162
{
 
163
  return decimal_string_size(d);
 
164
}
 
165
 
 
166
 
 
167
inline
 
168
int my_decimal_max_length(const my_decimal *d)
 
169
{
 
170
  /* -1 because we do not count \0 */
 
171
  return decimal_string_size(d) - 1;
 
172
}
 
173
 
 
174
 
 
175
inline
 
176
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
 
177
{
 
178
  return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
 
179
}
 
180
 
 
181
 
 
182
inline
 
183
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
 
184
{
 
185
  *to= *from;
 
186
  to->fix_buffer_pointer();
 
187
}
 
188
 
 
189
 
 
190
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
 
191
                      int scale);
 
192
 
 
193
 
 
194
inline
 
195
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
 
196
                      int scale)
 
197
{
 
198
  return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
 
199
}
 
200
 
 
201
 
 
202
inline
 
203
int my_decimal_set_zero(my_decimal *d)
 
204
{
 
205
  decimal_make_zero(static_cast<decimal_t*> (d));
 
206
  return 0;
 
207
}
 
208
 
 
209
 
 
210
inline
 
211
bool my_decimal_is_zero(const my_decimal *decimal_value)
 
212
{
 
213
  return decimal_is_zero(static_cast<const decimal_t*>(decimal_value));
 
214
}
 
215
 
 
216
 
 
217
inline
 
218
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
 
219
                     bool truncate, my_decimal *to)
 
220
{
 
221
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
 
222
                                          (truncate ? TRUNCATE : HALF_UP)));
 
223
}
 
224
 
 
225
 
 
226
inline
 
227
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
 
228
{
 
229
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
 
230
}
 
231
 
 
232
 
 
233
inline
 
234
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
 
235
{
 
236
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
 
237
}
 
238
 
 
239
 
 
240
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
 
241
                      uint32_t fixed_dec, char filler, String *str);
 
242
 
 
243
inline
 
244
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
 
245
                   int64_t *l)
 
246
{
 
247
  my_decimal rounded;
 
248
  /* decimal_round can return only E_DEC_TRUNCATED */
 
249
  decimal_round(static_cast<const decimal_t*>(d), &rounded, 0, HALF_UP);
 
250
  return check_result(mask, (unsigned_flag ?
 
251
                             decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
 
252
                             decimal2int64_t(&rounded, l)));
 
253
}
 
254
 
 
255
 
 
256
inline
 
257
int my_decimal2double(uint32_t, const my_decimal *d, double *result)
 
258
{
 
259
  /* No need to call check_result as this will always succeed */
 
260
  return decimal2double(static_cast<const decimal_t*>(d), result);
 
261
}
 
262
 
 
263
 
 
264
inline
 
265
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
 
266
{
 
267
  return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(d),end),
 
268
                                   d);
 
269
}
 
270
 
 
271
 
 
272
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
 
273
                   const CHARSET_INFO * charset, my_decimal *decimal_value);
 
274
 
 
275
inline
 
276
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
 
277
{
 
278
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
 
279
}
 
280
 
 
281
 
 
282
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
 
283
 
 
284
 
 
285
inline
 
286
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
 
287
{
 
288
  return check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)), d);
 
289
}
 
290
 
 
291
 
 
292
inline
 
293
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
 
294
{
 
295
  return check_result(mask, (unsigned_flag ?
 
296
                             uint64_t2decimal(static_cast<uint64_t>(i), d) :
 
297
                             int64_t2decimal(i, d)));
 
298
}
 
299
 
 
300
 
 
301
inline
 
302
void my_decimal_neg(decimal_t *arg)
 
303
{
 
304
  if (decimal_is_zero(arg))
 
305
  {
 
306
    arg->sign= 0;
 
307
    return;
 
308
  }
 
309
  decimal_neg(arg);
 
310
}
 
311
 
 
312
 
 
313
inline
 
314
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
 
315
                   const my_decimal *b)
 
316
{
 
317
  return check_result_and_overflow(mask,
 
318
                                   decimal_add(static_cast<const decimal_t*>(a),
 
319
                                               static_cast<const decimal_t*>(b), res),
 
320
                                   res);
 
321
}
 
322
 
 
323
 
 
324
inline
 
325
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
 
326
                   const my_decimal *b)
 
327
{
 
328
  return check_result_and_overflow(mask,
 
329
                                   decimal_sub(static_cast<const decimal_t*>(a),
 
330
                                               static_cast<const decimal_t*>(b), res),
 
331
                                   res);
 
332
}
 
333
 
 
334
 
 
335
inline
 
336
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
 
337
                   const my_decimal *b)
 
338
{
 
339
  return check_result_and_overflow(mask,
 
340
                                   decimal_mul(static_cast<const decimal_t*>(a),
 
341
                                               static_cast<const decimal_t*>(b),res),
 
342
                                   res);
 
343
}
 
344
 
 
345
 
 
346
inline
 
347
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
 
348
                   const my_decimal *b, int div_scale_inc)
 
349
{
 
350
  return check_result_and_overflow(mask,
 
351
                                   decimal_div(static_cast<const decimal_t*>(a),
 
352
                                               static_cast<const decimal_t*>(b),res,
 
353
                                               div_scale_inc),
 
354
                                   res);
 
355
}
 
356
 
 
357
 
 
358
inline
 
359
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
 
360
                   const my_decimal *b)
 
361
{
 
362
  return check_result_and_overflow(mask,
 
363
                                   decimal_mod(static_cast<const decimal_t*>(a),
 
364
                                               static_cast<const decimal_t*>(b),res),
 
365
                                   res);
 
366
}
 
367
 
 
368
 
 
369
/**
 
370
  @return
 
371
    -1 if a<b, 1 if a>b and 0 if a==b
 
372
*/
 
373
inline
 
374
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
 
375
{
 
376
  return decimal_cmp(static_cast<const decimal_t*>(a),
 
377
                     static_cast<const decimal_t*>(b));
 
378
}
 
379
 
 
380
 
 
381
inline
 
382
int my_decimal_intg(const my_decimal *a)
 
383
{
 
384
  return decimal_intg(static_cast<const decimal_t*>(a));
 
385
}
 
386
 
 
387
 
 
388
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
 
389
 
 
390
 
 
391
#endif /* DRIZZLED_MY_DECIMAL_H */