You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3145 lines
86 KiB

17 years ago
17 years ago
17 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. /* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. =======================================================================
  14. NOTE: this library implements SQL standard "exact numeric" type
  15. and is not at all generic, but rather intentinally crippled to
  16. follow the standard :)
  17. =======================================================================
  18. Quoting the standard
  19. (SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
  20. 4.4.2 Characteristics of numbers, page 27:
  21. An exact numeric type has a precision P and a scale S. P is a positive
  22. integer that determines the number of significant digits in a
  23. particular radix R, where R is either 2 or 10. S is a non-negative
  24. integer. Every value of an exact numeric type of scale S is of the
  25. form n*10^{-S}, where n is an integer such that -R^P <= n <= R^P.
  26. [...]
  27. If an assignment of some number would result in a loss of its most
  28. significant digit, an exception condition is raised. If least
  29. significant digits are lost, implementation-defined rounding or
  30. truncating occurs, with no exception condition being raised.
  31. [...]
  32. Whenever an exact or approximate numeric value is assigned to an exact
  33. numeric value site, an approximation of its value that preserves
  34. leading significant digits after rounding or truncating is represented
  35. in the declared type of the target. The value is converted to have the
  36. precision and scale of the target. The choice of whether to truncate
  37. or round is implementation-defined.
  38. [...]
  39. All numeric values between the smallest and the largest value,
  40. inclusive, in a given exact numeric type have an approximation
  41. obtained by rounding or truncation for that type; it is
  42. implementation-defined which other numeric values have such
  43. approximations.
  44. 5.3 <literal>, page 143
  45. <exact numeric literal> ::=
  46. <unsigned integer> [ <period> [ <unsigned integer> ] ]
  47. | <period> <unsigned integer>
  48. 6.1 <data type>, page 165:
  49. 19) The <scale> of an <exact numeric type> shall not be greater than
  50. the <precision> of the <exact numeric type>.
  51. 20) For the <exact numeric type>s DECIMAL and NUMERIC:
  52. a) The maximum value of <precision> is implementation-defined.
  53. <precision> shall not be greater than this value.
  54. b) The maximum value of <scale> is implementation-defined. <scale>
  55. shall not be greater than this maximum value.
  56. 21) NUMERIC specifies the data type exact numeric, with the decimal
  57. precision and scale specified by the <precision> and <scale>.
  58. 22) DECIMAL specifies the data type exact numeric, with the decimal
  59. scale specified by the <scale> and the implementation-defined
  60. decimal precision equal to or greater than the value of the
  61. specified <precision>.
  62. 6.26 <numeric value expression>, page 241:
  63. 1) If the declared type of both operands of a dyadic arithmetic
  64. operator is exact numeric, then the declared type of the result is
  65. an implementation-defined exact numeric type, with precision and
  66. scale determined as follows:
  67. a) Let S1 and S2 be the scale of the first and second operands
  68. respectively.
  69. b) The precision of the result of addition and subtraction is
  70. implementation-defined, and the scale is the maximum of S1 and S2.
  71. c) The precision of the result of multiplication is
  72. implementation-defined, and the scale is S1 + S2.
  73. d) The precision and scale of the result of division are
  74. implementation-defined.
  75. */
  76. #include <my_global.h>
  77. #include <m_ctype.h>
  78. #include <myisampack.h>
  79. #include <my_sys.h> /* for my_alloca */
  80. #include <m_string.h>
  81. #include <decimal.h>
  82. /*
  83. Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
  84. So one variable of type decimal_digit_t is limited:
  85. 0 < decimal_digit <= DIG_MAX < DIG_BASE
  86. in the struct st_decimal_t:
  87. intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
  88. before the point
  89. frac - number of decimal digits after the point
  90. buf is an array of decimal_digit_t's
  91. len is the length of buf (length of allocated space) in decimal_digit_t's,
  92. not in bytes
  93. */
  94. typedef decimal_digit_t dec1;
  95. typedef longlong dec2;
  96. #define DIG_PER_DEC1 9
  97. #define DIG_MASK 100000000
  98. #define DIG_BASE 1000000000
  99. #define DIG_MAX (DIG_BASE-1)
  100. #define DIG_BASE2 ((dec2)DIG_BASE * (dec2)DIG_BASE)
  101. #define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
  102. static const dec1 powers10[DIG_PER_DEC1+1]={
  103. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  104. static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
  105. static const dec1 frac_max[DIG_PER_DEC1-1]={
  106. 900000000, 990000000, 999000000,
  107. 999900000, 999990000, 999999000,
  108. 999999900, 999999990 };
  109. #ifdef HAVE_purify
  110. #define sanity(d) DBUG_ASSERT((d)->len > 0)
  111. #else
  112. #define sanity(d) DBUG_ASSERT((d)->len >0 && ((d)->buf[0] | \
  113. (d)->buf[(d)->len-1] | 1))
  114. #endif
  115. #define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \
  116. do \
  117. { \
  118. if (unlikely(intg1+frac1 > (len))) \
  119. { \
  120. if (unlikely(intg1 > (len))) \
  121. { \
  122. intg1=(len); \
  123. frac1=0; \
  124. error=E_DEC_OVERFLOW; \
  125. } \
  126. else \
  127. { \
  128. frac1=(len)-intg1; \
  129. error=E_DEC_TRUNCATED; \
  130. } \
  131. } \
  132. else \
  133. error=E_DEC_OK; \
  134. } while(0)
  135. #define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \
  136. do \
  137. { \
  138. dec1 a=(from1)+(from2)+(carry); \
  139. DBUG_ASSERT((carry) <= 1); \
  140. if (((carry)= a >= DIG_BASE)) /* no division here! */ \
  141. a-=DIG_BASE; \
  142. (to)=a; \
  143. } while(0)
  144. #define ADD2(to, from1, from2, carry) \
  145. do \
  146. { \
  147. dec2 a=((dec2)(from1))+(from2)+(carry); \
  148. if (((carry)= a >= DIG_BASE)) \
  149. a-=DIG_BASE; \
  150. if (unlikely(a >= DIG_BASE)) \
  151. { \
  152. a-=DIG_BASE; \
  153. carry++; \
  154. } \
  155. (to)=(dec1) a; \
  156. } while(0)
  157. #define SUB(to, from1, from2, carry) /* to=from1-from2 */ \
  158. do \
  159. { \
  160. dec1 a=(from1)-(from2)-(carry); \
  161. if (((carry)= a < 0)) \
  162. a+=DIG_BASE; \
  163. (to)=a; \
  164. } while(0)
  165. #define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \
  166. do \
  167. { \
  168. dec1 a=(from1)-(from2)-(carry); \
  169. if (((carry)= a < 0)) \
  170. a+=DIG_BASE; \
  171. if (unlikely(a < 0)) \
  172. { \
  173. a+=DIG_BASE; \
  174. carry++; \
  175. } \
  176. (to)=a; \
  177. } while(0)
  178. /*
  179. Get maximum value for given precision and scale
  180. SYNOPSIS
  181. max_decimal()
  182. precision/scale - see decimal_bin_size() below
  183. to - decimal where where the result will be stored
  184. to->buf and to->len must be set.
  185. */
  186. void max_decimal(int precision, int frac, decimal_t *to)
  187. {
  188. int intpart;
  189. dec1 *buf= to->buf;
  190. DBUG_ASSERT(precision && precision >= frac);
  191. to->sign= 0;
  192. if ((intpart= to->intg= (precision - frac)))
  193. {
  194. int firstdigits= intpart % DIG_PER_DEC1;
  195. if (firstdigits)
  196. *buf++= powers10[firstdigits] - 1; /* get 9 99 999 ... */
  197. for(intpart/= DIG_PER_DEC1; intpart; intpart--)
  198. *buf++= DIG_MAX;
  199. }
  200. if ((to->frac= frac))
  201. {
  202. int lastdigits= frac % DIG_PER_DEC1;
  203. for(frac/= DIG_PER_DEC1; frac; frac--)
  204. *buf++= DIG_MAX;
  205. if (lastdigits)
  206. *buf= frac_max[lastdigits - 1];
  207. }
  208. }
  209. static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result)
  210. {
  211. int intg= from->intg, i;
  212. dec1 *buf0= from->buf;
  213. i= ((intg - 1) % DIG_PER_DEC1) + 1;
  214. while (intg > 0 && *buf0 == 0)
  215. {
  216. intg-= i;
  217. i= DIG_PER_DEC1;
  218. buf0++;
  219. }
  220. if (intg > 0)
  221. {
  222. for (i= (intg - 1) % DIG_PER_DEC1; *buf0 < powers10[i--]; intg--) ;
  223. DBUG_ASSERT(intg > 0);
  224. }
  225. else
  226. intg=0;
  227. *intg_result= intg;
  228. return buf0;
  229. }
  230. /*
  231. Count actual length of fraction part (without ending zeroes)
  232. SYNOPSIS
  233. decimal_actual_fraction()
  234. from number for processing
  235. */
  236. int decimal_actual_fraction(decimal_t *from)
  237. {
  238. int frac= from->frac, i;
  239. dec1 *buf0= from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
  240. if (frac == 0)
  241. return 0;
  242. i= ((frac - 1) % DIG_PER_DEC1 + 1);
  243. while (frac > 0 && *buf0 == 0)
  244. {
  245. frac-= i;
  246. i= DIG_PER_DEC1;
  247. buf0--;
  248. }
  249. if (frac > 0)
  250. {
  251. for (i= DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1);
  252. *buf0 % powers10[i++] == 0;
  253. frac--) ;
  254. }
  255. return frac;
  256. }
  257. /*
  258. Convert decimal to its printable string representation
  259. SYNOPSIS
  260. decimal2string()
  261. from - value to convert
  262. to - points to buffer where string representation
  263. should be stored
  264. *to_len - in: size of to buffer (incl. terminating '\0')
  265. out: length of the actually written string (excl. '\0')
  266. fixed_precision - 0 if representation can be variable length and
  267. fixed_decimals will not be checked in this case.
  268. Put number as with fixed point position with this
  269. number of digits (sign counted and decimal point is
  270. counted)
  271. fixed_decimals - number digits after point.
  272. filler - character to fill gaps in case of fixed_precision > 0
  273. RETURN VALUE
  274. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  275. */
  276. int decimal2string(const decimal_t *from, char *to, int *to_len,
  277. int fixed_precision, int fixed_decimals,
  278. char filler)
  279. {
  280. /* {intg_len, frac_len} output widths; {intg, frac} places in input */
  281. int len, intg, frac= from->frac, i, intg_len, frac_len, fill;
  282. /* number digits before decimal point */
  283. int fixed_intg= (fixed_precision ?
  284. (fixed_precision - fixed_decimals) : 0);
  285. int error=E_DEC_OK;
  286. char *s=to;
  287. dec1 *buf, *buf0=from->buf, tmp;
  288. DBUG_ASSERT(*to_len >= 2+from->sign);
  289. /* removing leading zeroes */
  290. buf0= remove_leading_zeroes(from, &intg);
  291. if (unlikely(intg+frac==0))
  292. {
  293. intg=1;
  294. tmp=0;
  295. buf0=&tmp;
  296. }
  297. if (!(intg_len= fixed_precision ? fixed_intg : intg))
  298. intg_len= 1;
  299. frac_len= fixed_precision ? fixed_decimals : frac;
  300. len= from->sign + intg_len + test(frac) + frac_len;
  301. if (fixed_precision)
  302. {
  303. if (frac > fixed_decimals)
  304. {
  305. error= E_DEC_TRUNCATED;
  306. frac= fixed_decimals;
  307. }
  308. if (intg > fixed_intg)
  309. {
  310. error= E_DEC_OVERFLOW;
  311. intg= fixed_intg;
  312. }
  313. }
  314. else if (unlikely(len > --*to_len)) /* reserve one byte for \0 */
  315. {
  316. int j= len-*to_len;
  317. error= (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
  318. if (frac && j >= frac + 1) j--;
  319. if (j > frac)
  320. {
  321. intg-= j-frac;
  322. frac= 0;
  323. }
  324. else
  325. frac-=j;
  326. len= from->sign + intg_len + test(frac) + frac_len;
  327. }
  328. *to_len=len;
  329. s[len]=0;
  330. if (from->sign)
  331. *s++='-';
  332. if (frac)
  333. {
  334. char *s1= s + intg_len;
  335. fill= frac_len - frac;
  336. buf=buf0+ROUND_UP(intg);
  337. *s1++='.';
  338. for (; frac>0; frac-=DIG_PER_DEC1)
  339. {
  340. dec1 x=*buf++;
  341. for (i=min(frac, DIG_PER_DEC1); i; i--)
  342. {
  343. dec1 y=x/DIG_MASK;
  344. *s1++='0'+(uchar)y;
  345. x-=y*DIG_MASK;
  346. x*=10;
  347. }
  348. }
  349. for(; fill; fill--)
  350. *s1++=filler;
  351. }
  352. fill= intg_len - intg;
  353. if (intg == 0)
  354. fill--; /* symbol 0 before digital point */
  355. for(; fill; fill--)
  356. *s++=filler;
  357. if (intg)
  358. {
  359. s+=intg;
  360. for (buf=buf0+ROUND_UP(intg); intg>0; intg-=DIG_PER_DEC1)
  361. {
  362. dec1 x=*--buf;
  363. for (i=min(intg, DIG_PER_DEC1); i; i--)
  364. {
  365. dec1 y=x/10;
  366. *--s='0'+(uchar)(x-y*10);
  367. x=y;
  368. }
  369. }
  370. }
  371. else
  372. *s= '0';
  373. return error;
  374. }
  375. /*
  376. Return bounds of decimal digits in the number
  377. SYNOPSIS
  378. digits_bounds()
  379. from - decimal number for processing
  380. start_result - index (from 0 ) of first decimal digits will
  381. be written by this address
  382. end_result - index of position just after last decimal digit
  383. be written by this address
  384. */
  385. static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
  386. {
  387. int start, stop, i;
  388. dec1 *buf_beg= from->buf;
  389. dec1 *end= from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
  390. dec1 *buf_end= end - 1;
  391. /* find non-zero digit from number begining */
  392. while (buf_beg < end && *buf_beg == 0)
  393. buf_beg++;
  394. if (buf_beg >= end)
  395. {
  396. /* it is zero */
  397. *start_result= *end_result= 0;
  398. return;
  399. }
  400. /* find non-zero decimal digit from number begining */
  401. if (buf_beg == from->buf && from->intg)
  402. {
  403. start= DIG_PER_DEC1 - (i= ((from->intg-1) % DIG_PER_DEC1 + 1));
  404. i--;
  405. }
  406. else
  407. {
  408. i= DIG_PER_DEC1 - 1;
  409. start= (int) ((buf_beg - from->buf) * DIG_PER_DEC1);
  410. }
  411. if (buf_beg < end)
  412. for (; *buf_beg < powers10[i--]; start++) ;
  413. *start_result= start; /* index of first decimal digit (from 0) */
  414. /* find non-zero digit at the end */
  415. while (buf_end > buf_beg && *buf_end == 0)
  416. buf_end--;
  417. /* find non-zero decimal digit from the end */
  418. if (buf_end == end - 1 && from->frac)
  419. {
  420. stop= (int) (((buf_end - from->buf) * DIG_PER_DEC1 +
  421. (i= ((from->frac - 1) % DIG_PER_DEC1 + 1))));
  422. i= DIG_PER_DEC1 - i + 1;
  423. }
  424. else
  425. {
  426. stop= (int) ((buf_end - from->buf + 1) * DIG_PER_DEC1);
  427. i= 1;
  428. }
  429. for (; *buf_end % powers10[i++] == 0; stop--) ;
  430. *end_result= stop; /* index of position after last decimal digit (from 0) */
  431. }
  432. /*
  433. Left shift for alignment of data in buffer
  434. SYNOPSIS
  435. do_mini_left_shift()
  436. dec pointer to decimal number which have to be shifted
  437. shift number of decimal digits on which it should be shifted
  438. beg/end bounds of decimal digits (see digits_bounds())
  439. NOTE
  440. Result fitting in the buffer should be garanted.
  441. 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
  442. */
  443. void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
  444. {
  445. dec1 *from= dec->buf + ROUND_UP(beg + 1) - 1;
  446. dec1 *end= dec->buf + ROUND_UP(last) - 1;
  447. int c_shift= DIG_PER_DEC1 - shift;
  448. DBUG_ASSERT(from >= dec->buf);
  449. DBUG_ASSERT(end < dec->buf + dec->len);
  450. if (beg % DIG_PER_DEC1 < shift)
  451. *(from - 1)= (*from) / powers10[c_shift];
  452. for(; from < end; from++)
  453. *from= ((*from % powers10[c_shift]) * powers10[shift] +
  454. (*(from + 1)) / powers10[c_shift]);
  455. *from= (*from % powers10[c_shift]) * powers10[shift];
  456. }
  457. /*
  458. Right shift for alignment of data in buffer
  459. SYNOPSIS
  460. do_mini_left_shift()
  461. dec pointer to decimal number which have to be shifted
  462. shift number of decimal digits on which it should be shifted
  463. beg/end bounds of decimal digits (see digits_bounds())
  464. NOTE
  465. Result fitting in the buffer should be garanted.
  466. 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
  467. */
  468. void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
  469. {
  470. dec1 *from= dec->buf + ROUND_UP(last) - 1;
  471. dec1 *end= dec->buf + ROUND_UP(beg + 1) - 1;
  472. int c_shift= DIG_PER_DEC1 - shift;
  473. DBUG_ASSERT(from < dec->buf + dec->len);
  474. DBUG_ASSERT(end >= dec->buf);
  475. if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
  476. *(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
  477. for(; from > end; from--)
  478. *from= (*from / powers10[shift] +
  479. (*(from - 1) % powers10[shift]) * powers10[c_shift]);
  480. *from= *from / powers10[shift];
  481. }
  482. /*
  483. Shift of decimal digits in given number (with rounding if it need)
  484. SYNOPSIS
  485. decimal_shift()
  486. dec number to be shifted
  487. shift number of decimal positions
  488. shift > 0 means shift to left shift
  489. shift < 0 meand right shift
  490. NOTE
  491. In fact it is multipling on 10^shift.
  492. RETURN
  493. E_DEC_OK OK
  494. E_DEC_OVERFLOW operation lead to overflow, number is untoched
  495. E_DEC_TRUNCATED number was rounded to fit into buffer
  496. */
  497. int decimal_shift(decimal_t *dec, int shift)
  498. {
  499. /* index of first non zero digit (all indexes from 0) */
  500. int beg;
  501. /* index of position after last decimal digit */
  502. int end;
  503. /* index of digit position just after point */
  504. int point= ROUND_UP(dec->intg) * DIG_PER_DEC1;
  505. /* new point position */
  506. int new_point= point + shift;
  507. /* number of digits in result */
  508. int digits_int, digits_frac;
  509. /* length of result and new fraction in big digits*/
  510. int new_len, new_frac_len;
  511. /* return code */
  512. int err= E_DEC_OK;
  513. int new_front;
  514. if (shift == 0)
  515. return E_DEC_OK;
  516. digits_bounds(dec, &beg, &end);
  517. if (beg == end)
  518. {
  519. decimal_make_zero(dec);
  520. return E_DEC_OK;
  521. }
  522. digits_int= new_point - beg;
  523. set_if_bigger(digits_int, 0);
  524. digits_frac= end - new_point;
  525. set_if_bigger(digits_frac, 0);
  526. if ((new_len= ROUND_UP(digits_int) + (new_frac_len= ROUND_UP(digits_frac))) >
  527. dec->len)
  528. {
  529. int lack= new_len - dec->len;
  530. int diff;
  531. if (new_frac_len < lack)
  532. return E_DEC_OVERFLOW; /* lack more then we have in fraction */
  533. /* cat off fraction part to allow new number to fit in our buffer */
  534. err= E_DEC_TRUNCATED;
  535. new_frac_len-= lack;
  536. diff= digits_frac - (new_frac_len * DIG_PER_DEC1);
  537. /* Make rounding method as parameter? */
  538. decimal_round(dec, dec, end - point - diff, HALF_UP);
  539. end-= diff;
  540. digits_frac= new_frac_len * DIG_PER_DEC1;
  541. if (end <= beg)
  542. {
  543. /*
  544. we lost all digits (they will be shifted out of buffer), so we can
  545. just return 0
  546. */
  547. decimal_make_zero(dec);
  548. return E_DEC_TRUNCATED;
  549. }
  550. }
  551. if (shift % DIG_PER_DEC1)
  552. {
  553. int l_mini_shift, r_mini_shift, mini_shift;
  554. int do_left;
  555. /*
  556. Calculate left/right shift to align decimal digits inside our bug
  557. digits correctly
  558. */
  559. if (shift > 0)
  560. {
  561. l_mini_shift= shift % DIG_PER_DEC1;
  562. r_mini_shift= DIG_PER_DEC1 - l_mini_shift;
  563. /*
  564. It is left shift so prefer left shift, but if we have not place from
  565. left, we have to have it from right, because we checked length of
  566. result
  567. */
  568. do_left= l_mini_shift <= beg;
  569. DBUG_ASSERT(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
  570. }
  571. else
  572. {
  573. r_mini_shift= (-shift) % DIG_PER_DEC1;
  574. l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
  575. /* see comment above */
  576. do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
  577. DBUG_ASSERT(!do_left || l_mini_shift <= beg);
  578. }
  579. if (do_left)
  580. {
  581. do_mini_left_shift(dec, l_mini_shift, beg, end);
  582. mini_shift=- l_mini_shift;
  583. }
  584. else
  585. {
  586. do_mini_right_shift(dec, r_mini_shift, beg, end);
  587. mini_shift= r_mini_shift;
  588. }
  589. new_point+= mini_shift;
  590. /*
  591. If number is shifted and correctly aligned in buffer we can
  592. finish
  593. */
  594. if (!(shift+= mini_shift) && (new_point - digits_int) < DIG_PER_DEC1)
  595. {
  596. dec->intg= digits_int;
  597. dec->frac= digits_frac;
  598. return err; /* already shifted as it should be */
  599. }
  600. beg+= mini_shift;
  601. end+= mini_shift;
  602. }
  603. /* if new 'decimal front' is in first digit, we do not need move digits */
  604. if ((new_front= (new_point - digits_int)) >= DIG_PER_DEC1 ||
  605. new_front < 0)
  606. {
  607. /* need to move digits */
  608. int d_shift;
  609. dec1 *to, *barier;
  610. if (new_front > 0)
  611. {
  612. /* move left */
  613. d_shift= new_front / DIG_PER_DEC1;
  614. to= dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
  615. barier= dec->buf + (ROUND_UP(end) - 1 - d_shift);
  616. DBUG_ASSERT(to >= dec->buf);
  617. DBUG_ASSERT(barier + d_shift < dec->buf + dec->len);
  618. for(; to <= barier; to++)
  619. *to= *(to + d_shift);
  620. for(barier+= d_shift; to <= barier; to++)
  621. *to= 0;
  622. d_shift= -d_shift;
  623. }
  624. else
  625. {
  626. /* move right */
  627. d_shift= (1 - new_front) / DIG_PER_DEC1;
  628. to= dec->buf + ROUND_UP(end) - 1 + d_shift;
  629. barier= dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
  630. DBUG_ASSERT(to < dec->buf + dec->len);
  631. DBUG_ASSERT(barier - d_shift >= dec->buf);
  632. for(; to >= barier; to--)
  633. *to= *(to - d_shift);
  634. for(barier-= d_shift; to >= barier; to--)
  635. *to= 0;
  636. }
  637. d_shift*= DIG_PER_DEC1;
  638. beg+= d_shift;
  639. end+= d_shift;
  640. new_point+= d_shift;
  641. }
  642. /*
  643. If there are gaps then fill ren with 0.
  644. Only one of following 'for' loops will work becouse beg <= end
  645. */
  646. beg= ROUND_UP(beg + 1) - 1;
  647. end= ROUND_UP(end) - 1;
  648. DBUG_ASSERT(new_point >= 0);
  649. /* We don't want negative new_point below */
  650. if (new_point != 0)
  651. new_point= ROUND_UP(new_point) - 1;
  652. if (new_point > end)
  653. {
  654. do
  655. {
  656. dec->buf[new_point]=0;
  657. } while (--new_point > end);
  658. }
  659. else
  660. {
  661. for (; new_point < beg; new_point++)
  662. dec->buf[new_point]= 0;
  663. }
  664. dec->intg= digits_int;
  665. dec->frac= digits_frac;
  666. return err;
  667. }
  668. /*
  669. Convert string to decimal
  670. SYNOPSIS
  671. internal_str2decl()
  672. from - value to convert. Doesn't have to be \0 terminated!
  673. to - decimal where where the result will be stored
  674. to->buf and to->len must be set.
  675. end - Pointer to pointer to end of string. Will on return be
  676. set to the char after the last used character
  677. fixed - use to->intg, to->frac as limits for input number
  678. NOTE
  679. to->intg and to->frac can be modified even when fixed=1
  680. (but only decreased, in this case)
  681. RETURN VALUE
  682. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_BAD_NUM/E_DEC_OOM
  683. In case of E_DEC_FATAL_ERROR *to is set to decimal zero
  684. (to make error handling easier)
  685. */
  686. int
  687. internal_str2dec(const char *from, decimal_t *to, char **end, my_bool fixed)
  688. {
  689. const char *s= from, *s1, *endp, *end_of_string= *end;
  690. int i, intg, frac, error, intg1, frac1;
  691. dec1 x,*buf;
  692. sanity(to);
  693. error= E_DEC_BAD_NUM; /* In case of bad number */
  694. while (s < end_of_string && my_isspace(&my_charset_latin1, *s))
  695. s++;
  696. if (s == end_of_string)
  697. goto fatal_error;
  698. if ((to->sign= (*s == '-')))
  699. s++;
  700. else if (*s == '+')
  701. s++;
  702. s1=s;
  703. while (s < end_of_string && my_isdigit(&my_charset_latin1, *s))
  704. s++;
  705. intg= (int) (s-s1);
  706. if (s < end_of_string && *s=='.')
  707. {
  708. endp= s+1;
  709. while (endp < end_of_string && my_isdigit(&my_charset_latin1, *endp))
  710. endp++;
  711. frac= (int) (endp - s - 1);
  712. }
  713. else
  714. {
  715. frac= 0;
  716. endp= s;
  717. }
  718. *end= (char*) endp;
  719. if (frac+intg == 0)
  720. goto fatal_error;
  721. error= 0;
  722. if (fixed)
  723. {
  724. if (frac > to->frac)
  725. {
  726. error=E_DEC_TRUNCATED;
  727. frac=to->frac;
  728. }
  729. if (intg > to->intg)
  730. {
  731. error=E_DEC_OVERFLOW;
  732. intg=to->intg;
  733. }
  734. intg1=ROUND_UP(intg);
  735. frac1=ROUND_UP(frac);
  736. if (intg1+frac1 > to->len)
  737. {
  738. error= E_DEC_OOM;
  739. goto fatal_error;
  740. }
  741. }
  742. else
  743. {
  744. intg1=ROUND_UP(intg);
  745. frac1=ROUND_UP(frac);
  746. FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
  747. if (unlikely(error))
  748. {
  749. frac=frac1*DIG_PER_DEC1;
  750. if (error == E_DEC_OVERFLOW)
  751. intg=intg1*DIG_PER_DEC1;
  752. }
  753. }
  754. /* Error is guranteed to be set here */
  755. to->intg=intg;
  756. to->frac=frac;
  757. buf=to->buf+intg1;
  758. s1=s;
  759. for (x=0, i=0; intg; intg--)
  760. {
  761. x+= (*--s - '0')*powers10[i];
  762. if (unlikely(++i == DIG_PER_DEC1))
  763. {
  764. *--buf=x;
  765. x=0;
  766. i=0;
  767. }
  768. }
  769. if (i)
  770. *--buf=x;
  771. buf=to->buf+intg1;
  772. for (x=0, i=0; frac; frac--)
  773. {
  774. x= (*++s1 - '0') + x*10;
  775. if (unlikely(++i == DIG_PER_DEC1))
  776. {
  777. *buf++=x;
  778. x=0;
  779. i=0;
  780. }
  781. }
  782. if (i)
  783. *buf=x*powers10[DIG_PER_DEC1-i];
  784. /* Handle exponent */
  785. if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
  786. {
  787. int str_error;
  788. longlong exponent= my_strtoll10(endp+1, (char**) &end_of_string,
  789. &str_error);
  790. if (end_of_string != endp +1) /* If at least one digit */
  791. {
  792. *end= (char*) end_of_string;
  793. if (str_error > 0)
  794. {
  795. error= E_DEC_BAD_NUM;
  796. goto fatal_error;
  797. }
  798. if (exponent > INT_MAX/2 || (str_error == 0 && exponent < 0))
  799. {
  800. error= E_DEC_OVERFLOW;
  801. goto fatal_error;
  802. }
  803. if (exponent < INT_MIN/2 && error != E_DEC_OVERFLOW)
  804. {
  805. error= E_DEC_TRUNCATED;
  806. goto fatal_error;
  807. }
  808. if (error != E_DEC_OVERFLOW)
  809. error= decimal_shift(to, (int) exponent);
  810. }
  811. }
  812. return error;
  813. fatal_error:
  814. decimal_make_zero(to);
  815. return error;
  816. }
  817. /*
  818. Convert decimal to double
  819. SYNOPSIS
  820. decimal2double()
  821. from - value to convert
  822. to - result will be stored there
  823. RETURN VALUE
  824. E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
  825. */
  826. int decimal2double(const decimal_t *from, double *to)
  827. {
  828. char strbuf[FLOATING_POINT_BUFFER], *end;
  829. int len= sizeof(strbuf);
  830. int rc, error;
  831. rc = decimal2string(from, strbuf, &len, 0, 0, 0);
  832. end= strbuf + len;
  833. DBUG_PRINT("info", ("interm.: %s", strbuf));
  834. *to= my_strtod(strbuf, &end, &error);
  835. DBUG_PRINT("info", ("result: %f", *to));
  836. return (rc != E_DEC_OK) ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
  837. }
  838. /*
  839. Convert double to decimal
  840. SYNOPSIS
  841. double2decimal()
  842. from - value to convert
  843. to - result will be stored there
  844. RETURN VALUE
  845. E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
  846. */
  847. int double2decimal(double from, decimal_t *to)
  848. {
  849. char buff[FLOATING_POINT_BUFFER], *end;
  850. int res;
  851. DBUG_ENTER("double2decimal");
  852. end= buff + my_gcvt(from, MY_GCVT_ARG_DOUBLE, sizeof(buff) - 1, buff, NULL);
  853. res= string2decimal(buff, to, &end);
  854. DBUG_PRINT("exit", ("res: %d", res));
  855. DBUG_RETURN(res);
  856. }
  857. static int ull2dec(ulonglong from, decimal_t *to)
  858. {
  859. int intg1, error=E_DEC_OK;
  860. ulonglong x=from;
  861. dec1 *buf;
  862. sanity(to);
  863. for (intg1=1; from >= DIG_BASE; intg1++, from/=DIG_BASE) ;
  864. if (unlikely(intg1 > to->len))
  865. {
  866. intg1=to->len;
  867. error=E_DEC_OVERFLOW;
  868. }
  869. to->frac=0;
  870. to->intg=intg1*DIG_PER_DEC1;
  871. for (buf=to->buf+intg1; intg1; intg1--)
  872. {
  873. ulonglong y=x/DIG_BASE;
  874. *--buf=(dec1)(x-y*DIG_BASE);
  875. x=y;
  876. }
  877. return error;
  878. }
  879. int ulonglong2decimal(ulonglong from, decimal_t *to)
  880. {
  881. to->sign=0;
  882. return ull2dec(from, to);
  883. }
  884. int longlong2decimal(longlong from, decimal_t *to)
  885. {
  886. if ((to->sign= from < 0))
  887. return ull2dec(-from, to);
  888. return ull2dec(from, to);
  889. }
  890. int decimal2ulonglong(decimal_t *from, ulonglong *to)
  891. {
  892. dec1 *buf=from->buf;
  893. ulonglong x=0;
  894. int intg, frac;
  895. if (from->sign)
  896. {
  897. *to=ULL(0);
  898. return E_DEC_OVERFLOW;
  899. }
  900. for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
  901. {
  902. ulonglong y=x;
  903. x=x*DIG_BASE + *buf++;
  904. if (unlikely(y > ((ulonglong) ULONGLONG_MAX/DIG_BASE) || x < y))
  905. {
  906. *to=ULONGLONG_MAX;
  907. return E_DEC_OVERFLOW;
  908. }
  909. }
  910. *to=x;
  911. for (frac=from->frac; unlikely(frac > 0); frac-=DIG_PER_DEC1)
  912. if (*buf++)
  913. return E_DEC_TRUNCATED;
  914. return E_DEC_OK;
  915. }
  916. int decimal2longlong(decimal_t *from, longlong *to)
  917. {
  918. dec1 *buf=from->buf;
  919. longlong x=0;
  920. int intg, frac;
  921. for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
  922. {
  923. longlong y=x;
  924. /*
  925. Attention: trick!
  926. we're calculating -|from| instead of |from| here
  927. because |LONGLONG_MIN| > LONGLONG_MAX
  928. so we can convert -9223372036854775808 correctly
  929. */
  930. x=x*DIG_BASE - *buf++;
  931. if (unlikely(y < (LONGLONG_MIN/DIG_BASE) || x > y))
  932. {
  933. /*
  934. the decimal is bigger than any possible integer
  935. return border integer depending on the sign
  936. */
  937. *to= from->sign ? LONGLONG_MIN : LONGLONG_MAX;
  938. return E_DEC_OVERFLOW;
  939. }
  940. }
  941. /* boundary case: 9223372036854775808 */
  942. if (unlikely(from->sign==0 && x == LONGLONG_MIN))
  943. {
  944. *to= LONGLONG_MAX;
  945. return E_DEC_OVERFLOW;
  946. }
  947. *to=from->sign ? x : -x;
  948. for (frac=from->frac; unlikely(frac > 0); frac-=DIG_PER_DEC1)
  949. if (*buf++)
  950. return E_DEC_TRUNCATED;
  951. return E_DEC_OK;
  952. }
  953. /*
  954. Convert decimal to its binary fixed-length representation
  955. two representations of the same length can be compared with memcmp
  956. with the correct -1/0/+1 result
  957. SYNOPSIS
  958. decimal2bin()
  959. from - value to convert
  960. to - points to buffer where string representation should be stored
  961. precision/scale - see decimal_bin_size() below
  962. NOTE
  963. the buffer is assumed to be of the size decimal_bin_size(precision, scale)
  964. RETURN VALUE
  965. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  966. DESCRIPTION
  967. for storage decimal numbers are converted to the "binary" format.
  968. This format has the following properties:
  969. 1. length of the binary representation depends on the {precision, scale}
  970. as provided by the caller and NOT on the intg/frac of the decimal to
  971. convert.
  972. 2. binary representations of the same {precision, scale} can be compared
  973. with memcmp - with the same result as decimal_cmp() of the original
  974. decimals (not taking into account possible precision loss during
  975. conversion).
  976. This binary format is as follows:
  977. 1. First the number is converted to have a requested precision and scale.
  978. 2. Every full DIG_PER_DEC1 digits of intg part are stored in 4 bytes
  979. as is
  980. 3. The first intg % DIG_PER_DEC1 digits are stored in the reduced
  981. number of bytes (enough bytes to store this number of digits -
  982. see dig2bytes)
  983. 4. same for frac - full decimal_digit_t's are stored as is,
  984. the last frac % DIG_PER_DEC1 digits - in the reduced number of bytes.
  985. 5. If the number is negative - every byte is inversed.
  986. 5. The very first bit of the resulting byte array is inverted (because
  987. memcmp compares unsigned bytes, see property 2 above)
  988. Example:
  989. 1234567890.1234
  990. internally is represented as 3 decimal_digit_t's
  991. 1 234567890 123400000
  992. (assuming we want a binary representation with precision=14, scale=4)
  993. in hex it's
  994. 00-00-00-01 0D-FB-38-D2 07-5A-EF-40
  995. now, middle decimal_digit_t is full - it stores 9 decimal digits. It goes
  996. into binary representation as is:
  997. ........... 0D-FB-38-D2 ............
  998. First decimal_digit_t has only one decimal digit. We can store one digit in
  999. one byte, no need to waste four:
  1000. 01 0D-FB-38-D2 ............
  1001. now, last digit. It's 123400000. We can store 1234 in two bytes:
  1002. 01 0D-FB-38-D2 04-D2
  1003. So, we've packed 12 bytes number in 7 bytes.
  1004. And now we invert the highest bit to get the final result:
  1005. 81 0D FB 38 D2 04 D2
  1006. And for -1234567890.1234 it would be
  1007. 7E F2 04 37 2D FB 2D
  1008. */
  1009. int decimal2bin(decimal_t *from, uchar *to, int precision, int frac)
  1010. {
  1011. dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
  1012. int error=E_DEC_OK, intg=precision-frac,
  1013. isize1, intg1, intg1x, from_intg,
  1014. intg0=intg/DIG_PER_DEC1,
  1015. frac0=frac/DIG_PER_DEC1,
  1016. intg0x=intg-intg0*DIG_PER_DEC1,
  1017. frac0x=frac-frac0*DIG_PER_DEC1,
  1018. frac1=from->frac/DIG_PER_DEC1,
  1019. frac1x=from->frac-frac1*DIG_PER_DEC1,
  1020. isize0=intg0*sizeof(dec1)+dig2bytes[intg0x],
  1021. fsize0=frac0*sizeof(dec1)+dig2bytes[frac0x],
  1022. fsize1=frac1*sizeof(dec1)+dig2bytes[frac1x];
  1023. const int orig_isize0= isize0;
  1024. const int orig_fsize0= fsize0;
  1025. uchar *orig_to= to;
  1026. buf1= remove_leading_zeroes(from, &from_intg);
  1027. if (unlikely(from_intg+fsize1==0))
  1028. {
  1029. mask=0; /* just in case */
  1030. intg=1;
  1031. buf1=&mask;
  1032. }
  1033. intg1=from_intg/DIG_PER_DEC1;
  1034. intg1x=from_intg-intg1*DIG_PER_DEC1;
  1035. isize1=intg1*sizeof(dec1)+dig2bytes[intg1x];
  1036. if (intg < from_intg)
  1037. {
  1038. buf1+=intg1-intg0+(intg1x>0)-(intg0x>0);
  1039. intg1=intg0; intg1x=intg0x;
  1040. error=E_DEC_OVERFLOW;
  1041. }
  1042. else if (isize0 > isize1)
  1043. {
  1044. while (isize0-- > isize1)
  1045. *to++= (char)mask;
  1046. }
  1047. if (fsize0 < fsize1)
  1048. {
  1049. frac1=frac0; frac1x=frac0x;
  1050. error=E_DEC_TRUNCATED;
  1051. }
  1052. else if (fsize0 > fsize1 && frac1x)
  1053. {
  1054. if (frac0 == frac1)
  1055. {
  1056. frac1x=frac0x;
  1057. fsize0= fsize1;
  1058. }
  1059. else
  1060. {
  1061. frac1++;
  1062. frac1x=0;
  1063. }
  1064. }
  1065. /* intg1x part */
  1066. if (intg1x)
  1067. {
  1068. int i=dig2bytes[intg1x];
  1069. dec1 x=(*buf1++ % powers10[intg1x]) ^ mask;
  1070. switch (i)
  1071. {
  1072. case 1: mi_int1store(to, x); break;
  1073. case 2: mi_int2store(to, x); break;
  1074. case 3: mi_int3store(to, x); break;
  1075. case 4: mi_int4store(to, x); break;
  1076. default: DBUG_ASSERT(0);
  1077. }
  1078. to+=i;
  1079. }
  1080. /* intg1+frac1 part */
  1081. for (stop1=buf1+intg1+frac1; buf1 < stop1; to+=sizeof(dec1))
  1082. {
  1083. dec1 x=*buf1++ ^ mask;
  1084. DBUG_ASSERT(sizeof(dec1) == 4);
  1085. mi_int4store(to, x);
  1086. }
  1087. /* frac1x part */
  1088. if (frac1x)
  1089. {
  1090. dec1 x;
  1091. int i=dig2bytes[frac1x],
  1092. lim=(frac1 < frac0 ? DIG_PER_DEC1 : frac0x);
  1093. while (frac1x < lim && dig2bytes[frac1x] == i)
  1094. frac1x++;
  1095. x=(*buf1 / powers10[DIG_PER_DEC1 - frac1x]) ^ mask;
  1096. switch (i)
  1097. {
  1098. case 1: mi_int1store(to, x); break;
  1099. case 2: mi_int2store(to, x); break;
  1100. case 3: mi_int3store(to, x); break;
  1101. case 4: mi_int4store(to, x); break;
  1102. default: DBUG_ASSERT(0);
  1103. }
  1104. to+=i;
  1105. }
  1106. if (fsize0 > fsize1)
  1107. {
  1108. uchar *to_end= orig_to + orig_fsize0 + orig_isize0;
  1109. while (fsize0-- > fsize1 && to < to_end)
  1110. *to++= (uchar)mask;
  1111. }
  1112. orig_to[0]^= 0x80;
  1113. /* Check that we have written the whole decimal and nothing more */
  1114. DBUG_ASSERT(to == orig_to + orig_fsize0 + orig_isize0);
  1115. return error;
  1116. }
  1117. /*
  1118. Restores decimal from its binary fixed-length representation
  1119. SYNOPSIS
  1120. bin2decimal()
  1121. from - value to convert
  1122. to - result
  1123. precision/scale - see decimal_bin_size() below
  1124. NOTE
  1125. see decimal2bin()
  1126. the buffer is assumed to be of the size decimal_bin_size(precision, scale)
  1127. RETURN VALUE
  1128. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
  1129. */
  1130. int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale)
  1131. {
  1132. int error=E_DEC_OK, intg=precision-scale,
  1133. intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
  1134. intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1,
  1135. intg1=intg0+(intg0x>0), frac1=frac0+(frac0x>0);
  1136. dec1 *buf=to->buf, mask=(*from & 0x80) ? 0 : -1;
  1137. const uchar *stop;
  1138. uchar *d_copy;
  1139. int bin_size= decimal_bin_size(precision, scale);
  1140. sanity(to);
  1141. d_copy= (uchar*) my_alloca(bin_size);
  1142. memcpy(d_copy, from, bin_size);
  1143. d_copy[0]^= 0x80;
  1144. from= d_copy;
  1145. FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
  1146. if (unlikely(error))
  1147. {
  1148. if (intg1 < intg0+(intg0x>0))
  1149. {
  1150. from+=dig2bytes[intg0x]+sizeof(dec1)*(intg0-intg1);
  1151. frac0=frac0x=intg0x=0;
  1152. intg0=intg1;
  1153. }
  1154. else
  1155. {
  1156. frac0x=0;
  1157. frac0=frac1;
  1158. }
  1159. }
  1160. to->sign=(mask != 0);
  1161. to->intg=intg0*DIG_PER_DEC1+intg0x;
  1162. to->frac=frac0*DIG_PER_DEC1+frac0x;
  1163. if (intg0x)
  1164. {
  1165. int i=dig2bytes[intg0x];
  1166. dec1 UNINIT_VAR(x);
  1167. switch (i)
  1168. {
  1169. case 1: x=mi_sint1korr(from); break;
  1170. case 2: x=mi_sint2korr(from); break;
  1171. case 3: x=mi_sint3korr(from); break;
  1172. case 4: x=mi_sint4korr(from); break;
  1173. default: DBUG_ASSERT(0);
  1174. }
  1175. from+=i;
  1176. *buf=x ^ mask;
  1177. if (((ulonglong)*buf) >= (ulonglong) powers10[intg0x+1])
  1178. goto err;
  1179. if (buf > to->buf || *buf != 0)
  1180. buf++;
  1181. else
  1182. to->intg-=intg0x;
  1183. }
  1184. for (stop=from+intg0*sizeof(dec1); from < stop; from+=sizeof(dec1))
  1185. {
  1186. DBUG_ASSERT(sizeof(dec1) == 4);
  1187. *buf=mi_sint4korr(from) ^ mask;
  1188. if (((uint32)*buf) > DIG_MAX)
  1189. goto err;
  1190. if (buf > to->buf || *buf != 0)
  1191. buf++;
  1192. else
  1193. to->intg-=DIG_PER_DEC1;
  1194. }
  1195. DBUG_ASSERT(to->intg >=0);
  1196. for (stop=from+frac0*sizeof(dec1); from < stop; from+=sizeof(dec1))
  1197. {
  1198. DBUG_ASSERT(sizeof(dec1) == 4);
  1199. *buf=mi_sint4korr(from) ^ mask;
  1200. if (((uint32)*buf) > DIG_MAX)
  1201. goto err;
  1202. buf++;
  1203. }
  1204. if (frac0x)
  1205. {
  1206. int i=dig2bytes[frac0x];
  1207. dec1 UNINIT_VAR(x);
  1208. switch (i)
  1209. {
  1210. case 1: x=mi_sint1korr(from); break;
  1211. case 2: x=mi_sint2korr(from); break;
  1212. case 3: x=mi_sint3korr(from); break;
  1213. case 4: x=mi_sint4korr(from); break;
  1214. default: DBUG_ASSERT(0);
  1215. }
  1216. *buf=(x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
  1217. if (((uint32)*buf) > DIG_MAX)
  1218. goto err;
  1219. buf++;
  1220. }
  1221. my_afree(d_copy);
  1222. /*
  1223. No digits? We have read the number zero, of unspecified precision.
  1224. Make it a proper zero, with non-zero precision.
  1225. */
  1226. if (to->intg == 0 && to->frac == 0)
  1227. decimal_make_zero(to);
  1228. return error;
  1229. err:
  1230. my_afree(d_copy);
  1231. decimal_make_zero(to);
  1232. return(E_DEC_BAD_NUM);
  1233. }
  1234. /*
  1235. Returns the size of array to hold a decimal with given precision and scale
  1236. RETURN VALUE
  1237. size in dec1
  1238. (multiply by sizeof(dec1) to get the size if bytes)
  1239. */
  1240. int decimal_size(int precision, int scale)
  1241. {
  1242. DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
  1243. return ROUND_UP(precision-scale)+ROUND_UP(scale);
  1244. }
  1245. /*
  1246. Returns the size of array to hold a binary representation of a decimal
  1247. RETURN VALUE
  1248. size in bytes
  1249. */
  1250. int decimal_bin_size(int precision, int scale)
  1251. {
  1252. int intg=precision-scale,
  1253. intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
  1254. intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1;
  1255. DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
  1256. return intg0*sizeof(dec1)+dig2bytes[intg0x]+
  1257. frac0*sizeof(dec1)+dig2bytes[frac0x];
  1258. }
  1259. /*
  1260. Rounds the decimal to "scale" digits
  1261. SYNOPSIS
  1262. decimal_round()
  1263. from - decimal to round,
  1264. to - result buffer. from==to is allowed
  1265. scale - to what position to round. can be negative!
  1266. mode - round to nearest even or truncate
  1267. NOTES
  1268. scale can be negative !
  1269. one TRUNCATED error (line XXX below) isn't treated very logical :(
  1270. RETURN VALUE
  1271. E_DEC_OK/E_DEC_TRUNCATED
  1272. */
  1273. int
  1274. decimal_round(const decimal_t *from, decimal_t *to, int scale,
  1275. decimal_round_mode mode)
  1276. {
  1277. int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1,
  1278. frac1=ROUND_UP(from->frac), UNINIT_VAR(round_digit),
  1279. intg0=ROUND_UP(from->intg), error=E_DEC_OK, len=to->len;
  1280. dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
  1281. int first_dig;
  1282. sanity(to);
  1283. switch (mode) {
  1284. case HALF_UP:
  1285. case HALF_EVEN: round_digit=5; break;
  1286. case CEILING: round_digit= from->sign ? 10 : 0; break;
  1287. case FLOOR: round_digit= from->sign ? 0 : 10; break;
  1288. case TRUNCATE: round_digit=10; break;
  1289. default: DBUG_ASSERT(0);
  1290. }
  1291. /*
  1292. For my_decimal we always use len == DECIMAL_BUFF_LENGTH == 9
  1293. For internal testing here (ifdef MAIN) we always use len == 100/4
  1294. */
  1295. DBUG_ASSERT(from->len == to->len);
  1296. if (unlikely(frac0+intg0 > len))
  1297. {
  1298. frac0=len-intg0;
  1299. scale=frac0*DIG_PER_DEC1;
  1300. error=E_DEC_TRUNCATED;
  1301. }
  1302. if (scale+from->intg < 0)
  1303. {
  1304. decimal_make_zero(to);
  1305. return E_DEC_OK;
  1306. }
  1307. if (to != from)
  1308. {
  1309. dec1 *p0= buf0+intg0+max(frac1, frac0);
  1310. dec1 *p1= buf1+intg0+max(frac1, frac0);
  1311. DBUG_ASSERT(p0 - buf0 <= len);
  1312. DBUG_ASSERT(p1 - buf1 <= len);
  1313. while (buf0 < p0)
  1314. *(--p1) = *(--p0);
  1315. buf0=to->buf;
  1316. buf1=to->buf;
  1317. to->sign=from->sign;
  1318. to->intg=min(intg0, len)*DIG_PER_DEC1;
  1319. }
  1320. if (frac0 > frac1)
  1321. {
  1322. buf1+=intg0+frac1;
  1323. while (frac0-- > frac1)
  1324. *buf1++=0;
  1325. goto done;
  1326. }
  1327. if (scale >= from->frac)
  1328. goto done; /* nothing to do */
  1329. buf0+=intg0+frac0-1;
  1330. buf1+=intg0+frac0-1;
  1331. if (scale == frac0*DIG_PER_DEC1)
  1332. {
  1333. int do_inc= FALSE;
  1334. DBUG_ASSERT(frac0+intg0 >= 0);
  1335. switch (round_digit) {
  1336. case 0:
  1337. {
  1338. dec1 *p0= buf0 + (frac1-frac0);
  1339. for (; p0 > buf0; p0--)
  1340. {
  1341. if (*p0)
  1342. {
  1343. do_inc= TRUE;
  1344. break;
  1345. }
  1346. }
  1347. break;
  1348. }
  1349. case 5:
  1350. {
  1351. x= buf0[1]/DIG_MASK;
  1352. do_inc= (x>5) || ((x == 5) &&
  1353. (mode == HALF_UP || (frac0+intg0 > 0 && *buf0 & 1)));
  1354. break;
  1355. }
  1356. default:
  1357. break;
  1358. }
  1359. if (do_inc)
  1360. {
  1361. if (frac0+intg0>0)
  1362. (*buf1)++;
  1363. else
  1364. *(++buf1)=DIG_BASE;
  1365. }
  1366. else if (frac0+intg0==0)
  1367. {
  1368. decimal_make_zero(to);
  1369. return E_DEC_OK;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. /* TODO - fix this code as it won't work for CEILING mode */
  1375. int pos=frac0*DIG_PER_DEC1-scale-1;
  1376. DBUG_ASSERT(frac0+intg0 > 0);
  1377. x=*buf1 / powers10[pos];
  1378. y=x % 10;
  1379. if (y > round_digit ||
  1380. (round_digit == 5 && y == 5 && (mode == HALF_UP || (x/10) & 1)))
  1381. x+=10;
  1382. *buf1=powers10[pos]*(x-y);
  1383. }
  1384. /*
  1385. In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
  1386. the buffer are as follows.
  1387. Before <1, 5e8>
  1388. After <2, 5e8>
  1389. Hence we need to set the 2nd field to 0.
  1390. The same holds if we round 1.5e-9 to 2e-9.
  1391. */
  1392. if (frac0 < frac1)
  1393. {
  1394. dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
  1395. dec1 *end= to->buf + len;
  1396. while (buf < end)
  1397. *buf++=0;
  1398. }
  1399. if (*buf1 >= DIG_BASE)
  1400. {
  1401. carry=1;
  1402. *buf1-=DIG_BASE;
  1403. while (carry && --buf1 >= to->buf)
  1404. ADD(*buf1, *buf1, 0, carry);
  1405. if (unlikely(carry))
  1406. {
  1407. /* shifting the number to create space for new digit */
  1408. if (frac0+intg0 >= len)
  1409. {
  1410. frac0--;
  1411. scale=frac0*DIG_PER_DEC1;
  1412. error=E_DEC_TRUNCATED; /* XXX */
  1413. }
  1414. for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
  1415. {
  1416. buf1[0]=buf1[-1];
  1417. }
  1418. *buf1=1;
  1419. to->intg++;
  1420. }
  1421. }
  1422. else
  1423. {
  1424. for (;;)
  1425. {
  1426. if (likely(*buf1))
  1427. break;
  1428. if (buf1-- == to->buf)
  1429. {
  1430. /* making 'zero' with the proper scale */
  1431. dec1 *p0= to->buf + frac0 + 1;
  1432. to->intg=1;
  1433. to->frac= max(scale, 0);
  1434. to->sign= 0;
  1435. for (buf1= to->buf; buf1<p0; buf1++)
  1436. *buf1= 0;
  1437. return E_DEC_OK;
  1438. }
  1439. }
  1440. }
  1441. /* Here we check 999.9 -> 1000 case when we need to increase intg */
  1442. first_dig= to->intg % DIG_PER_DEC1;
  1443. if (first_dig && (*buf1 >= powers10[first_dig]))
  1444. to->intg++;
  1445. if (scale<0)
  1446. scale=0;
  1447. done:
  1448. to->frac=scale;
  1449. return error;
  1450. }
  1451. /*
  1452. Returns the size of the result of the operation
  1453. SYNOPSIS
  1454. decimal_result_size()
  1455. from1 - operand of the unary operation or first operand of the
  1456. binary operation
  1457. from2 - second operand of the binary operation
  1458. op - operation. one char '+', '-', '*', '/' are allowed
  1459. others may be added later
  1460. param - extra param to the operation. unused for '+', '-', '*'
  1461. scale increment for '/'
  1462. NOTE
  1463. returned valued may be larger than the actual buffer requred
  1464. in the operation, as decimal_result_size, by design, operates on
  1465. precision/scale values only and not on the actual decimal number
  1466. RETURN VALUE
  1467. size of to->buf array in dec1 elements. to get size in bytes
  1468. multiply by sizeof(dec1)
  1469. */
  1470. int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param)
  1471. {
  1472. switch (op) {
  1473. case '-':
  1474. return ROUND_UP(max(from1->intg, from2->intg)) +
  1475. ROUND_UP(max(from1->frac, from2->frac));
  1476. case '+':
  1477. return ROUND_UP(max(from1->intg, from2->intg)+1) +
  1478. ROUND_UP(max(from1->frac, from2->frac));
  1479. case '*':
  1480. return ROUND_UP(from1->intg+from2->intg)+
  1481. ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
  1482. case '/':
  1483. return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param);
  1484. default: DBUG_ASSERT(0);
  1485. }
  1486. return -1; /* shut up the warning */
  1487. }
  1488. static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  1489. {
  1490. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1491. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
  1492. frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
  1493. dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
  1494. sanity(to);
  1495. /* is there a need for extra word because of carry ? */
  1496. x=intg1 > intg2 ? from1->buf[0] :
  1497. intg2 > intg1 ? from2->buf[0] :
  1498. from1->buf[0] + from2->buf[0] ;
  1499. if (unlikely(x > DIG_MAX-1)) /* yes, there is */
  1500. {
  1501. intg0++;
  1502. to->buf[0]=0; /* safety */
  1503. }
  1504. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
  1505. if (unlikely(error == E_DEC_OVERFLOW))
  1506. {
  1507. max_decimal(to->len * DIG_PER_DEC1, 0, to);
  1508. return error;
  1509. }
  1510. buf0=to->buf+intg0+frac0;
  1511. to->sign=from1->sign;
  1512. to->frac=max(from1->frac, from2->frac);
  1513. to->intg=intg0*DIG_PER_DEC1;
  1514. if (unlikely(error))
  1515. {
  1516. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1517. set_if_smaller(frac1, frac0);
  1518. set_if_smaller(frac2, frac0);
  1519. set_if_smaller(intg1, intg0);
  1520. set_if_smaller(intg2, intg0);
  1521. }
  1522. /* part 1 - max(frac) ... min (frac) */
  1523. if (frac1 > frac2)
  1524. {
  1525. buf1=from1->buf+intg1+frac1;
  1526. stop=from1->buf+intg1+frac2;
  1527. buf2=from2->buf+intg2+frac2;
  1528. stop2=from1->buf+(intg1 > intg2 ? intg1-intg2 : 0);
  1529. }
  1530. else
  1531. {
  1532. buf1=from2->buf+intg2+frac2;
  1533. stop=from2->buf+intg2+frac1;
  1534. buf2=from1->buf+intg1+frac1;
  1535. stop2=from2->buf+(intg2 > intg1 ? intg2-intg1 : 0);
  1536. }
  1537. while (buf1 > stop)
  1538. *--buf0=*--buf1;
  1539. /* part 2 - min(frac) ... min(intg) */
  1540. carry=0;
  1541. while (buf1 > stop2)
  1542. {
  1543. ADD(*--buf0, *--buf1, *--buf2, carry);
  1544. }
  1545. /* part 3 - min(intg) ... max(intg) */
  1546. buf1= intg1 > intg2 ? ((stop=from1->buf)+intg1-intg2) :
  1547. ((stop=from2->buf)+intg2-intg1) ;
  1548. while (buf1 > stop)
  1549. {
  1550. ADD(*--buf0, *--buf1, 0, carry);
  1551. }
  1552. if (unlikely(carry))
  1553. *--buf0=1;
  1554. DBUG_ASSERT(buf0 == to->buf || buf0 == to->buf+1);
  1555. return error;
  1556. }
  1557. /* to=from1-from2.
  1558. if to==0, return -1/0/+1 - the result of the comparison */
  1559. static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  1560. {
  1561. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1562. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
  1563. int frac0=max(frac1, frac2), error;
  1564. dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
  1565. /* let carry:=1 if from2 > from1 */
  1566. start1=buf1=from1->buf; stop1=buf1+intg1;
  1567. start2=buf2=from2->buf; stop2=buf2+intg2;
  1568. if (unlikely(*buf1 == 0))
  1569. {
  1570. while (buf1 < stop1 && *buf1 == 0)
  1571. buf1++;
  1572. start1=buf1;
  1573. intg1= (int) (stop1-buf1);
  1574. }
  1575. if (unlikely(*buf2 == 0))
  1576. {
  1577. while (buf2 < stop2 && *buf2 == 0)
  1578. buf2++;
  1579. start2=buf2;
  1580. intg2= (int) (stop2-buf2);
  1581. }
  1582. if (intg2 > intg1)
  1583. carry=1;
  1584. else if (intg2 == intg1)
  1585. {
  1586. dec1 *end1= stop1 + (frac1 - 1);
  1587. dec1 *end2= stop2 + (frac2 - 1);
  1588. while (unlikely((buf1 <= end1) && (*end1 == 0)))
  1589. end1--;
  1590. while (unlikely((buf2 <= end2) && (*end2 == 0)))
  1591. end2--;
  1592. frac1= (int) (end1 - stop1) + 1;
  1593. frac2= (int) (end2 - stop2) + 1;
  1594. while (buf1 <=end1 && buf2 <= end2 && *buf1 == *buf2)
  1595. buf1++, buf2++;
  1596. if (buf1 <= end1)
  1597. {
  1598. if (buf2 <= end2)
  1599. carry= *buf2 > *buf1;
  1600. else
  1601. carry= 0;
  1602. }
  1603. else
  1604. {
  1605. if (buf2 <= end2)
  1606. carry=1;
  1607. else /* short-circuit everything: from1 == from2 */
  1608. {
  1609. if (to == 0) /* decimal_cmp() */
  1610. return 0;
  1611. decimal_make_zero(to);
  1612. return E_DEC_OK;
  1613. }
  1614. }
  1615. }
  1616. if (to == 0) /* decimal_cmp() */
  1617. return carry == from1->sign ? 1 : -1;
  1618. sanity(to);
  1619. to->sign=from1->sign;
  1620. /* ensure that always from1 > from2 (and intg1 >= intg2) */
  1621. if (carry)
  1622. {
  1623. swap_variables(const decimal_t *, from1, from2);
  1624. swap_variables(dec1 *,start1, start2);
  1625. swap_variables(int,intg1,intg2);
  1626. swap_variables(int,frac1,frac2);
  1627. to->sign= 1 - to->sign;
  1628. }
  1629. FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
  1630. buf0=to->buf+intg1+frac0;
  1631. to->frac=max(from1->frac, from2->frac);
  1632. to->intg=intg1*DIG_PER_DEC1;
  1633. if (unlikely(error))
  1634. {
  1635. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1636. set_if_smaller(frac1, frac0);
  1637. set_if_smaller(frac2, frac0);
  1638. set_if_smaller(intg2, intg1);
  1639. }
  1640. carry=0;
  1641. /* part 1 - max(frac) ... min (frac) */
  1642. if (frac1 > frac2)
  1643. {
  1644. buf1=start1+intg1+frac1;
  1645. stop1=start1+intg1+frac2;
  1646. buf2=start2+intg2+frac2;
  1647. while (frac0-- > frac1)
  1648. *--buf0=0;
  1649. while (buf1 > stop1)
  1650. *--buf0=*--buf1;
  1651. }
  1652. else
  1653. {
  1654. buf1=start1+intg1+frac1;
  1655. buf2=start2+intg2+frac2;
  1656. stop2=start2+intg2+frac1;
  1657. while (frac0-- > frac2)
  1658. *--buf0=0;
  1659. while (buf2 > stop2)
  1660. {
  1661. SUB(*--buf0, 0, *--buf2, carry);
  1662. }
  1663. }
  1664. /* part 2 - min(frac) ... intg2 */
  1665. while (buf2 > start2)
  1666. {
  1667. SUB(*--buf0, *--buf1, *--buf2, carry);
  1668. }
  1669. /* part 3 - intg2 ... intg1 */
  1670. while (carry && buf1 > start1)
  1671. {
  1672. SUB(*--buf0, *--buf1, 0, carry);
  1673. }
  1674. while (buf1 > start1)
  1675. *--buf0=*--buf1;
  1676. while (buf0 > to->buf)
  1677. *--buf0=0;
  1678. return error;
  1679. }
  1680. int decimal_intg(const decimal_t *from)
  1681. {
  1682. int res;
  1683. remove_leading_zeroes(from, &res);
  1684. return res;
  1685. }
  1686. int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  1687. {
  1688. if (likely(from1->sign == from2->sign))
  1689. return do_add(from1, from2, to);
  1690. return do_sub(from1, from2, to);
  1691. }
  1692. int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  1693. {
  1694. if (likely(from1->sign == from2->sign))
  1695. return do_sub(from1, from2, to);
  1696. return do_add(from1, from2, to);
  1697. }
  1698. int decimal_cmp(const decimal_t *from1, const decimal_t *from2)
  1699. {
  1700. if (likely(from1->sign == from2->sign))
  1701. return do_sub(from1, from2, 0);
  1702. return from1->sign > from2->sign ? -1 : 1;
  1703. }
  1704. int decimal_is_zero(const decimal_t *from)
  1705. {
  1706. dec1 *buf1=from->buf,
  1707. *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac);
  1708. while (buf1 < end)
  1709. if (*buf1++)
  1710. return 0;
  1711. return 1;
  1712. }
  1713. /*
  1714. multiply two decimals
  1715. SYNOPSIS
  1716. decimal_mul()
  1717. from1, from2 - factors
  1718. to - product
  1719. RETURN VALUE
  1720. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
  1721. NOTES
  1722. in this implementation, with sizeof(dec1)=4 we have DIG_PER_DEC1=9,
  1723. and 63-digit number will take only 7 dec1 words (basically a 7-digit
  1724. "base 999999999" number). Thus there's no need in fast multiplication
  1725. algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
  1726. method.
  1727. XXX if this library is to be used with huge numbers of thousands of
  1728. digits, fast multiplication must be implemented.
  1729. */
  1730. int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  1731. {
  1732. int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
  1733. frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
  1734. intg0=ROUND_UP(from1->intg+from2->intg),
  1735. frac0=frac1+frac2, error, i, j, d_to_move;
  1736. dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
  1737. *start2, *stop2, *stop1, *start0, carry;
  1738. sanity(to);
  1739. i=intg0; /* save 'ideal' values */
  1740. j=frac0;
  1741. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error); /* bound size */
  1742. to->sign=from1->sign != from2->sign;
  1743. to->frac=from1->frac+from2->frac; /* store size in digits */
  1744. to->intg=intg0*DIG_PER_DEC1;
  1745. if (unlikely(error))
  1746. {
  1747. set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
  1748. set_if_smaller(to->intg, intg0*DIG_PER_DEC1);
  1749. if (unlikely(i > intg0)) /* bounded integer-part */
  1750. {
  1751. i-=intg0;
  1752. j=i >> 1;
  1753. intg1-= j;
  1754. intg2-=i-j;
  1755. frac1=frac2=0; /* frac0 is already 0 here */
  1756. }
  1757. else /* bounded fract part */
  1758. {
  1759. j-=frac0;
  1760. i=j >> 1;
  1761. if (frac1 <= frac2)
  1762. {
  1763. frac1-= i;
  1764. frac2-=j-i;
  1765. }
  1766. else
  1767. {
  1768. frac2-= i;
  1769. frac1-=j-i;
  1770. }
  1771. }
  1772. }
  1773. start0=to->buf+intg0+frac0-1;
  1774. start2=buf2+frac2-1;
  1775. stop1=buf1-intg1;
  1776. stop2=buf2-intg2;
  1777. bzero(to->buf, (intg0+frac0)*sizeof(dec1));
  1778. for (buf1+=frac1-1; buf1 >= stop1; buf1--, start0--)
  1779. {
  1780. carry=0;
  1781. for (buf0=start0, buf2=start2; buf2 >= stop2; buf2--, buf0--)
  1782. {
  1783. dec1 hi, lo;
  1784. dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
  1785. hi=(dec1)(p/DIG_BASE);
  1786. lo=(dec1)(p-((dec2)hi)*DIG_BASE);
  1787. ADD2(*buf0, *buf0, lo, carry);
  1788. carry+=hi;
  1789. }
  1790. if (carry)
  1791. {
  1792. if (buf0 < to->buf)
  1793. return E_DEC_OVERFLOW;
  1794. ADD2(*buf0, *buf0, 0, carry);
  1795. }
  1796. for (buf0--; carry; buf0--)
  1797. {
  1798. if (buf0 < to->buf)
  1799. return E_DEC_OVERFLOW;
  1800. ADD(*buf0, *buf0, 0, carry);
  1801. }
  1802. }
  1803. /* Now we have to check for -0.000 case */
  1804. if (to->sign)
  1805. {
  1806. dec1 *buf= to->buf;
  1807. dec1 *end= to->buf + intg0 + frac0;
  1808. DBUG_ASSERT(buf != end);
  1809. for (;;)
  1810. {
  1811. if (*buf)
  1812. break;
  1813. if (++buf == end)
  1814. {
  1815. /* We got decimal zero */
  1816. decimal_make_zero(to);
  1817. break;
  1818. }
  1819. }
  1820. }
  1821. buf1= to->buf;
  1822. d_to_move= intg0 + ROUND_UP(to->frac);
  1823. while (!*buf1 && (to->intg > DIG_PER_DEC1))
  1824. {
  1825. buf1++;
  1826. to->intg-= DIG_PER_DEC1;
  1827. d_to_move--;
  1828. }
  1829. if (to->buf < buf1)
  1830. {
  1831. dec1 *cur_d= to->buf;
  1832. for (; d_to_move--; cur_d++, buf1++)
  1833. *cur_d= *buf1;
  1834. }
  1835. return error;
  1836. }
  1837. /*
  1838. naive division algorithm (Knuth's Algorithm D in 4.3.1) -
  1839. it's ok for short numbers
  1840. also we're using alloca() to allocate a temporary buffer
  1841. XXX if this library is to be used with huge numbers of thousands of
  1842. digits, fast division must be implemented and alloca should be
  1843. changed to malloc (or at least fallback to malloc if alloca() fails)
  1844. but then, decimal_mul() should be rewritten too :(
  1845. */
  1846. static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
  1847. decimal_t *to, decimal_t *mod, int scale_incr)
  1848. {
  1849. int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
  1850. frac2=ROUND_UP(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
  1851. UNINIT_VAR(error), i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
  1852. dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
  1853. *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
  1854. dec2 norm_factor, x, guess, y;
  1855. if (mod)
  1856. to=mod;
  1857. sanity(to);
  1858. /* removing all the leading zeroes */
  1859. i= ((prec2 - 1) % DIG_PER_DEC1) + 1;
  1860. while (prec2 > 0 && *buf2 == 0)
  1861. {
  1862. prec2-= i;
  1863. i= DIG_PER_DEC1;
  1864. buf2++;
  1865. }
  1866. if (prec2 <= 0) /* short-circuit everything: from2 == 0 */
  1867. return E_DEC_DIV_ZERO;
  1868. for (i= (prec2 - 1) % DIG_PER_DEC1; *buf2 < powers10[i--]; prec2--) ;
  1869. DBUG_ASSERT(prec2 > 0);
  1870. i=((prec1-1) % DIG_PER_DEC1)+1;
  1871. while (prec1 > 0 && *buf1 == 0)
  1872. {
  1873. prec1-=i;
  1874. i=DIG_PER_DEC1;
  1875. buf1++;
  1876. }
  1877. if (prec1 <= 0)
  1878. { /* short-circuit everything: from1 == 0 */
  1879. decimal_make_zero(to);
  1880. return E_DEC_OK;
  1881. }
  1882. for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
  1883. DBUG_ASSERT(prec1 > 0);
  1884. /* let's fix scale_incr, taking into account frac1,frac2 increase */
  1885. if ((scale_incr-= frac1 - from1->frac + frac2 - from2->frac) < 0)
  1886. scale_incr=0;
  1887. dintg=(prec1-frac1)-(prec2-frac2)+(*buf1 >= *buf2);
  1888. if (dintg < 0)
  1889. {
  1890. dintg/=DIG_PER_DEC1;
  1891. intg0=0;
  1892. }
  1893. else
  1894. intg0=ROUND_UP(dintg);
  1895. if (mod)
  1896. {
  1897. /* we're calculating N1 % N2.
  1898. The result will have
  1899. frac=max(frac1, frac2), as for subtraction
  1900. intg=intg2
  1901. */
  1902. to->sign=from1->sign;
  1903. to->frac=max(from1->frac, from2->frac);
  1904. frac0=0;
  1905. }
  1906. else
  1907. {
  1908. /*
  1909. we're calculating N1/N2. N1 is in the buf1, has prec1 digits
  1910. N2 is in the buf2, has prec2 digits. Scales are frac1 and
  1911. frac2 accordingly.
  1912. Thus, the result will have
  1913. frac = ROUND_UP(frac1+frac2+scale_incr)
  1914. and
  1915. intg = (prec1-frac1) - (prec2-frac2) + 1
  1916. prec = intg+frac
  1917. */
  1918. frac0=ROUND_UP(frac1+frac2+scale_incr);
  1919. FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
  1920. to->sign=from1->sign != from2->sign;
  1921. to->intg=intg0*DIG_PER_DEC1;
  1922. to->frac=frac0*DIG_PER_DEC1;
  1923. }
  1924. buf0=to->buf;
  1925. stop0=buf0+intg0+frac0;
  1926. if (likely(div_mod))
  1927. while (dintg++ < 0 && buf0 < &to->buf[to->len])
  1928. {
  1929. *buf0++=0;
  1930. }
  1931. len1=(i=ROUND_UP(prec1))+ROUND_UP(2*frac2+scale_incr+1) + 1;
  1932. set_if_bigger(len1, 3);
  1933. if (!(tmp1=(dec1 *)my_alloca(len1*sizeof(dec1))))
  1934. return E_DEC_OOM;
  1935. memcpy(tmp1, buf1, i*sizeof(dec1));
  1936. bzero(tmp1+i, (len1-i)*sizeof(dec1));
  1937. start1=tmp1;
  1938. stop1=start1+len1;
  1939. start2=buf2;
  1940. stop2=buf2+ROUND_UP(prec2)-1;
  1941. /* removing end zeroes */
  1942. while (*stop2 == 0 && stop2 >= start2)
  1943. stop2--;
  1944. len2= (int) (stop2++ - start2);
  1945. /*
  1946. calculating norm2 (normalized *start2) - we need *start2 to be large
  1947. (at least > DIG_BASE/2), but unlike Knuth's Alg. D we don't want to
  1948. normalize input numbers (as we don't make a copy of the divisor).
  1949. Thus we normalize first dec1 of buf2 only, and we'll normalize *start1
  1950. on the fly for the purpose of guesstimation only.
  1951. It's also faster, as we're saving on normalization of buf2
  1952. */
  1953. norm_factor=DIG_BASE/(*start2+1);
  1954. norm2=(dec1)(norm_factor*start2[0]);
  1955. if (likely(len2>0))
  1956. norm2+=(dec1)(norm_factor*start2[1]/DIG_BASE);
  1957. if (*start1 < *start2)
  1958. dcarry=*start1++;
  1959. else
  1960. dcarry=0;
  1961. /* main loop */
  1962. for (; buf0 < stop0; buf0++)
  1963. {
  1964. /* short-circuit, if possible */
  1965. if (unlikely(dcarry == 0 && *start1 < *start2))
  1966. guess=0;
  1967. else
  1968. {
  1969. /* D3: make a guess */
  1970. x=start1[0]+((dec2)dcarry)*DIG_BASE;
  1971. y=start1[1];
  1972. guess=(norm_factor*x+norm_factor*y/DIG_BASE)/norm2;
  1973. if (unlikely(guess >= DIG_BASE))
  1974. guess=DIG_BASE-1;
  1975. if (likely(len2>0))
  1976. {
  1977. /* hmm, this is a suspicious trick - I removed normalization here */
  1978. if (start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y)
  1979. guess--;
  1980. if (unlikely(start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y))
  1981. guess--;
  1982. DBUG_ASSERT(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
  1983. }
  1984. /* D4: multiply and subtract */
  1985. buf2=stop2;
  1986. buf1=start1+len2;
  1987. DBUG_ASSERT(buf1 < stop1);
  1988. for (carry=0; buf2 > start2; buf1--)
  1989. {
  1990. dec1 hi, lo;
  1991. x=guess * (*--buf2);
  1992. hi=(dec1)(x/DIG_BASE);
  1993. lo=(dec1)(x-((dec2)hi)*DIG_BASE);
  1994. SUB2(*buf1, *buf1, lo, carry);
  1995. carry+=hi;
  1996. }
  1997. carry= dcarry < carry;
  1998. /* D5: check the remainder */
  1999. if (unlikely(carry))
  2000. {
  2001. /* D6: correct the guess */
  2002. guess--;
  2003. buf2=stop2;
  2004. buf1=start1+len2;
  2005. for (carry=0; buf2 > start2; buf1--)
  2006. {
  2007. ADD(*buf1, *buf1, *--buf2, carry);
  2008. }
  2009. }
  2010. }
  2011. if (likely(div_mod))
  2012. {
  2013. DBUG_ASSERT(buf0 < to->buf + to->len);
  2014. *buf0=(dec1)guess;
  2015. }
  2016. dcarry= *start1;
  2017. start1++;
  2018. }
  2019. if (mod)
  2020. {
  2021. /*
  2022. now the result is in tmp1, it has
  2023. intg=prec1-frac1
  2024. frac=max(frac1, frac2)=to->frac
  2025. */
  2026. if (dcarry)
  2027. *--start1=dcarry;
  2028. buf0=to->buf;
  2029. intg0=(int) (ROUND_UP(prec1-frac1)-(start1-tmp1));
  2030. frac0=ROUND_UP(to->frac);
  2031. error=E_DEC_OK;
  2032. if (unlikely(frac0==0 && intg0==0))
  2033. {
  2034. decimal_make_zero(to);
  2035. goto done;
  2036. }
  2037. if (intg0<=0)
  2038. {
  2039. if (unlikely(-intg0 >= to->len))
  2040. {
  2041. decimal_make_zero(to);
  2042. error=E_DEC_TRUNCATED;
  2043. goto done;
  2044. }
  2045. stop1=start1+frac0;
  2046. frac0+=intg0;
  2047. to->intg=0;
  2048. while (intg0++ < 0)
  2049. *buf0++=0;
  2050. }
  2051. else
  2052. {
  2053. if (unlikely(intg0 > to->len))
  2054. {
  2055. frac0=0;
  2056. intg0=to->len;
  2057. error=E_DEC_OVERFLOW;
  2058. goto done;
  2059. }
  2060. DBUG_ASSERT(intg0 <= ROUND_UP(from2->intg));
  2061. stop1=start1+frac0+intg0;
  2062. to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
  2063. }
  2064. if (unlikely(intg0+frac0 > to->len))
  2065. {
  2066. stop1-=frac0+intg0-to->len;
  2067. frac0=to->len-intg0;
  2068. to->frac=frac0*DIG_PER_DEC1;
  2069. error=E_DEC_TRUNCATED;
  2070. }
  2071. DBUG_ASSERT(buf0 + (stop1 - start1) <= to->buf + to->len);
  2072. while (start1 < stop1)
  2073. *buf0++=*start1++;
  2074. }
  2075. done:
  2076. my_afree(tmp1);
  2077. return error;
  2078. }
  2079. /*
  2080. division of two decimals
  2081. SYNOPSIS
  2082. decimal_div()
  2083. from1 - dividend
  2084. from2 - divisor
  2085. to - quotient
  2086. RETURN VALUE
  2087. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
  2088. NOTES
  2089. see do_div_mod()
  2090. */
  2091. int
  2092. decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
  2093. int scale_incr)
  2094. {
  2095. return do_div_mod(from1, from2, to, 0, scale_incr);
  2096. }
  2097. /*
  2098. modulus
  2099. SYNOPSIS
  2100. decimal_mod()
  2101. from1 - dividend
  2102. from2 - divisor
  2103. to - modulus
  2104. RETURN VALUE
  2105. E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
  2106. NOTES
  2107. see do_div_mod()
  2108. DESCRIPTION
  2109. the modulus R in R = M mod N
  2110. is defined as
  2111. 0 <= |R| < |M|
  2112. sign R == sign M
  2113. R = M - k*N, where k is integer
  2114. thus, there's no requirement for M or N to be integers
  2115. */
  2116. int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
  2117. {
  2118. return do_div_mod(from1, from2, 0, to, 0);
  2119. }
  2120. #ifdef MAIN
  2121. int full= 0;
  2122. decimal_t a, b, c;
  2123. char buf1[100], buf2[100], buf3[100];
  2124. void dump_decimal(decimal_t *d)
  2125. {
  2126. int i;
  2127. printf("/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
  2128. for (i=0; i < ROUND_UP(d->frac)+ROUND_UP(d->intg)-1; i++)
  2129. printf("%09d, ", d->buf[i]);
  2130. printf("%09d} */ ", d->buf[i]);
  2131. }
  2132. void check_result_code(int actual, int want)
  2133. {
  2134. if (actual != want)
  2135. {
  2136. printf("\n^^^^^^^^^^^^^ must return %d\n", want);
  2137. exit(1);
  2138. }
  2139. }
  2140. void print_decimal(decimal_t *d, const char *orig, int actual, int want)
  2141. {
  2142. char s[100];
  2143. int slen=sizeof(s);
  2144. if (full) dump_decimal(d);
  2145. decimal2string(d, s, &slen, 0, 0, 0);
  2146. printf("'%s'", s);
  2147. check_result_code(actual, want);
  2148. if (orig && strcmp(orig, s))
  2149. {
  2150. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2151. exit(1);
  2152. }
  2153. }
  2154. void test_d2s()
  2155. {
  2156. char s[100];
  2157. int slen, res;
  2158. /***********************************/
  2159. printf("==== decimal2string ====\n");
  2160. a.buf[0]=12345; a.intg=5; a.frac=0; a.sign=0;
  2161. slen=sizeof(s);
  2162. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2163. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2164. a.buf[1]=987000000; a.frac=3;
  2165. slen=sizeof(s);
  2166. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2167. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2168. a.sign=1;
  2169. slen=sizeof(s);
  2170. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2171. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2172. slen=8;
  2173. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2174. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2175. slen=5;
  2176. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2177. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2178. a.buf[0]=987000000; a.frac=3; a.intg=0;
  2179. slen=sizeof(s);
  2180. res=decimal2string(&a, s, &slen, 0, 0, 0);
  2181. dump_decimal(&a); printf(" --> res=%d str='%s' len=%d\n", res, s, slen);
  2182. }
  2183. void test_s2d(const char *s, const char *orig, int ex)
  2184. {
  2185. char s1[100], *end;
  2186. int res;
  2187. sprintf(s1, "'%s'", s);
  2188. end= strend(s);
  2189. printf("len=%2d %-30s => res=%d ", a.len, s1,
  2190. (res= string2decimal(s, &a, &end)));
  2191. print_decimal(&a, orig, res, ex);
  2192. printf("\n");
  2193. }
  2194. void test_d2f(const char *s, int ex)
  2195. {
  2196. char s1[100], *end;
  2197. double x;
  2198. int res;
  2199. sprintf(s1, "'%s'", s);
  2200. end= strend(s);
  2201. string2decimal(s, &a, &end);
  2202. res=decimal2double(&a, &x);
  2203. if (full) dump_decimal(&a);
  2204. printf("%-40s => res=%d %.*g\n", s1, res, a.intg+a.frac, x);
  2205. check_result_code(res, ex);
  2206. }
  2207. void test_d2b2d(const char *str, int p, int s, const char *orig, int ex)
  2208. {
  2209. char s1[100], buf[100], *end;
  2210. int res, i, size=decimal_bin_size(p, s);
  2211. sprintf(s1, "'%s'", str);
  2212. end= strend(str);
  2213. string2decimal(str, &a, &end);
  2214. res=decimal2bin(&a, buf, p, s);
  2215. printf("%-31s {%2d, %2d} => res=%d size=%-2d ", s1, p, s, res, size);
  2216. if (full)
  2217. {
  2218. printf("0x");
  2219. for (i=0; i < size; i++)
  2220. printf("%02x", ((uchar *)buf)[i]);
  2221. }
  2222. res=bin2decimal(buf, &a, p, s);
  2223. printf(" => res=%d ", res);
  2224. print_decimal(&a, orig, res, ex);
  2225. printf("\n");
  2226. }
  2227. void test_f2d(double from, int ex)
  2228. {
  2229. int res;
  2230. res=double2decimal(from, &a);
  2231. printf("%-40.*f => res=%d ", DBL_DIG-2, from, res);
  2232. print_decimal(&a, 0, res, ex);
  2233. printf("\n");
  2234. }
  2235. void test_ull2d(ulonglong from, const char *orig, int ex)
  2236. {
  2237. char s[100];
  2238. int res;
  2239. res=ulonglong2decimal(from, &a);
  2240. longlong10_to_str(from,s,10);
  2241. printf("%-40s => res=%d ", s, res);
  2242. print_decimal(&a, orig, res, ex);
  2243. printf("\n");
  2244. }
  2245. void test_ll2d(longlong from, const char *orig, int ex)
  2246. {
  2247. char s[100];
  2248. int res;
  2249. res=longlong2decimal(from, &a);
  2250. longlong10_to_str(from,s,-10);
  2251. printf("%-40s => res=%d ", s, res);
  2252. print_decimal(&a, orig, res, ex);
  2253. printf("\n");
  2254. }
  2255. void test_d2ull(const char *s, const char *orig, int ex)
  2256. {
  2257. char s1[100], *end;
  2258. ulonglong x;
  2259. int res;
  2260. end= strend(s);
  2261. string2decimal(s, &a, &end);
  2262. res=decimal2ulonglong(&a, &x);
  2263. if (full) dump_decimal(&a);
  2264. longlong10_to_str(x,s1,10);
  2265. printf("%-40s => res=%d %s\n", s, res, s1);
  2266. check_result_code(res, ex);
  2267. if (orig && strcmp(orig, s1))
  2268. {
  2269. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2270. exit(1);
  2271. }
  2272. }
  2273. void test_d2ll(const char *s, const char *orig, int ex)
  2274. {
  2275. char s1[100], *end;
  2276. longlong x;
  2277. int res;
  2278. end= strend(s);
  2279. string2decimal(s, &a, &end);
  2280. res=decimal2longlong(&a, &x);
  2281. if (full) dump_decimal(&a);
  2282. longlong10_to_str(x,s1,-10);
  2283. printf("%-40s => res=%d %s\n", s, res, s1);
  2284. check_result_code(res, ex);
  2285. if (orig && strcmp(orig, s1))
  2286. {
  2287. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2288. exit(1);
  2289. }
  2290. }
  2291. void test_da(const char *s1, const char *s2, const char *orig, int ex)
  2292. {
  2293. char s[100], *end;
  2294. int res;
  2295. sprintf(s, "'%s' + '%s'", s1, s2);
  2296. end= strend(s1);
  2297. string2decimal(s1, &a, &end);
  2298. end= strend(s2);
  2299. string2decimal(s2, &b, &end);
  2300. res=decimal_add(&a, &b, &c);
  2301. printf("%-40s => res=%d ", s, res);
  2302. print_decimal(&c, orig, res, ex);
  2303. printf("\n");
  2304. }
  2305. void test_ds(const char *s1, const char *s2, const char *orig, int ex)
  2306. {
  2307. char s[100], *end;
  2308. int res;
  2309. sprintf(s, "'%s' - '%s'", s1, s2);
  2310. end= strend(s1);
  2311. string2decimal(s1, &a, &end);
  2312. end= strend(s2);
  2313. string2decimal(s2, &b, &end);
  2314. res=decimal_sub(&a, &b, &c);
  2315. printf("%-40s => res=%d ", s, res);
  2316. print_decimal(&c, orig, res, ex);
  2317. printf("\n");
  2318. }
  2319. void test_dc(const char *s1, const char *s2, int orig)
  2320. {
  2321. char s[100], *end;
  2322. int res;
  2323. sprintf(s, "'%s' <=> '%s'", s1, s2);
  2324. end= strend(s1);
  2325. string2decimal(s1, &a, &end);
  2326. end= strend(s2);
  2327. string2decimal(s2, &b, &end);
  2328. res=decimal_cmp(&a, &b);
  2329. printf("%-40s => res=%d\n", s, res);
  2330. if (orig != res)
  2331. {
  2332. printf("\n^^^^^^^^^^^^^ must've been %d\n", orig);
  2333. exit(1);
  2334. }
  2335. }
  2336. void test_dm(const char *s1, const char *s2, const char *orig, int ex)
  2337. {
  2338. char s[100], *end;
  2339. int res;
  2340. sprintf(s, "'%s' * '%s'", s1, s2);
  2341. end= strend(s1);
  2342. string2decimal(s1, &a, &end);
  2343. end= strend(s2);
  2344. string2decimal(s2, &b, &end);
  2345. res=decimal_mul(&a, &b, &c);
  2346. printf("%-40s => res=%d ", s, res);
  2347. print_decimal(&c, orig, res, ex);
  2348. printf("\n");
  2349. }
  2350. void test_dv(const char *s1, const char *s2, const char *orig, int ex)
  2351. {
  2352. char s[100], *end;
  2353. int res;
  2354. sprintf(s, "'%s' / '%s'", s1, s2);
  2355. end= strend(s1);
  2356. string2decimal(s1, &a, &end);
  2357. end= strend(s2);
  2358. string2decimal(s2, &b, &end);
  2359. res=decimal_div(&a, &b, &c, 5);
  2360. printf("%-40s => res=%d ", s, res);
  2361. check_result_code(res, ex);
  2362. if (res == E_DEC_DIV_ZERO)
  2363. printf("E_DEC_DIV_ZERO");
  2364. else
  2365. print_decimal(&c, orig, res, ex);
  2366. printf("\n");
  2367. }
  2368. void test_md(const char *s1, const char *s2, const char *orig, int ex)
  2369. {
  2370. char s[100], *end;
  2371. int res;
  2372. sprintf(s, "'%s' %% '%s'", s1, s2);
  2373. end= strend(s1);
  2374. string2decimal(s1, &a, &end);
  2375. end= strend(s2);
  2376. string2decimal(s2, &b, &end);
  2377. res=decimal_mod(&a, &b, &c);
  2378. printf("%-40s => res=%d ", s, res);
  2379. check_result_code(res, ex);
  2380. if (res == E_DEC_DIV_ZERO)
  2381. printf("E_DEC_DIV_ZERO");
  2382. else
  2383. print_decimal(&c, orig, res, ex);
  2384. printf("\n");
  2385. }
  2386. const char *round_mode[]=
  2387. {"TRUNCATE", "HALF_EVEN", "HALF_UP", "CEILING", "FLOOR"};
  2388. void test_ro(const char *s1, int n, decimal_round_mode mode, const char *orig,
  2389. int ex)
  2390. {
  2391. char s[100], *end;
  2392. int res;
  2393. sprintf(s, "'%s', %d, %s", s1, n, round_mode[mode]);
  2394. end= strend(s1);
  2395. string2decimal(s1, &a, &end);
  2396. res=decimal_round(&a, &b, n, mode);
  2397. printf("%-40s => res=%d ", s, res);
  2398. print_decimal(&b, orig, res, ex);
  2399. printf("\n");
  2400. }
  2401. void test_mx(int precision, int frac, const char *orig)
  2402. {
  2403. char s[100];
  2404. sprintf(s, "%d, %d", precision, frac);
  2405. max_decimal(precision, frac, &a);
  2406. printf("%-40s => ", s);
  2407. print_decimal(&a, orig, 0, 0);
  2408. printf("\n");
  2409. }
  2410. void test_pr(const char *s1, int prec, int dec, char filler, const char *orig,
  2411. int ex)
  2412. {
  2413. char s[100], *end;
  2414. char s2[100];
  2415. int slen= sizeof(s2);
  2416. int res;
  2417. sprintf(s, filler ? "'%s', %d, %d, '%c'" : "'%s', %d, %d, '\\0'",
  2418. s1, prec, dec, filler);
  2419. end= strend(s1);
  2420. string2decimal(s1, &a, &end);
  2421. res= decimal2string(&a, s2, &slen, prec, dec, filler);
  2422. printf("%-40s => res=%d '%s'", s, res, s2);
  2423. check_result_code(res, ex);
  2424. if (orig && strcmp(orig, s2))
  2425. {
  2426. printf("\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
  2427. exit(1);
  2428. }
  2429. printf("\n");
  2430. }
  2431. void test_sh(const char *s1, int shift, const char *orig, int ex)
  2432. {
  2433. char s[100], *end;
  2434. int res;
  2435. sprintf(s, "'%s' %s %d", s1, ((shift < 0) ? ">>" : "<<"), abs(shift));
  2436. end= strend(s1);
  2437. string2decimal(s1, &a, &end);
  2438. res= decimal_shift(&a, shift);
  2439. printf("%-40s => res=%d ", s, res);
  2440. print_decimal(&a, orig, res, ex);
  2441. printf("\n");
  2442. }
  2443. void test_fr(const char *s1, const char *orig)
  2444. {
  2445. char s[100], *end;
  2446. sprintf(s, "'%s'", s1);
  2447. printf("%-40s => ", s);
  2448. end= strend(s1);
  2449. string2decimal(s1, &a, &end);
  2450. a.frac= decimal_actual_fraction(&a);
  2451. print_decimal(&a, orig, 0, 0);
  2452. printf("\n");
  2453. }
  2454. int main()
  2455. {
  2456. a.buf=(void*)buf1;
  2457. a.len=sizeof(buf1)/sizeof(dec1);
  2458. b.buf=(void*)buf2;
  2459. b.len=sizeof(buf2)/sizeof(dec1);
  2460. c.buf=(void*)buf3;
  2461. c.len=sizeof(buf3)/sizeof(dec1);
  2462. if (full)
  2463. test_d2s();
  2464. printf("==== string2decimal ====\n");
  2465. test_s2d("12345", "12345", 0);
  2466. test_s2d("12345.", "12345", 0);
  2467. test_s2d("123.45", "123.45", 0);
  2468. test_s2d("-123.45", "-123.45", 0);
  2469. test_s2d(".00012345000098765", "0.00012345000098765", 0);
  2470. test_s2d(".12345000098765", "0.12345000098765", 0);
  2471. test_s2d("-.000000012345000098765", "-0.000000012345000098765", 0);
  2472. test_s2d("1234500009876.5", "1234500009876.5", 0);
  2473. a.len=1;
  2474. test_s2d("123450000098765", "98765", 2);
  2475. test_s2d("123450.000098765", "123450", 1);
  2476. a.len=sizeof(buf1)/sizeof(dec1);
  2477. test_s2d("123E5", "12300000", 0);
  2478. test_s2d("123E-2", "1.23", 0);
  2479. printf("==== decimal2double ====\n");
  2480. test_d2f("12345", 0);
  2481. test_d2f("123.45", 0);
  2482. test_d2f("-123.45", 0);
  2483. test_d2f("0.00012345000098765", 0);
  2484. test_d2f("1234500009876.5", 0);
  2485. printf("==== double2decimal ====\n");
  2486. test_f2d(12345, 0);
  2487. test_f2d(1.0/3, 0);
  2488. test_f2d(-123.45, 0);
  2489. test_f2d(0.00012345000098765, 0);
  2490. test_f2d(1234500009876.5, 0);
  2491. printf("==== ulonglong2decimal ====\n");
  2492. test_ull2d(ULL(12345), "12345", 0);
  2493. test_ull2d(ULL(0), "0", 0);
  2494. test_ull2d(ULL(18446744073709551615), "18446744073709551615", 0);
  2495. printf("==== decimal2ulonglong ====\n");
  2496. test_d2ull("12345", "12345", 0);
  2497. test_d2ull("0", "0", 0);
  2498. test_d2ull("18446744073709551615", "18446744073709551615", 0);
  2499. test_d2ull("18446744073709551616", "18446744073", 2);
  2500. test_d2ull("-1", "0", 2);
  2501. test_d2ull("1.23", "1", 1);
  2502. test_d2ull("9999999999999999999999999.000", "9999999999999999", 2);
  2503. printf("==== longlong2decimal ====\n");
  2504. test_ll2d(LL(-12345), "-12345", 0);
  2505. test_ll2d(LL(-1), "-1", 0);
  2506. test_ll2d(LL(-9223372036854775807), "-9223372036854775807", 0);
  2507. test_ll2d(ULL(9223372036854775808), "-9223372036854775808", 0);
  2508. printf("==== decimal2longlong ====\n");
  2509. test_d2ll("18446744073709551615", "18446744073", 2);
  2510. test_d2ll("-1", "-1", 0);
  2511. test_d2ll("-1.23", "-1", 1);
  2512. test_d2ll("-9223372036854775807", "-9223372036854775807", 0);
  2513. test_d2ll("-9223372036854775808", "-9223372036854775808", 0);
  2514. test_d2ll("9223372036854775808", "9223372036854775807", 2);
  2515. printf("==== do_add ====\n");
  2516. test_da(".00012345000098765" ,"123.45", "123.45012345000098765", 0);
  2517. test_da(".1" ,".45", "0.55", 0);
  2518. test_da("1234500009876.5" ,".00012345000098765", "1234500009876.50012345000098765", 0);
  2519. test_da("9999909999999.5" ,".555", "9999910000000.055", 0);
  2520. test_da("99999999" ,"1", "100000000", 0);
  2521. test_da("989999999" ,"1", "990000000", 0);
  2522. test_da("999999999" ,"1", "1000000000", 0);
  2523. test_da("12345" ,"123.45", "12468.45", 0);
  2524. test_da("-12345" ,"-123.45", "-12468.45", 0);
  2525. test_ds("-12345" ,"123.45", "-12468.45", 0);
  2526. test_ds("12345" ,"-123.45", "12468.45", 0);
  2527. printf("==== do_sub ====\n");
  2528. test_ds(".00012345000098765", "123.45","-123.44987654999901235", 0);
  2529. test_ds("1234500009876.5", ".00012345000098765","1234500009876.49987654999901235", 0);
  2530. test_ds("9999900000000.5", ".555","9999899999999.945", 0);
  2531. test_ds("1111.5551", "1111.555","0.0001", 0);
  2532. test_ds(".555", ".555","0", 0);
  2533. test_ds("10000000", "1","9999999", 0);
  2534. test_ds("1000001000", ".1","1000000999.9", 0);
  2535. test_ds("1000000000", ".1","999999999.9", 0);
  2536. test_ds("12345", "123.45","12221.55", 0);
  2537. test_ds("-12345", "-123.45","-12221.55", 0);
  2538. test_da("-12345", "123.45","-12221.55", 0);
  2539. test_da("12345", "-123.45","12221.55", 0);
  2540. test_ds("123.45", "12345","-12221.55", 0);
  2541. test_ds("-123.45", "-12345","12221.55", 0);
  2542. test_da("123.45", "-12345","-12221.55", 0);
  2543. test_da("-123.45", "12345","12221.55", 0);
  2544. test_da("5", "-6.0","-1.0", 0);
  2545. printf("==== decimal_mul ====\n");
  2546. test_dm("12", "10","120", 0);
  2547. test_dm("-123.456", "98765.4321","-12193185.1853376", 0);
  2548. test_dm("-123456000000", "98765432100000","-12193185185337600000000000", 0);
  2549. test_dm("123456", "987654321","121931851853376", 0);
  2550. test_dm("123456", "9876543210","1219318518533760", 0);
  2551. test_dm("123", "0.01","1.23", 0);
  2552. test_dm("123", "0","0", 0);
  2553. printf("==== decimal_div ====\n");
  2554. test_dv("120", "10","12.000000000", 0);
  2555. test_dv("123", "0.01","12300.000000000", 0);
  2556. test_dv("120", "100000000000.00000","0.000000001200000000", 0);
  2557. test_dv("123", "0","", 4);
  2558. test_dv("0", "0", "", 4);
  2559. test_dv("-12193185.1853376", "98765.4321","-123.456000000000000000", 0);
  2560. test_dv("121931851853376", "987654321","123456.000000000", 0);
  2561. test_dv("0", "987","0", 0);
  2562. test_dv("1", "3","0.333333333", 0);
  2563. test_dv("1.000000000000", "3","0.333333333333333333", 0);
  2564. test_dv("1", "1","1.000000000", 0);
  2565. test_dv("0.0123456789012345678912345", "9999999999","0.000000000001234567890246913578148141", 0);
  2566. test_dv("10.333000000", "12.34500","0.837019036046982584042122316", 0);
  2567. test_dv("10.000000000060", "2","5.000000000030000000", 0);
  2568. printf("==== decimal_mod ====\n");
  2569. test_md("234","10","4", 0);
  2570. test_md("234.567","10.555","2.357", 0);
  2571. test_md("-234.567","10.555","-2.357", 0);
  2572. test_md("234.567","-10.555","2.357", 0);
  2573. c.buf[1]=0x3ABECA;
  2574. test_md("99999999999999999999999999999999999999","3","0", 0);
  2575. if (c.buf[1] != 0x3ABECA)
  2576. {
  2577. printf("%X - overflow\n", c.buf[1]);
  2578. exit(1);
  2579. }
  2580. printf("==== decimal2bin/bin2decimal ====\n");
  2581. test_d2b2d("-10.55", 4, 2,"-10.55", 0);
  2582. test_d2b2d("0.0123456789012345678912345", 30, 25,"0.0123456789012345678912345", 0);
  2583. test_d2b2d("12345", 5, 0,"12345", 0);
  2584. test_d2b2d("12345", 10, 3,"12345.000", 0);
  2585. test_d2b2d("123.45", 10, 3,"123.450", 0);
  2586. test_d2b2d("-123.45", 20, 10,"-123.4500000000", 0);
  2587. test_d2b2d(".00012345000098765", 15, 14,"0.00012345000098", 0);
  2588. test_d2b2d(".00012345000098765", 22, 20,"0.00012345000098765000", 0);
  2589. test_d2b2d(".12345000098765", 30, 20,"0.12345000098765000000", 0);
  2590. test_d2b2d("-.000000012345000098765", 30, 20,"-0.00000001234500009876", 0);
  2591. test_d2b2d("1234500009876.5", 30, 5,"1234500009876.50000", 0);
  2592. test_d2b2d("111111111.11", 10, 2,"11111111.11", 0);
  2593. test_d2b2d("000000000.01", 7, 3,"0.010", 0);
  2594. test_d2b2d("123.4", 10, 2, "123.40", 0);
  2595. printf("==== decimal_cmp ====\n");
  2596. test_dc("12","13",-1);
  2597. test_dc("13","12",1);
  2598. test_dc("-10","10",-1);
  2599. test_dc("10","-10",1);
  2600. test_dc("-12","-13",1);
  2601. test_dc("0","12",-1);
  2602. test_dc("-10","0",-1);
  2603. test_dc("4","4",0);
  2604. printf("==== decimal_round ====\n");
  2605. test_ro("5678.123451",-4,TRUNCATE,"0", 0);
  2606. test_ro("5678.123451",-3,TRUNCATE,"5000", 0);
  2607. test_ro("5678.123451",-2,TRUNCATE,"5600", 0);
  2608. test_ro("5678.123451",-1,TRUNCATE,"5670", 0);
  2609. test_ro("5678.123451",0,TRUNCATE,"5678", 0);
  2610. test_ro("5678.123451",1,TRUNCATE,"5678.1", 0);
  2611. test_ro("5678.123451",2,TRUNCATE,"5678.12", 0);
  2612. test_ro("5678.123451",3,TRUNCATE,"5678.123", 0);
  2613. test_ro("5678.123451",4,TRUNCATE,"5678.1234", 0);
  2614. test_ro("5678.123451",5,TRUNCATE,"5678.12345", 0);
  2615. test_ro("5678.123451",6,TRUNCATE,"5678.123451", 0);
  2616. test_ro("-5678.123451",-4,TRUNCATE,"0", 0);
  2617. memset(buf2, 33, sizeof(buf2));
  2618. test_ro("99999999999999999999999999999999999999",-31,TRUNCATE,"99999990000000000000000000000000000000", 0);
  2619. test_ro("15.1",0,HALF_UP,"15", 0);
  2620. test_ro("15.5",0,HALF_UP,"16", 0);
  2621. test_ro("15.9",0,HALF_UP,"16", 0);
  2622. test_ro("-15.1",0,HALF_UP,"-15", 0);
  2623. test_ro("-15.5",0,HALF_UP,"-16", 0);
  2624. test_ro("-15.9",0,HALF_UP,"-16", 0);
  2625. test_ro("15.1",1,HALF_UP,"15.1", 0);
  2626. test_ro("-15.1",1,HALF_UP,"-15.1", 0);
  2627. test_ro("15.17",1,HALF_UP,"15.2", 0);
  2628. test_ro("15.4",-1,HALF_UP,"20", 0);
  2629. test_ro("-15.4",-1,HALF_UP,"-20", 0);
  2630. test_ro("5.4",-1,HALF_UP,"10", 0);
  2631. test_ro(".999", 0, HALF_UP, "1", 0);
  2632. memset(buf2, 33, sizeof(buf2));
  2633. test_ro("999999999", -9, HALF_UP, "1000000000", 0);
  2634. test_ro("15.1",0,HALF_EVEN,"15", 0);
  2635. test_ro("15.5",0,HALF_EVEN,"16", 0);
  2636. test_ro("14.5",0,HALF_EVEN,"14", 0);
  2637. test_ro("15.9",0,HALF_EVEN,"16", 0);
  2638. test_ro("15.1",0,CEILING,"16", 0);
  2639. test_ro("-15.1",0,CEILING,"-15", 0);
  2640. test_ro("15.1",0,FLOOR,"15", 0);
  2641. test_ro("-15.1",0,FLOOR,"-16", 0);
  2642. test_ro("999999999999999999999.999", 0, CEILING,"1000000000000000000000", 0);
  2643. test_ro("-999999999999999999999.999", 0, FLOOR,"-1000000000000000000000", 0);
  2644. b.buf[0]=DIG_BASE+1;
  2645. b.buf++;
  2646. test_ro(".3", 0, HALF_UP, "0", 0);
  2647. b.buf--;
  2648. if (b.buf[0] != DIG_BASE+1)
  2649. {
  2650. printf("%d - underflow\n", b.buf[0]);
  2651. exit(1);
  2652. }
  2653. printf("==== max_decimal ====\n");
  2654. test_mx(1,1,"0.9");
  2655. test_mx(1,0,"9");
  2656. test_mx(2,1,"9.9");
  2657. test_mx(4,2,"99.99");
  2658. test_mx(6,3,"999.999");
  2659. test_mx(8,4,"9999.9999");
  2660. test_mx(10,5,"99999.99999");
  2661. test_mx(12,6,"999999.999999");
  2662. test_mx(14,7,"9999999.9999999");
  2663. test_mx(16,8,"99999999.99999999");
  2664. test_mx(18,9,"999999999.999999999");
  2665. test_mx(20,10,"9999999999.9999999999");
  2666. test_mx(20,20,"0.99999999999999999999");
  2667. test_mx(20,0,"99999999999999999999");
  2668. test_mx(40,20,"99999999999999999999.99999999999999999999");
  2669. printf("==== decimal2string ====\n");
  2670. test_pr("123.123", 0, 0, 0, "123.123", 0);
  2671. test_pr("123.123", 7, 3, '0', "123.123", 0);
  2672. test_pr("123.123", 9, 3, '0', "00123.123", 0);
  2673. test_pr("123.123", 9, 4, '0', "0123.1230", 0);
  2674. test_pr("123.123", 9, 5, '0', "123.12300", 0);
  2675. test_pr("123.123", 9, 2, '0', "000123.12", 1);
  2676. test_pr("123.123", 9, 6, '0', "23.123000", 2);
  2677. printf("==== decimal_shift ====\n");
  2678. test_sh("123.123", 1, "1231.23", 0);
  2679. test_sh("123457189.123123456789000", 1, "1234571891.23123456789", 0);
  2680. test_sh("123457189.123123456789000", 4, "1234571891231.23456789", 0);
  2681. test_sh("123457189.123123456789000", 8, "12345718912312345.6789", 0);
  2682. test_sh("123457189.123123456789000", 9, "123457189123123456.789", 0);
  2683. test_sh("123457189.123123456789000", 10, "1234571891231234567.89", 0);
  2684. test_sh("123457189.123123456789000", 17, "12345718912312345678900000", 0);
  2685. test_sh("123457189.123123456789000", 18, "123457189123123456789000000", 0);
  2686. test_sh("123457189.123123456789000", 19, "1234571891231234567890000000", 0);
  2687. test_sh("123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
  2688. test_sh("123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
  2689. test_sh("123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
  2690. test_sh("000000000000000000000000123457189.123123456789000", 26, "12345718912312345678900000000000000", 0);
  2691. test_sh("00000000123457189.123123456789000", 27, "123457189123123456789000000000000000", 0);
  2692. test_sh("00000000000000000123457189.123123456789000", 28, "1234571891231234567890000000000000000", 0);
  2693. test_sh("123", 1, "1230", 0);
  2694. test_sh("123", 10, "1230000000000", 0);
  2695. test_sh(".123", 1, "1.23", 0);
  2696. test_sh(".123", 10, "1230000000", 0);
  2697. test_sh(".123", 14, "12300000000000", 0);
  2698. test_sh("000.000", 1000, "0", 0);
  2699. test_sh("000.", 1000, "0", 0);
  2700. test_sh(".000", 1000, "0", 0);
  2701. test_sh("1", 1000, "1", 2);
  2702. test_sh("123.123", -1, "12.3123", 0);
  2703. test_sh("123987654321.123456789000", -1, "12398765432.1123456789", 0);
  2704. test_sh("123987654321.123456789000", -2, "1239876543.21123456789", 0);
  2705. test_sh("123987654321.123456789000", -3, "123987654.321123456789", 0);
  2706. test_sh("123987654321.123456789000", -8, "1239.87654321123456789", 0);
  2707. test_sh("123987654321.123456789000", -9, "123.987654321123456789", 0);
  2708. test_sh("123987654321.123456789000", -10, "12.3987654321123456789", 0);
  2709. test_sh("123987654321.123456789000", -11, "1.23987654321123456789", 0);
  2710. test_sh("123987654321.123456789000", -12, "0.123987654321123456789", 0);
  2711. test_sh("123987654321.123456789000", -13, "0.0123987654321123456789", 0);
  2712. test_sh("123987654321.123456789000", -14, "0.00123987654321123456789", 0);
  2713. test_sh("00000087654321.123456789000", -14, "0.00000087654321123456789", 0);
  2714. a.len= 2;
  2715. test_sh("123.123", -2, "1.23123", 0);
  2716. test_sh("123.123", -3, "0.123123", 0);
  2717. test_sh("123.123", -6, "0.000123123", 0);
  2718. test_sh("123.123", -7, "0.0000123123", 0);
  2719. test_sh("123.123", -15, "0.000000000000123123", 0);
  2720. test_sh("123.123", -16, "0.000000000000012312", 1);
  2721. test_sh("123.123", -17, "0.000000000000001231", 1);
  2722. test_sh("123.123", -18, "0.000000000000000123", 1);
  2723. test_sh("123.123", -19, "0.000000000000000012", 1);
  2724. test_sh("123.123", -20, "0.000000000000000001", 1);
  2725. test_sh("123.123", -21, "0", 1);
  2726. test_sh(".000000000123", -1, "0.0000000000123", 0);
  2727. test_sh(".000000000123", -6, "0.000000000000000123", 0);
  2728. test_sh(".000000000123", -7, "0.000000000000000012", 1);
  2729. test_sh(".000000000123", -8, "0.000000000000000001", 1);
  2730. test_sh(".000000000123", -9, "0", 1);
  2731. test_sh(".000000000123", 1, "0.00000000123", 0);
  2732. test_sh(".000000000123", 8, "0.0123", 0);
  2733. test_sh(".000000000123", 9, "0.123", 0);
  2734. test_sh(".000000000123", 10, "1.23", 0);
  2735. test_sh(".000000000123", 17, "12300000", 0);
  2736. test_sh(".000000000123", 18, "123000000", 0);
  2737. test_sh(".000000000123", 19, "1230000000", 0);
  2738. test_sh(".000000000123", 20, "12300000000", 0);
  2739. test_sh(".000000000123", 21, "123000000000", 0);
  2740. test_sh(".000000000123", 22, "1230000000000", 0);
  2741. test_sh(".000000000123", 23, "12300000000000", 0);
  2742. test_sh(".000000000123", 24, "123000000000000", 0);
  2743. test_sh(".000000000123", 25, "1230000000000000", 0);
  2744. test_sh(".000000000123", 26, "12300000000000000", 0);
  2745. test_sh(".000000000123", 27, "123000000000000000", 0);
  2746. test_sh(".000000000123", 28, "0.000000000123", 2);
  2747. test_sh("123456789.987654321", -1, "12345678.998765432", 1);
  2748. test_sh("123456789.987654321", -2, "1234567.899876543", 1);
  2749. test_sh("123456789.987654321", -8, "1.234567900", 1);
  2750. test_sh("123456789.987654321", -9, "0.123456789987654321", 0);
  2751. test_sh("123456789.987654321", -10, "0.012345678998765432", 1);
  2752. test_sh("123456789.987654321", -17, "0.000000001234567900", 1);
  2753. test_sh("123456789.987654321", -18, "0.000000000123456790", 1);
  2754. test_sh("123456789.987654321", -19, "0.000000000012345679", 1);
  2755. test_sh("123456789.987654321", -26, "0.000000000000000001", 1);
  2756. test_sh("123456789.987654321", -27, "0", 1);
  2757. test_sh("123456789.987654321", 1, "1234567900", 1);
  2758. test_sh("123456789.987654321", 2, "12345678999", 1);
  2759. test_sh("123456789.987654321", 4, "1234567899877", 1);
  2760. test_sh("123456789.987654321", 8, "12345678998765432", 1);
  2761. test_sh("123456789.987654321", 9, "123456789987654321", 0);
  2762. test_sh("123456789.987654321", 10, "123456789.987654321", 2);
  2763. test_sh("123456789.987654321", 0, "123456789.987654321", 0);
  2764. a.len= sizeof(buf1)/sizeof(dec1);
  2765. printf("==== decimal_actual_fraction ====\n");
  2766. test_fr("1.123456789000000000", "1.123456789");
  2767. test_fr("1.12345678000000000", "1.12345678");
  2768. test_fr("1.1234567000000000", "1.1234567");
  2769. test_fr("1.123456000000000", "1.123456");
  2770. test_fr("1.12345000000000", "1.12345");
  2771. test_fr("1.1234000000000", "1.1234");
  2772. test_fr("1.123000000000", "1.123");
  2773. test_fr("1.12000000000", "1.12");
  2774. test_fr("1.1000000000", "1.1");
  2775. test_fr("1.000000000", "1");
  2776. test_fr("1.0", "1");
  2777. test_fr("10000000000000000000.0", "10000000000000000000");
  2778. return 0;
  2779. }
  2780. #endif