~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/my_decimal.h

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2005-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
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
 */
15
19
 
16
20
/**
17
21
  @file
18
22
 
19
23
  It is interface module to fixed precision decimals library.
20
24
 
21
 
  Most functions use 'uint mask' as parameter, if during operation error
 
25
  Most functions use 'uint32_t mask' as parameter, if during operation error
22
26
  which fit in this mask is detected then it will be processed automatically
23
27
  here. (errors are E_DEC_* constants, see include/decimal.h)
24
28
 
28
32
#ifndef my_decimal_h
29
33
#define my_decimal_h
30
34
 
31
 
C_MODE_START
32
 
#include <decimal.h>
33
 
C_MODE_END
 
35
#ifdef __cplusplus
 
36
extern "C" {
 
37
#endif
 
38
 
 
39
#include <mystrings/decimal.h>
 
40
 
 
41
#ifdef __cplusplus
 
42
}
 
43
#endif
 
44
 
34
45
 
35
46
#define DECIMAL_LONGLONG_DIGITS 22
36
47
#define DECIMAL_LONG_DIGITS 10
37
48
#define DECIMAL_LONG3_DIGITS 8
38
49
 
39
 
/** maximum length of buffer in our big digits (uint32). */
 
50
/** maximum length of buffer in our big digits (uint32_t). */
40
51
#define DECIMAL_BUFF_LENGTH 9
41
52
 
42
53
/* the number of digits that my_decimal can possibly contain */
65
76
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
66
77
 
67
78
 
68
 
inline uint my_decimal_size(uint precision, uint scale)
 
79
inline uint32_t my_decimal_size(uint32_t precision, uint32_t scale)
69
80
{
70
81
  /*
71
82
    Always allocate more space to allow library to put decimal point
75
86
}
76
87
 
77
88
 
78
 
inline int my_decimal_int_part(uint precision, uint decimals)
 
89
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
79
90
{
80
91
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
81
92
}
101
112
    buf= buffer;
102
113
#if !defined (HAVE_purify) 
103
114
    /* Set buffer to 'random' value to find wrong buffer usage */
104
 
    for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
 
115
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
105
116
      buffer[i]= i;
106
117
#endif
107
118
  }
113
124
 
114
125
  bool sign() const { return decimal_t::sign; }
115
126
  void sign(bool s) { decimal_t::sign= s; }
116
 
  uint precision() const { return intg + frac; }
 
127
  uint32_t precision() const { return intg + frac; }
117
128
};
118
129
 
119
 
#ifndef MYSQL_CLIENT
120
130
int decimal_operation_results(int result);
121
 
#else
122
 
inline int decimal_operation_results(int result)
123
 
{
124
 
  return result;
125
 
}
126
 
#endif /*MYSQL_CLIENT*/
127
131
 
128
132
inline
129
133
void max_my_decimal(my_decimal *to, int precision, int frac)
138
142
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
139
143
}
140
144
 
141
 
inline int check_result(uint mask, int result)
 
145
inline int check_result(uint32_t mask, int result)
142
146
{
143
147
  if (result & mask)
144
148
    decimal_operation_results(result);
145
149
  return result;
146
150
}
147
151
 
148
 
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
 
152
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
149
153
{
150
154
  if (check_result(mask, result) & E_DEC_OVERFLOW)
151
155
  {
157
161
  return result;
158
162
}
159
163
 
160
 
inline uint my_decimal_length_to_precision(uint length, uint scale,
 
164
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
161
165
                                           bool unsigned_flag)
162
166
{
163
167
  return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
164
168
}
165
169
 
166
 
inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
 
170
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
167
171
                                             bool unsigned_flag)
168
172
{
169
173
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
170
 
  return (uint32)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
 
174
  return (uint32_t)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
171
175
}
172
176
 
173
177
inline
186
190
 
187
191
 
188
192
inline
189
 
int my_decimal_get_binary_size(uint precision, uint scale)
 
193
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
190
194
{
191
195
  return decimal_bin_size((int)precision, (int)scale);
192
196
}
200
204
}
201
205
 
202
206
 
203
 
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
 
207
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
204
208
                      int scale);
205
209
 
206
210
 
207
211
inline
208
 
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
 
212
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
209
213
                      int scale)
210
214
{
211
215
  return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
228
232
 
229
233
 
230
234
inline
231
 
int my_decimal_round(uint mask, const my_decimal *from, int scale,
 
235
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
232
236
                     bool truncate, my_decimal *to)
233
237
{
234
238
  return check_result(mask, decimal_round((decimal_t*) from, to, scale,
237
241
 
238
242
 
239
243
inline
240
 
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
 
244
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
241
245
{
242
246
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
243
247
}
244
248
 
245
249
 
246
250
inline
247
 
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
 
251
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
248
252
{
249
253
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
250
254
}
251
255
 
252
256
 
253
 
#ifndef MYSQL_CLIENT
254
 
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
255
 
                      uint fixed_dec, char filler, String *str);
256
 
#endif
 
257
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
 
258
                      uint32_t fixed_dec, char filler, String *str);
257
259
 
258
260
inline
259
 
int my_decimal2int(uint mask, const my_decimal *d, my_bool unsigned_flag,
 
261
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
260
262
                   int64_t *l)
261
263
{
262
264
  my_decimal rounded;
269
271
 
270
272
 
271
273
inline
272
 
int my_decimal2double(uint mask __attribute__((__unused__)), 
 
274
int my_decimal2double(uint32_t mask __attribute__((unused)), 
273
275
                      const my_decimal *d, double *result)
274
276
{
275
277
  /* No need to call check_result as this will always succeed */
278
280
 
279
281
 
280
282
inline
281
 
int str2my_decimal(uint mask, const char *str, my_decimal *d, char **end)
 
283
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
282
284
{
283
285
  return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
284
286
                                   d);
285
287
}
286
288
 
287
289
 
288
 
int str2my_decimal(uint mask, const char *from, uint length,
289
 
                   CHARSET_INFO *charset, my_decimal *decimal_value);
 
290
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
 
291
                   const CHARSET_INFO * charset, my_decimal *decimal_value);
290
292
 
291
 
#if defined(MYSQL_SERVER)
 
293
#if defined(DRIZZLE_SERVER)
292
294
inline
293
 
int string2my_decimal(uint mask, const String *str, my_decimal *d)
 
295
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
294
296
{
295
297
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
296
298
}
297
299
 
298
300
 
299
 
my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec);
300
 
 
301
 
 
302
 
#endif /*defined(MYSQL_SERVER) */
 
301
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
 
302
 
 
303
 
 
304
#endif /*defined(DRIZZLE_SERVER) */
303
305
 
304
306
inline
305
 
int double2my_decimal(uint mask, double val, my_decimal *d)
 
307
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
306
308
{
307
309
  return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
308
310
}
309
311
 
310
312
 
311
313
inline
312
 
int int2my_decimal(uint mask, int64_t i, my_bool unsigned_flag, my_decimal *d)
 
314
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
313
315
{
314
316
  return check_result(mask, (unsigned_flag ?
315
317
                             uint64_t2decimal((uint64_t)i, d) :
330
332
 
331
333
 
332
334
inline
333
 
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
 
335
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
334
336
                   const my_decimal *b)
335
337
{
336
338
  return check_result_and_overflow(mask,
340
342
 
341
343
 
342
344
inline
343
 
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
 
345
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
344
346
                   const my_decimal *b)
345
347
{
346
348
  return check_result_and_overflow(mask,
350
352
 
351
353
 
352
354
inline
353
 
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
 
355
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
354
356
                   const my_decimal *b)
355
357
{
356
358
  return check_result_and_overflow(mask,
360
362
 
361
363
 
362
364
inline
363
 
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
 
365
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
364
366
                   const my_decimal *b, int div_scale_inc)
365
367
{
366
368
  return check_result_and_overflow(mask,
371
373
 
372
374
 
373
375
inline
374
 
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
 
376
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
375
377
                   const my_decimal *b)
376
378
{
377
379
  return check_result_and_overflow(mask,
398
400
}
399
401
 
400
402
 
401
 
void my_decimal_trim(ulong *precision, uint *scale);
 
403
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
402
404
 
403
405
 
404
406
#endif /*my_decimal_h*/