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