~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/my_decimal.h

  • Committer: Stewart Smith
  • Date: 2009-12-02 06:01:21 UTC
  • mto: (1237.1.2 push)
  • mto: This revision was merged to the branch mainline in revision 1238.
  • Revision ID: stewart@flamingspork.com-20091202060121-68gyfqifqcjcmi2v
my_end() no longer requires an argument (we removed them all)

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