~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/my_decimal.h

  • Committer: Brian Aker
  • Date: 2008-07-01 20:14:24 UTC
  • Revision ID: brian@tangent.org-20080701201424-rsof5enxl7gkr50p
More cleanup on pread()

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
 
 */
 
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 */
19
15
 
20
16
/**
21
17
  @file
22
18
 
23
19
  It is interface module to fixed precision decimals library.
24
20
 
25
 
  Most functions use 'uint32_t mask' as parameter, if during operation error
 
21
  Most functions use 'uint mask' as parameter, if during operation error
26
22
  which fit in this mask is detected then it will be processed automatically
27
23
  here. (errors are E_DEC_* constants, see include/decimal.h)
28
24
 
32
28
#ifndef my_decimal_h
33
29
#define my_decimal_h
34
30
 
35
 
#ifdef __cplusplus
36
 
extern "C" {
37
 
#endif
38
 
 
39
 
#include <mystrings/decimal.h>
40
 
 
41
 
#ifdef __cplusplus
42
 
}
43
 
#endif
44
 
 
 
31
C_MODE_START
 
32
#include <decimal.h>
 
33
C_MODE_END
45
34
 
46
35
#define DECIMAL_LONGLONG_DIGITS 22
47
36
#define DECIMAL_LONG_DIGITS 10
48
37
#define DECIMAL_LONG3_DIGITS 8
49
38
 
50
 
/** maximum length of buffer in our big digits (uint32_t). */
 
39
/** maximum length of buffer in our big digits (uint32). */
51
40
#define DECIMAL_BUFF_LENGTH 9
52
41
 
53
42
/* the number of digits that my_decimal can possibly contain */
76
65
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
77
66
 
78
67
 
79
 
inline uint32_t my_decimal_size(uint32_t precision, uint32_t scale)
 
68
inline uint my_decimal_size(uint precision, uint scale)
80
69
{
81
70
  /*
82
71
    Always allocate more space to allow library to put decimal point
86
75
}
87
76
 
88
77
 
89
 
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
 
78
inline int my_decimal_int_part(uint precision, uint decimals)
90
79
{
91
80
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
92
81
}
110
99
  {
111
100
    len= DECIMAL_BUFF_LENGTH;
112
101
    buf= buffer;
113
 
#if !defined (HAVE_purify) 
 
102
#if !defined (HAVE_purify) && !defined(DBUG_OFF)
114
103
    /* Set buffer to 'random' value to find wrong buffer usage */
115
 
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
 
104
    for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
116
105
      buffer[i]= i;
117
106
#endif
118
107
  }
124
113
 
125
114
  bool sign() const { return decimal_t::sign; }
126
115
  void sign(bool s) { decimal_t::sign= s; }
127
 
  uint32_t precision() const { return intg + frac; }
 
116
  uint precision() const { return intg + frac; }
128
117
};
129
118
 
 
119
 
 
120
#ifndef DBUG_OFF
 
121
void print_decimal(const my_decimal *dec);
 
122
void print_decimal_buff(const my_decimal *dec, const uchar* ptr, int length);
 
123
const char *dbug_decimal_as_string(char *buff, const my_decimal *val);
 
124
#else
 
125
#define dbug_decimal_as_string(A) NULL
 
126
#endif
 
127
 
 
128
#ifndef MYSQL_CLIENT
130
129
int decimal_operation_results(int result);
 
130
#else
 
131
inline int decimal_operation_results(int result)
 
132
{
 
133
  return result;
 
134
}
 
135
#endif /*MYSQL_CLIENT*/
131
136
 
132
137
inline
133
138
void max_my_decimal(my_decimal *to, int precision, int frac)
134
139
{
135
 
  assert((precision <= DECIMAL_MAX_PRECISION)&&
 
140
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION)&&
136
141
              (frac <= DECIMAL_MAX_SCALE));
137
142
  max_decimal(precision, frac, (decimal_t*) to);
138
143
}
142
147
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
143
148
}
144
149
 
145
 
inline int check_result(uint32_t mask, int result)
 
150
inline int check_result(uint mask, int result)
146
151
{
147
152
  if (result & mask)
148
153
    decimal_operation_results(result);
149
154
  return result;
150
155
}
151
156
 
152
 
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
 
157
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
153
158
{
154
159
  if (check_result(mask, result) & E_DEC_OVERFLOW)
155
160
  {
161
166
  return result;
162
167
}
163
168
 
164
 
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
 
169
inline uint my_decimal_length_to_precision(uint length, uint scale,
165
170
                                           bool unsigned_flag)
166
171
{
167
172
  return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
168
173
}
169
174
 
170
 
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
 
175
inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
171
176
                                             bool unsigned_flag)
172
177
{
173
178
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
174
 
  return (uint32_t)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
 
179
  return (uint32)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
175
180
}
176
181
 
177
182
inline
190
195
 
191
196
 
192
197
inline
193
 
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
 
198
int my_decimal_get_binary_size(uint precision, uint scale)
194
199
{
195
200
  return decimal_bin_size((int)precision, (int)scale);
196
201
}
204
209
}
205
210
 
206
211
 
207
 
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
 
212
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
208
213
                      int scale);
209
214
 
210
215
 
211
216
inline
212
 
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
 
217
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
213
218
                      int scale)
214
219
{
215
220
  return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
232
237
 
233
238
 
234
239
inline
235
 
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
 
240
int my_decimal_round(uint mask, const my_decimal *from, int scale,
236
241
                     bool truncate, my_decimal *to)
237
242
{
238
243
  return check_result(mask, decimal_round((decimal_t*) from, to, scale,
241
246
 
242
247
 
243
248
inline
244
 
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
 
249
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
245
250
{
246
251
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
247
252
}
248
253
 
249
254
 
250
255
inline
251
 
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
 
256
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
252
257
{
253
258
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
254
259
}
255
260
 
256
261
 
257
 
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
258
 
                      uint32_t fixed_dec, char filler, String *str);
 
262
#ifndef MYSQL_CLIENT
 
263
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
 
264
                      uint fixed_dec, char filler, String *str);
 
265
#endif
259
266
 
260
267
inline
261
 
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
262
 
                   int64_t *l)
 
268
int my_decimal2int(uint mask, const my_decimal *d, my_bool unsigned_flag,
 
269
                   longlong *l)
263
270
{
264
271
  my_decimal rounded;
265
272
  /* decimal_round can return only E_DEC_TRUNCATED */
266
273
  decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
267
274
  return check_result(mask, (unsigned_flag ?
268
 
                             decimal2uint64_t(&rounded, (uint64_t *)l) :
269
 
                             decimal2int64_t(&rounded, l)));
 
275
                             decimal2ulonglong(&rounded, (ulonglong *)l) :
 
276
                             decimal2longlong(&rounded, l)));
270
277
}
271
278
 
272
279
 
273
280
inline
274
 
int my_decimal2double(uint32_t mask __attribute__((unused)), 
275
 
                      const my_decimal *d, double *result)
 
281
int my_decimal2double(uint mask, const my_decimal *d, double *result)
276
282
{
277
283
  /* No need to call check_result as this will always succeed */
278
284
  return decimal2double((decimal_t*) d, result);
280
286
 
281
287
 
282
288
inline
283
 
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
 
289
int str2my_decimal(uint mask, const char *str, my_decimal *d, char **end)
284
290
{
285
291
  return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
286
292
                                   d);
287
293
}
288
294
 
289
295
 
290
 
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
291
 
                   const CHARSET_INFO * charset, my_decimal *decimal_value);
 
296
int str2my_decimal(uint mask, const char *from, uint length,
 
297
                   CHARSET_INFO *charset, my_decimal *decimal_value);
292
298
 
293
 
#if defined(DRIZZLE_SERVER)
 
299
#if defined(MYSQL_SERVER)
294
300
inline
295
 
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
 
301
int string2my_decimal(uint mask, const String *str, my_decimal *d)
296
302
{
297
303
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
298
304
}
299
305
 
300
306
 
301
 
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
302
 
 
303
 
 
304
 
#endif /*defined(DRIZZLE_SERVER) */
 
307
my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec);
 
308
 
 
309
 
 
310
#endif /*defined(MYSQL_SERVER) */
305
311
 
306
312
inline
307
 
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
 
313
int double2my_decimal(uint mask, double val, my_decimal *d)
308
314
{
309
315
  return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
310
316
}
311
317
 
312
318
 
313
319
inline
314
 
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
 
320
int int2my_decimal(uint mask, longlong i, my_bool unsigned_flag, my_decimal *d)
315
321
{
316
322
  return check_result(mask, (unsigned_flag ?
317
 
                             uint64_t2decimal((uint64_t)i, d) :
318
 
                             int64_t2decimal(i, d)));
 
323
                             ulonglong2decimal((ulonglong)i, d) :
 
324
                             longlong2decimal(i, d)));
319
325
}
320
326
 
321
327
 
332
338
 
333
339
 
334
340
inline
335
 
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
 
341
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
336
342
                   const my_decimal *b)
337
343
{
338
344
  return check_result_and_overflow(mask,
342
348
 
343
349
 
344
350
inline
345
 
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
 
351
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
346
352
                   const my_decimal *b)
347
353
{
348
354
  return check_result_and_overflow(mask,
352
358
 
353
359
 
354
360
inline
355
 
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
 
361
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
356
362
                   const my_decimal *b)
357
363
{
358
364
  return check_result_and_overflow(mask,
362
368
 
363
369
 
364
370
inline
365
 
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
 
371
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
366
372
                   const my_decimal *b, int div_scale_inc)
367
373
{
368
374
  return check_result_and_overflow(mask,
373
379
 
374
380
 
375
381
inline
376
 
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
 
382
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
377
383
                   const my_decimal *b)
378
384
{
379
385
  return check_result_and_overflow(mask,
400
406
}
401
407
 
402
408
 
403
 
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
 
409
void my_decimal_trim(ulong *precision, uint *scale);
404
410
 
405
411
 
406
412
#endif /*my_decimal_h*/