~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/my_decimal.h

  • Committer: Monty Taylor
  • Date: 2008-10-05 01:41:06 UTC
  • Revision ID: monty@inaugust.com-20081005014106-bulqe4kp7i6ipts1
Moved qsort declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
  It is interface module to fixed precision decimals library.
24
24
 
25
 
  Most functions use 'uint32_t mask' as parameter, if during operation error
 
25
  Most functions use 'uint mask' as parameter, if during operation error
26
26
  which fit in this mask is detected then it will be processed automatically
27
27
  here. (errors are E_DEC_* constants, see include/decimal.h)
28
28
 
76
76
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
77
77
 
78
78
 
79
 
inline uint32_t my_decimal_size(uint32_t precision, uint32_t scale)
 
79
inline uint my_decimal_size(uint precision, uint scale)
80
80
{
81
81
  /*
82
82
    Always allocate more space to allow library to put decimal point
86
86
}
87
87
 
88
88
 
89
 
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
 
89
inline int my_decimal_int_part(uint precision, uint decimals)
90
90
{
91
91
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
92
92
}
112
112
    buf= buffer;
113
113
#if !defined (HAVE_purify) 
114
114
    /* Set buffer to 'random' value to find wrong buffer usage */
115
 
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
 
115
    for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
116
116
      buffer[i]= i;
117
117
#endif
118
118
  }
124
124
 
125
125
  bool sign() const { return decimal_t::sign; }
126
126
  void sign(bool s) { decimal_t::sign= s; }
127
 
  uint32_t precision() const { return intg + frac; }
 
127
  uint precision() const { return intg + frac; }
128
128
};
129
129
 
 
130
#ifndef DRIZZLE_CLIENT
130
131
int decimal_operation_results(int result);
 
132
#else
 
133
inline int decimal_operation_results(int result)
 
134
{
 
135
  return result;
 
136
}
 
137
#endif /*DRIZZLE_CLIENT*/
131
138
 
132
139
inline
133
140
void max_my_decimal(my_decimal *to, int precision, int frac)
142
149
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
143
150
}
144
151
 
145
 
inline int check_result(uint32_t mask, int result)
 
152
inline int check_result(uint mask, int result)
146
153
{
147
154
  if (result & mask)
148
155
    decimal_operation_results(result);
149
156
  return result;
150
157
}
151
158
 
152
 
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
 
159
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
153
160
{
154
161
  if (check_result(mask, result) & E_DEC_OVERFLOW)
155
162
  {
161
168
  return result;
162
169
}
163
170
 
164
 
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
 
171
inline uint my_decimal_length_to_precision(uint length, uint scale,
165
172
                                           bool unsigned_flag)
166
173
{
167
174
  return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
168
175
}
169
176
 
170
 
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
 
177
inline uint32_t my_decimal_precision_to_length(uint precision, uint8_t scale,
171
178
                                             bool unsigned_flag)
172
179
{
173
180
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
190
197
 
191
198
 
192
199
inline
193
 
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
 
200
int my_decimal_get_binary_size(uint precision, uint scale)
194
201
{
195
202
  return decimal_bin_size((int)precision, (int)scale);
196
203
}
204
211
}
205
212
 
206
213
 
207
 
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
 
214
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
208
215
                      int scale);
209
216
 
210
217
 
211
218
inline
212
 
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
 
219
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
213
220
                      int scale)
214
221
{
215
222
  return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
232
239
 
233
240
 
234
241
inline
235
 
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
 
242
int my_decimal_round(uint mask, const my_decimal *from, int scale,
236
243
                     bool truncate, my_decimal *to)
237
244
{
238
245
  return check_result(mask, decimal_round((decimal_t*) from, to, scale,
241
248
 
242
249
 
243
250
inline
244
 
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
 
251
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
245
252
{
246
253
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
247
254
}
248
255
 
249
256
 
250
257
inline
251
 
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
 
258
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
252
259
{
253
260
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
254
261
}
255
262
 
256
263
 
257
 
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
258
 
                      uint32_t fixed_dec, char filler, String *str);
 
264
#ifndef DRIZZLE_CLIENT
 
265
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
 
266
                      uint fixed_dec, char filler, String *str);
 
267
#endif
259
268
 
260
269
inline
261
 
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
 
270
int my_decimal2int(uint mask, const my_decimal *d, bool unsigned_flag,
262
271
                   int64_t *l)
263
272
{
264
273
  my_decimal rounded;
271
280
 
272
281
 
273
282
inline
274
 
int my_decimal2double(uint32_t mask __attribute__((unused)), 
 
283
int my_decimal2double(uint mask __attribute__((unused)), 
275
284
                      const my_decimal *d, double *result)
276
285
{
277
286
  /* No need to call check_result as this will always succeed */
280
289
 
281
290
 
282
291
inline
283
 
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
 
292
int str2my_decimal(uint mask, char *str, my_decimal *d, char **end)
284
293
{
285
294
  return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
286
295
                                   d);
287
296
}
288
297
 
289
298
 
290
 
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
 
299
int str2my_decimal(uint mask, const char *from, uint length,
291
300
                   const CHARSET_INFO * charset, my_decimal *decimal_value);
292
301
 
293
302
#if defined(DRIZZLE_SERVER)
294
303
inline
295
 
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
 
304
int string2my_decimal(uint mask, const String *str, my_decimal *d)
296
305
{
297
306
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
298
307
}
304
313
#endif /*defined(DRIZZLE_SERVER) */
305
314
 
306
315
inline
307
 
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
 
316
int double2my_decimal(uint mask, double val, my_decimal *d)
308
317
{
309
318
  return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
310
319
}
311
320
 
312
321
 
313
322
inline
314
 
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
 
323
int int2my_decimal(uint mask, int64_t i, bool unsigned_flag, my_decimal *d)
315
324
{
316
325
  return check_result(mask, (unsigned_flag ?
317
326
                             uint64_t2decimal((uint64_t)i, d) :
332
341
 
333
342
 
334
343
inline
335
 
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
 
344
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
336
345
                   const my_decimal *b)
337
346
{
338
347
  return check_result_and_overflow(mask,
342
351
 
343
352
 
344
353
inline
345
 
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
 
354
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
346
355
                   const my_decimal *b)
347
356
{
348
357
  return check_result_and_overflow(mask,
352
361
 
353
362
 
354
363
inline
355
 
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
 
364
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
356
365
                   const my_decimal *b)
357
366
{
358
367
  return check_result_and_overflow(mask,
362
371
 
363
372
 
364
373
inline
365
 
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
 
374
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
366
375
                   const my_decimal *b, int div_scale_inc)
367
376
{
368
377
  return check_result_and_overflow(mask,
373
382
 
374
383
 
375
384
inline
376
 
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
 
385
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
377
386
                   const my_decimal *b)
378
387
{
379
388
  return check_result_and_overflow(mask,
400
409
}
401
410
 
402
411
 
403
 
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
 
412
void my_decimal_trim(uint32_t *precision, uint *scale);
404
413
 
405
414
 
406
415
#endif /*my_decimal_h*/