~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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
 */
1 by brian
clean slate
19
20
/**
21
  @file
22
23
  It is interface module to fixed precision decimals library.
24
482 by Brian Aker
Remove uint.
25
  Most functions use 'uint32_t mask' as parameter, if during operation error
1 by brian
clean slate
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 my_decimal_h
33
#define my_decimal_h
34
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
35
#ifdef __cplusplus
36
extern "C" {
37
#endif
38
212.5.4 by Monty Taylor
Renamed strings to mystrings, for include/lib naming consistency.
39
#include <mystrings/decimal.h>
520.8.3 by Monty Taylor
Moved hash back to mysys.
40
#include <mysys/my_time.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
41
#include <drizzled/sql_string.h>
398.1.9 by Monty Taylor
Cleaned up stuff out of global.h.
42
43
#ifdef __cplusplus
44
}
45
#endif
46
1 by brian
clean slate
47
48
#define DECIMAL_LONGLONG_DIGITS 22
49
#define DECIMAL_LONG_DIGITS 10
50
#define DECIMAL_LONG3_DIGITS 8
51
205 by Brian Aker
uint32 -> uin32_t
52
/** maximum length of buffer in our big digits (uint32_t). */
1 by brian
clean slate
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
482 by Brian Aker
Remove uint.
81
inline uint32_t my_decimal_size(uint32_t precision, uint32_t scale)
1 by brian
clean slate
82
{
83
  /*
84
    Always allocate more space to allow library to put decimal point
85
    where it want
86
  */
87
  return decimal_size(precision, scale) + 1;
88
}
89
90
482 by Brian Aker
Remove uint.
91
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
1 by brian
clean slate
92
{
93
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
94
}
95
96
97
/**
98
  my_decimal class limits 'decimal_t' type to what we need in MySQL.
99
100
  It contains internally all necessary space needed by the instance so
101
  no extra memory is needed. One should call fix_buffer_pointer() function
102
  when he moves my_decimal objects in memory.
103
*/
104
105
class my_decimal :public decimal_t
106
{
107
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
108
109
public:
110
111
  void init()
112
  {
113
    len= DECIMAL_BUFF_LENGTH;
114
    buf= buffer;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
115
#if !defined (HAVE_purify)
1 by brian
clean slate
116
    /* Set buffer to 'random' value to find wrong buffer usage */
482 by Brian Aker
Remove uint.
117
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
1 by brian
clean slate
118
      buffer[i]= i;
119
#endif
120
  }
121
  my_decimal()
122
  {
123
    init();
124
  }
125
  void fix_buffer_pointer() { buf= buffer; }
126
127
  bool sign() const { return decimal_t::sign; }
128
  void sign(bool s) { decimal_t::sign= s; }
482 by Brian Aker
Remove uint.
129
  uint32_t precision() const { return intg + frac; }
1 by brian
clean slate
130
};
131
132
int decimal_operation_results(int result);
133
134
inline
135
void max_my_decimal(my_decimal *to, int precision, int frac)
136
{
51.1.33 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
137
  assert((precision <= DECIMAL_MAX_PRECISION)&&
1 by brian
clean slate
138
              (frac <= DECIMAL_MAX_SCALE));
139
  max_decimal(precision, frac, (decimal_t*) to);
140
}
141
142
inline void max_internal_decimal(my_decimal *to)
143
{
144
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
145
}
146
482 by Brian Aker
Remove uint.
147
inline int check_result(uint32_t mask, int result)
1 by brian
clean slate
148
{
149
  if (result & mask)
150
    decimal_operation_results(result);
151
  return result;
152
}
153
482 by Brian Aker
Remove uint.
154
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
1 by brian
clean slate
155
{
156
  if (check_result(mask, result) & E_DEC_OVERFLOW)
157
  {
158
    bool sign= val->sign();
159
    val->fix_buffer_pointer();
160
    max_internal_decimal(val);
161
    val->sign(sign);
162
  }
163
  return result;
164
}
165
482 by Brian Aker
Remove uint.
166
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
1 by brian
clean slate
167
                                           bool unsigned_flag)
168
{
895 by Brian Aker
Completion (?) of uint conversion.
169
  return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
1 by brian
clean slate
170
}
171
482 by Brian Aker
Remove uint.
172
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
1 by brian
clean slate
173
                                             bool unsigned_flag)
174
{
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
175
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
205 by Brian Aker
uint32 -> uin32_t
176
  return (uint32_t)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
1 by brian
clean slate
177
}
178
179
inline
180
int my_decimal_string_length(const my_decimal *d)
181
{
182
  return decimal_string_size(d);
183
}
184
185
186
inline
187
int my_decimal_max_length(const my_decimal *d)
188
{
189
  /* -1 because we do not count \0 */
190
  return decimal_string_size(d) - 1;
191
}
192
193
194
inline
482 by Brian Aker
Remove uint.
195
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
1 by brian
clean slate
196
{
197
  return decimal_bin_size((int)precision, (int)scale);
198
}
199
200
201
inline
202
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
203
{
204
  *to= *from;
205
  to->fix_buffer_pointer();
206
}
207
208
482 by Brian Aker
Remove uint.
209
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
1 by brian
clean slate
210
		      int scale);
211
212
213
inline
482 by Brian Aker
Remove uint.
214
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
1 by brian
clean slate
215
		      int scale)
216
{
217
  return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
218
}
219
220
221
inline
222
int my_decimal_set_zero(my_decimal *d)
223
{
224
  decimal_make_zero(((decimal_t*) d));
225
  return 0;
226
}
227
228
229
inline
230
bool my_decimal_is_zero(const my_decimal *decimal_value)
231
{
232
  return decimal_is_zero((decimal_t*) decimal_value);
233
}
234
235
236
inline
482 by Brian Aker
Remove uint.
237
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
1 by brian
clean slate
238
                     bool truncate, my_decimal *to)
239
{
240
  return check_result(mask, decimal_round((decimal_t*) from, to, scale,
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
241
                                          (truncate ? TRUNCATE : HALF_UP)));
1 by brian
clean slate
242
}
243
244
245
inline
482 by Brian Aker
Remove uint.
246
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
1 by brian
clean slate
247
{
248
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
249
}
250
251
252
inline
482 by Brian Aker
Remove uint.
253
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
1 by brian
clean slate
254
{
255
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
256
}
257
258
482 by Brian Aker
Remove uint.
259
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
260
                      uint32_t fixed_dec, char filler, String *str);
1 by brian
clean slate
261
262
inline
482 by Brian Aker
Remove uint.
263
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
264
                   int64_t *l)
1 by brian
clean slate
265
{
266
  my_decimal rounded;
267
  /* decimal_round can return only E_DEC_TRUNCATED */
268
  decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
269
  return check_result(mask, (unsigned_flag ?
151 by Brian Aker
Ulonglong to uint64_t
270
			     decimal2uint64_t(&rounded, (uint64_t *)l) :
152 by Brian Aker
longlong replacement
271
			     decimal2int64_t(&rounded, l)));
1 by brian
clean slate
272
}
273
274
275
inline
646 by Brian Aker
Next pass through attribute.
276
int my_decimal2double(uint32_t, const my_decimal *d, double *result)
1 by brian
clean slate
277
{
278
  /* No need to call check_result as this will always succeed */
279
  return decimal2double((decimal_t*) d, result);
280
}
281
282
283
inline
482 by Brian Aker
Remove uint.
284
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
1 by brian
clean slate
285
{
286
  return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
287
                                   d);
288
}
289
290
482 by Brian Aker
Remove uint.
291
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
292
                   const CHARSET_INFO * charset, my_decimal *decimal_value);
1 by brian
clean slate
293
294
inline
482 by Brian Aker
Remove uint.
295
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
1 by brian
clean slate
296
{
297
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
298
}
299
300
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
301
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
1 by brian
clean slate
302
303
304
inline
482 by Brian Aker
Remove uint.
305
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
1 by brian
clean slate
306
{
307
  return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
308
}
309
310
311
inline
482 by Brian Aker
Remove uint.
312
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
1 by brian
clean slate
313
{
314
  return check_result(mask, (unsigned_flag ?
151 by Brian Aker
Ulonglong to uint64_t
315
			     uint64_t2decimal((uint64_t)i, d) :
152 by Brian Aker
longlong replacement
316
			     int64_t2decimal(i, d)));
1 by brian
clean slate
317
}
318
319
320
inline
321
void my_decimal_neg(decimal_t *arg)
322
{
323
  if (decimal_is_zero(arg))
324
  {
325
    arg->sign= 0;
326
    return;
327
  }
328
  decimal_neg(arg);
329
}
330
331
332
inline
482 by Brian Aker
Remove uint.
333
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
1 by brian
clean slate
334
		   const my_decimal *b)
335
{
336
  return check_result_and_overflow(mask,
337
                                   decimal_add((decimal_t*)a,(decimal_t*)b,res),
338
                                   res);
339
}
340
341
342
inline
482 by Brian Aker
Remove uint.
343
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
1 by brian
clean slate
344
		   const my_decimal *b)
345
{
346
  return check_result_and_overflow(mask,
347
                                   decimal_sub((decimal_t*)a,(decimal_t*)b,res),
348
                                   res);
349
}
350
351
352
inline
482 by Brian Aker
Remove uint.
353
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
1 by brian
clean slate
354
		   const my_decimal *b)
355
{
356
  return check_result_and_overflow(mask,
357
                                   decimal_mul((decimal_t*)a,(decimal_t*)b,res),
358
                                   res);
359
}
360
361
362
inline
482 by Brian Aker
Remove uint.
363
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
1 by brian
clean slate
364
		   const my_decimal *b, int div_scale_inc)
365
{
366
  return check_result_and_overflow(mask,
367
                                   decimal_div((decimal_t*)a,(decimal_t*)b,res,
368
                                               div_scale_inc),
369
                                   res);
370
}
371
372
373
inline
482 by Brian Aker
Remove uint.
374
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
1 by brian
clean slate
375
		   const my_decimal *b)
376
{
377
  return check_result_and_overflow(mask,
378
                                   decimal_mod((decimal_t*)a,(decimal_t*)b,res),
379
                                   res);
380
}
381
382
383
/**
384
  @return
385
    -1 if a<b, 1 if a>b and 0 if a==b
386
*/
387
inline
388
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
389
{
390
  return decimal_cmp((decimal_t*) a, (decimal_t*) b);
391
}
392
393
394
inline
395
int my_decimal_intg(const my_decimal *a)
396
{
397
  return decimal_intg((decimal_t*) a);
398
}
399
400
482 by Brian Aker
Remove uint.
401
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
1 by brian
clean slate
402
403
404
#endif /*my_decimal_h*/
405