//-----------------------------------------------------------------------------
// Copyright © 2006 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/ctools
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// file		decimal.h
//
// purpose	Define macros to calculate a relatively good estimate of the
//		number of decimal digits (or characters which would include a
//		sign for negative numbers, or memory space to allocate that
//		would include the string termination character) needed for the
//		largest possible number that could fit in a specified number
//		bits (a Mersenne Number).  This estimate will never be lower
//		than actually needed.  In some cases it may be slightly higher
//		for even the largest number of that bit size.  It certainly
//		will be larger if the actual value is much lower.
//
// The following is a list of integer ratios (that approach log10(2) while
// always being larger and never smaller) which can be used to calculate a
// minimum number of decimal digits needed for the kargest value in a given
// number of bits.
//                        --- 32 bit arch --  --- 64 bit arch --
// multiplier    divisor  supported max bits  supported max bits
// ----------   --------  ------------------  ------------------
//         87 /      289            49367440  212031541077121280
//        146 /      485            29417584  126347562148695552
//        789 /     2621             5443558   23379903768960140
//       1432 /     4757             2999278   12881804520746894
//       2075 /     6893             2069863    8889997143956410
//       2718 /     9029             1580194    6786881557656200
//       3361 /    11165             1277883    5488468929993916
//       4004 /    13301             1072669    4607078939487900
//      12655 /    42039              339388    1457664486267053
//      33961 /   112816              126467     543174349215558
//      55267 /   183593               77713     333775020784728 <<== small
//      76573 /   254370               56089     240904027185947
//     174452 /   579517               24619     105741086795850
//     272331 /   904664               15771      67736482712984
//     370210 /  1229811               11601      49827784429674
//     468089 /  1554958                9175      39408625440268
//     565968 /  1880105                7588      32593263353599
//     663847 /  2205252                6469      27787643950654
//     761726 /  2530399                5638      24217033518233
//     859605 /  2855546                4996      21459558836570
//     957484 /  3180693                4485      19265850994596
//    1055363 /  3505840                4069      17479051353619
//    1153242 /  3830987                3724      15995553469011
//    1251121 /  4156134                3432      14744172684904
//    1349000 /  4481281                3183      13674384042780
//    1446879 /  4806428                2968      12749334307644
//    1544758 /  5131575                2780      11941510627367
//    1642637 /  5456722                2614      11229957728768
//    1740516 /  5781869                2467      10598434069959
//    1838395 /  6107016                2336      10034157008537
//    3774669 / 12539179                1137       4886983222558 <<== big
//-----------------------------------------------------------------------------

__DEFINE_BEGIN__

//-----------------------------------------------------------------------------
// macros	decimal_max_signed_bits
//		decimal_max_signed_bits_big
//
// purpose	Yield the maximum number of bits that are supported by the
//		macros that calculate the number of digits, characters, or
//		memory requirements.
//-----------------------------------------------------------------------------
#define decimal_max_signed_bits		(uint_max(unsigned long)/55267UL)
#define decimal_max_signed_bits_big	(uint_max(unsigned long long)/3774669ULL)

//-----------------------------------------------------------------------------
// macros	decimal_digits_for_*
//
// purpose	Given a number of bits, calculate the number of decimal digits
//		that the largest possible number (known as a Mersenne Number)
//		that can be stored in that many bits, would have.
//
// limitation	In a 32 bit architecture, these macros will calculate the
//		number of decimal digits exactly for integers with up to 77713
//		bits for unsigned or 77714 bits for signed.  If the number of
//		bits could possibly exceed this limit, use the macros ending
//		with _big instead, which force 64 bit calculation, support up
//		to 4886983222558 bits for unsigned or 4886983222559 bits for
//		signed, and have fewer overestimates.
//
//		In a 64 bit architecture, these macros will calculate the
//		number of decimal digits exactly for integers with up to
//		333775020784728 bits for unsigned or 333775020784729 for
//		signed.  The macros ending with _big will also work with the
//		same limitations as in a 32 bit architecture.
//
// note		You would need much more than 32 terabytes of RAM to be able
//		to exceed the limitations of the _big versions.
//-----------------------------------------------------------------------------
#define decimal_digits_for_type(t)						\
	((((sizeof((t))*(CHAR_BIT))-int_is_signed((t)))*55267UL)/183593UL)+1UL)

#define decimal_digits_for_signed_bits(b)					\
	((((((unsigned long)(b))-1UL)*55267UL)/183593UL)+1UL)
#define decimal_digits_for_unsigned_bits(b)					\
	(((((unsigned long)(b))*55267UL)/183593UL)+1UL)

#define decimal_digits_for_signed_bits_big(b)					\
	((((((unsigned long long)(b))-1ULL)*3774669ULL)/12539179ULL)+1ULL)
#define decimal_digits_for_unsigned_bits_big(b)					\
	(((((unsigned long long)(b))*3774669ULL)/12539179ULL)+1ULL)

#define decimal_digits_for_signed_char						\
	((((CHAR_BIT)-1U)*55267UL)/183593UL)+1U)
#define decimal_digits_for_unsigned_char					\
	((((CHAR_BIT)*55267UL)/183593UL)+1U)
#define decimal_digits_for_signed_short						\
	((((sizeof(signed short)*(CHAR_BIT))-1U)*55267UL)/183593UL)+1U)
#define decimal_digits_for_unsigned_short					\
	((((sizeof(unsigned short)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_digits_for_signed_int						\
	((((sizeof(signed int)*(CHAR_BIT))-1U)*55267UL)/183593UL)+1U)
#define decimal_digits_for_unsigned_int						\
	((((sizeof(unsigned int)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_digits_for_signed_long						\
	((((sizeof(signed long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+1U)
#define decimal_digits_for_unsigned_long					\
	((((sizeof(unsigned long)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_digits_for_signed_long_long					\
	((((sizeof(signed long long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+1U)
#define decimal_digits_for_unsigned_long_long					\
	((((sizeof(unsigned long long)*(CHAR_BIT))*55267UL)/183593UL)+1U)

//-----------------------------------------------------------------------------
// macros	decimal_chars_for_*
//
// purpose	Given a number of bits, calculate the number of characters,
//		accounting for a possible sign character for signed types,
//		that the largest possible number (known as a Mersenne Number),
//		or also smallest possible number for signed types, that can be
//		stored in that many bits, would have.
//
// limitation	In a 32 bit architecture, these macros will calculate the
//		number of characters exactly for integers with up to 77713
//		bits for unsigned or 77714 bits for signed.  If the number of
//		bits could possibly exceed this limit, use the macros ending
//		with _big instead, which force 64 bit calculation, support up
//		to 4886983222558 bits for unsigned or 4886983222559 bits for
//		signed, and have fewer overestimates.
//
//		In a 64 bit architecture, these macros will calculate the
//		number of characters exactly for integers with up to
//		333775020784728 bits for unsigned or 333775020784729 for
//		signed.  The macros ending with _big will also work with the
//		same limitations as in a 32 bit architecture.
//
// note		You would need much more than 32 terabytes of RAM to be able
//		to exceed the limitations of the _big versions.
//-----------------------------------------------------------------------------
#define decimal_chars_for_type(t)						\
	((((sizeof((t))*(CHAR_BIT))-int_is_signed((t)))*55267UL)/183593UL)+int_is_signed((t))+1UL)

#define decimal_chars_for_signed_bits(b)					\
	((((((unsigned long)(b))-1UL)*55267UL)/183593UL)+2UL)
#define decimal_chars_for_unsigned_bits(b)					\
	(((((unsigned long)(b))*55267UL)/183593UL)+1UL)

#define decimal_chars_for_signed_bits_big(b)					\
	((((((unsigned long long)(b))-1ULL)*3774669ULL)/12539179ULL)+2ULL)
#define decimal_chars_for_unsigned_bits_big(b)					\
	(((((unsigned long long)(b))*3774669ULL)/12539179ULL)+1ULL)

#define decimal_chars_for_signed_char						\
	((((CHAR_BIT)-1U)*55267UL)/183593UL)+2U)
#define decimal_chars_for_unsigned_char						\
	((((CHAR_BIT)*55267UL)/183593UL)+1U)
#define decimal_chars_for_signed_short						\
	((((sizeof(signed short)*(CHAR_BIT))-1U)*55267UL)/183593UL)+2U)
#define decimal_chars_for_unsigned_short					\
	((((sizeof(unsigned short)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_chars_for_signed_int						\
	((((sizeof(signed int)*(CHAR_BIT))-1U)*55267UL)/183593UL)+2U)
#define decimal_chars_for_unsigned_int						\
	((((sizeof(unsigned int)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_chars_for_signed_long						\
	((((sizeof(signed long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+2U)
#define decimal_chars_for_unsigned_long						\
	((((sizeof(unsigned long)*(CHAR_BIT))*55267UL)/183593UL)+1U)
#define decimal_chars_for_signed_long_long					\
	((((sizeof(signed long long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+2U)
#define decimal_chars_for_unsigned_long_long					\
	((((sizeof(unsigned long long)*(CHAR_BIT))*55267UL)/183593UL)+1U)

//-----------------------------------------------------------------------------
// macros	decimal_space_for_*
//
// purpose	Given a number of bits, calculate the amount of memory space,
//		accounting for a possible sign character for signed types and
//		the null terminating character, that the largest possible
//		number (known as a Mersenne Number), or also smallest possible
//		number for signed types, that can be stored in that many bits,
//		would have.
//
// limitation	In a 32 bit architecture, these macros will calculate the
//		number of memory spaces exactly for integers with up to 77713
//		bits for unsigned or 77714 bits for signed.  If the number of
//		bits could possibly exceed this limit, use the macros ending
//		with _big instead, which force 64 bit calculation, support up
//		to 4886983222558 bits for unsigned or 4886983222559 bits for
//		signed, and have fewer overestimates.
//
//		In a 64 bit architecture, these macros will calculate the
//		number of memory spaces exactly for integers with up to
//		333775020784728 bits for unsigned or 333775020784729 for
//		signed.  The macros ending with _big will also work with the
//		same limitations as in a 32 bit architecture.
//
// note		You would need much more than 32 terabytes of RAM to be able
//		to exceed the limitations of the _big versions.
//-----------------------------------------------------------------------------
#define decimal_space_for_type(t)						\
	((((sizeof((t))*(CHAR_BIT))-int_is_signed((t)))*55267UL)/183593UL)+int_is_signed((t))+2UL)

#define decimal_space_for_signed_bits(b)					\
	((((((unsigned long)(b))-1UL)*55267UL)/183593UL)+3UL)
#define decimal_space_for_unsigned_bits(b)					\
	(((((unsigned long)(b))*55267UL)/183593UL)+2UL)

#define decimal_space_for_signed_bits_big(b)					\
	((((((unsigned long long)(b))-1ULL)*3774669ULL)/12539179ULL)+3ULL)
#define decimal_space_for_unsigned_bits_big(b)					\
	(((((unsigned long long)(b))*3774669ULL)/12539179ULL)+2ULL)

#define decimal_space_for_signed_char						\
	((((CHAR_BIT)-1U)*55267UL)/183593UL)+3U)
#define decimal_space_for_unsigned_char						\
	((((CHAR_BIT)*55267UL)/183593UL)+2U)
#define decimal_space_for_signed_short						\
	((((sizeof(signed short)*(CHAR_BIT))-1U)*55267UL)/183593UL)+3U)
#define decimal_space_for_unsigned_short					\
	((((sizeof(unsigned short)*(CHAR_BIT))*55267UL)/183593UL)+2U)
#define decimal_space_for_signed_int						\
	((((sizeof(signed int)*(CHAR_BIT))-1U)*55267UL)/183593UL)+3U)
#define decimal_space_for_unsigned_int						\
	((((sizeof(unsigned int)*(CHAR_BIT))*55267UL)/183593UL)+2U)
#define decimal_space_for_signed_long						\
	((((sizeof(signed long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+3U)
#define decimal_space_for_unsigned_long						\
	((((sizeof(unsigned long)*(CHAR_BIT))*55267UL)/183593UL)+2U)
#define decimal_space_for_signed_long_long					\
	((((sizeof(signed long long)*(CHAR_BIT))-1U)*55267UL)/183593UL)+3U)
#define decimal_space_for_unsigned_long_long					\
	((((sizeof(unsigned long long)*(CHAR_BIT))*55267UL)/183593UL)+2U)

__DEFINE_END__

