8#ifndef INCLUDED_BDLDFP_DECIMALCONVERTUTIL
9#define INCLUDED_BDLDFP_DECIMALCONVERTUTIL
364#include <bdlscm_version.h>
380#include <bsl_cstring.h>
382#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
383#include <bsl_c_signal.h>
399#if defined(BDLDFP_DECIMALPLATFORM_INTELDFP)
400 typedef DecimalConvertUtil_IntelDfp Imp;
402 BDLDFP_DECIMALPLATFORM_COMPILER_ERROR;
409 static const int k_EXPONENT_SHIFT_SMALL64 = 53;
410 static const int k_EXPONENT_MASK64 = 0x3ff;
411 static const unsigned long long k_SPECIAL_ENCODING_MASK64 =
412 0x6000000000000000ull;
413 static const unsigned long long k_SMALL_COEFF_MASK64 =
414 0x001fffffffffffffull;
427 static int decimal64ToUnpackedSpecial(
bool *isNegative,
623 const unsigned char *buffer);
625 const unsigned char *buffer);
632 const unsigned char *buffer);
635 const unsigned char *buffer);
637 const unsigned char *buffer);
645 const unsigned char *buffer);
678 const unsigned char *buffer);
680 const unsigned char *buffer);
687 const unsigned char *buffer);
690 const unsigned char *buffer);
692 const unsigned char *buffer);
700 const unsigned char *buffer);
731 const unsigned char *buffer);
734 const unsigned char *buffer);
737 const unsigned char *buffer);
751 const unsigned char *buffer);
754 const unsigned char *buffer);
757 const unsigned char *buffer);
765 unsigned char *buffer,
777 unsigned char *buffer,
789 const unsigned char *buffer,
803 const unsigned char *buffer,
815 const unsigned char *buffer,
824 unsigned char *buffer,
835 const unsigned char *buffer);
846int DecimalConvertUtil::decimal64ToUnpackedSpecial(
852#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
863 (bidValue & k_SPECIAL_ENCODING_MASK64) == k_SPECIAL_ENCODING_MASK64)) {
869 *isNegative = (bidValue & 0x8000000000000000ull) ? 1 : 0;
871 *biasedExponent =
static_cast<int>(
872 (bidValue >> k_EXPONENT_SHIFT_SMALL64) & k_EXPONENT_MASK64);
874 *mantissa = bidValue & k_SMALL_COEFF_MASK64;
880Decimal64 DecimalConvertUtil::decimal64FromUnpackedSpecial(
885#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
887 result.
data()->d_raw = (isNegative ? 0x8000000000000000ull : 0) |
888 (
static_cast<BID_UINT64
>(exponent + 398)
889 << k_EXPONENT_SHIFT_SMALL64) |
895 -
static_cast<long long>(mantissa),
904Decimal64 DecimalConvertUtil::decimal64FromUnpackedSpecial(
int mantissa,
907#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
909 result.
data()->d_raw = (
static_cast<BID_UINT64
>(exponent + 398)
910 << k_EXPONENT_SHIFT_SMALL64) |
921 unsigned char *buffer,
934 bsl::memcpy(
reinterpret_cast<unsigned char *
>(&encoded),
941 reinterpret_cast<unsigned char*
>(&encoded),
949 unsigned char *buffer,
959 decimal64ToUnpackedSpecial(&isNegative,
964 if (395 <= exponent && exponent < 399) {
965 if (mantissa < (1u << 14)) {
966 unsigned short squished =
static_cast<unsigned short>(
967 mantissa | (exponent - 395) << 14);
970 bsl::memcpy(buffer, &squishedN, 2);
974 if (392 <= exponent && exponent < 400) {
975 if (mantissa < (1u << 21)) {
980 unsigned int squished =
static_cast<unsigned int>(
981 (mantissa << 8) | (exponent - 392) << 29);
985 reinterpret_cast<unsigned char*
>(&squishedN),
992 if (382 <= exponent && exponent < 414) {
993 if (mantissa < (1u << 26)) {
994 unsigned int squished =
static_cast<unsigned int>(
995 mantissa | (exponent - 382) << 26);
997 squished |= 1u << 31;
1000 bsl::memcpy(buffer, &squishedN, 4);
1003 if (mantissa < (1ull << 34)) {
1009 squished |= 1ull << 63;
1014 reinterpret_cast<unsigned char*
>(&squishedN),
1027 return (size > 0 && size <= 5) || size == 8;
1032 const unsigned char *buffer,
1045 bsl::memcpy(&encoded, buffer, 8);
1049 bsl::memcpy(&decimal, &encoded,
sizeof(decimal));
1058 const unsigned char *buffer,
1074 const unsigned char *buffer,
1082 int exponent = (buffer[0] >> 6) - 3;
1083 int mantissa =
static_cast<int>(((buffer[0] & 0x3F) << 8) |
1084 static_cast<int>(buffer[1]));
1086 return decimal64FromUnpackedSpecial(mantissa, exponent);
1089 int exponent = (buffer[0] >> 5) - 6;
1090 int mantissa =
static_cast<int>(((buffer[0] & 0x1F) << 16) |
1091 static_cast<int>(buffer[1]) << 8 |
1092 static_cast<int>(buffer[2]));
1093 return decimal64FromUnpackedSpecial(mantissa, exponent);
1096 bool isNegative = buffer[0] >> 7;
1097 int exponent = ((buffer[0] & 0x7F) >> 2) - 16;
1098 int mantissa =
static_cast<int>(((buffer[0] & 0x03) << 24) |
1099 static_cast<int>(buffer[1]) << 16 |
1100 static_cast<int>(buffer[2]) << 8 |
1101 static_cast<int>(buffer[3]));
1102 return decimal64FromUnpackedSpecial(isNegative, mantissa, exponent);
1106 int exponent = (buffer[0] >> 7) - 2;
1107 int mantissa =
static_cast<int>(buffer[0] & 0x7F);
1108 return decimal64FromUnpackedSpecial(mantissa, exponent);
1110#ifdef BSLS_PLATFORM_CMP_IBM
1119 bool isNegative = buffer[0] >> 7;
1120 int exponent = ((buffer[0] & 0x7F) >> 2) - 16;
1127 return decimal64FromUnpackedSpecial(isNegative, mantissa, exponent);
1132#ifdef BSLS_PLATFORM_CMP_IBM
1141 unsigned char *buffer,
1151 decimal64ToUnpackedSpecial(&isNegative,
1156 if (396 <= exponent && exponent < 400) {
1157 if (mantissa < (1u << 13)) {
1161 unsigned short squished =
static_cast<unsigned short>(
1162 mantissa | (exponent - 396) << 13);
1165 bsl::memcpy(buffer, &squishedN, 2);
1170 if (394 <= exponent && exponent < 402) {
1171 if (mantissa < (1u << 19)) {
1176 unsigned int squished =
static_cast<unsigned int>(
1177 (mantissa << 8) | (exponent - 394) << 27);
1181 squished |= 1u << 31;
1186 reinterpret_cast<unsigned char*
>(&squishedN),
1197 if (382 <= exponent &&
1198 (exponent < 413 || (!isNegative && exponent == 413))) {
1199 if (mantissa < (1u << 24)) {
1200 unsigned int squished =
static_cast<unsigned int>(
1201 mantissa | (exponent - 382) << 24);
1203 squished |= 1u << 29;
1207 squished |= 3u << 30;
1209 bsl::memcpy(buffer, &squishedN, 4);
1218 bsl::memcpy(
reinterpret_cast<unsigned char *
>(&encoded),
1224 bsl::memcpy(buffer,
reinterpret_cast<unsigned char*
>(&encoded), 8);
1232 const unsigned char *buffer)
1234 if (!(*buffer & 0x80)) {
1238 int exponent = (buffer[0] >> 5) - 2;
1239 int mantissa =
static_cast<int>(((buffer[0] & 0x1F) << 8) |
1240 static_cast<int>(buffer[1]));
1242 *decimal = decimal64FromUnpackedSpecial(mantissa, exponent);
1245 else if ((*buffer & 0xC0) == 0x80) {
1249 unsigned char eByte1 = buffer[0] & 0x3F;
1251 int exponent = (eByte1 >> 3) - 4;
1252 int mantissa =
static_cast<int>(((eByte1 & 0x07) << 16) |
1253 static_cast<int>(buffer[1] << 8) |
1254 static_cast<int>(buffer[2]));
1255 *decimal = decimal64FromUnpackedSpecial(mantissa, exponent);
1258 else if (*buffer == 0xFF) {
1264 bsl::memcpy(&encoded, buffer, 8);
1267 bsl::memcpy(decimal,
1268 reinterpret_cast<unsigned char *
>(&encoded),
1277 unsigned char eByte1 = buffer[0] & 0x3F;
1278 bool isNegative = eByte1 >> 5;
1279 int exponent = (eByte1 & 0x1F) - 16;
1281 static_cast<int>(
static_cast<int>(buffer[1] << 16) |
1282 static_cast<int>(buffer[2] << 8) |
1283 static_cast<int>(buffer[3]));
1285 *decimal = decimal64FromUnpackedSpecial(isNegative,
1297 return Imp::decimalToDouble(decimal);
1303 return Imp::decimalToDouble(decimal);
1309 return Imp::decimalToDouble(decimal);
1315 return Imp::decimalToDouble(decimal);
1321 return Imp::decimalToDouble(decimal);
1327 return Imp::decimalToDouble(decimal);
1335 return Imp::decimalToFloat(decimal);
1341 return Imp::decimalToFloat(decimal);
1347 return Imp::decimalToFloat(decimal);
1353 return Imp::decimalToFloat(decimal);
1359 return Imp::decimalToFloat(decimal);
1365 return Imp::decimalToFloat(decimal);
1375 Imp::decimalToDPD(buffer, decimal);
1382 Imp::decimalToDPD(buffer, decimal);
1389 Imp::decimalToDPD(buffer, decimal);
1396 Imp::decimalToDPD(buffer, decimal);
1403 Imp::decimalToDPD(buffer, decimal);
1410 Imp::decimalToDPD(buffer, decimal);
1419 return Imp::decimal32FromDPD(buffer);
1425 const unsigned char *buffer)
1427 *decimal = Imp::decimal32FromDPD(buffer);
1434 return Imp::decimal64FromDPD(buffer);
1440 const unsigned char *buffer)
1442 *decimal = Imp::decimal64FromDPD(buffer);
1449 return Imp::decimal128FromDPD(buffer);
1455 const unsigned char *buffer)
1457 *decimal = Imp::decimal128FromDPD(buffer);
1463 const unsigned char *buffer)
1465 Imp::decimalFromDPD(decimal, buffer);
1471 const unsigned char *buffer)
1473 Imp::decimalFromDPD(decimal, buffer);
1479 const unsigned char *buffer)
1481 Imp::decimalFromDPD(decimal, buffer);
1491 Imp::decimalToBID(buffer, decimal);
1498 Imp::decimalToBID(buffer, decimal);
1505 Imp::decimalToBID(buffer, decimal);
1512 Imp::decimalToBID(buffer, decimal);
1519 Imp::decimalToBID(buffer, decimal);
1526 Imp::decimalToBID(buffer, decimal);
1535 return Imp::decimal32FromBID(buffer);
1541 const unsigned char *buffer)
1543 *decimal = Imp::decimal32FromBID(buffer);
1550 return Imp::decimal64FromBID(buffer);
1556 const unsigned char *buffer)
1558 *decimal = Imp::decimal64FromBID(buffer);
1565 return Imp::decimal128FromBID(buffer);
1571 const unsigned char *buffer)
1573 *decimal = Imp::decimal128FromBID(buffer);
1579 const unsigned char *buffer)
1581 Imp::decimalFromBID(decimal, buffer);
1587 const unsigned char *buffer)
1589 Imp::decimalFromBID(decimal, buffer);
1595 const unsigned char *buffer)
1597 Imp::decimalFromBID(decimal, buffer);
Imp::ValueType64 ValueType64
Definition bdldfp_decimalimputil.h:250
static ValueType64 makeDecimalRaw64(unsigned long long int significand, int exponent)
Definition bdldfp_decimal.h:3023
Definition bdldfp_decimal.h:730
Definition bdldfp_decimal.h:1834
DecimalImpUtil::ValueType64 * data()
Return a modifiable pointer to the underlying implementation.
Definition bdldfp_decimal.h:6063
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_BYTEORDER_HTONS(x)
Definition bsls_byteorder.h:272
#define BSLS_BYTEORDER_HTONL(x)
Definition bsls_byteorder.h:274
#define BSLS_BYTEORDER_NTOHLL(x)
Definition bsls_byteorder.h:267
#define BSLS_BYTEORDER_HTONLL(x)
Definition bsls_byteorder.h:276
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdldfp_decimal.h:712
Decimal_Type64 Decimal64
Definition bdldfp_decimal.h:715
Definition bdldfp_decimalconvertutil.h:395
static const unsigned char * decimal128FromNetwork(Decimal128 *decimal, const unsigned char *buffer)
static Decimal128 decimal128FromFloat(float binary, int digits=0)
static Decimal64 decimal64FromMultiWidthEncoding(const unsigned char *buffer, bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1031
static float decimalToFloat(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1351
static const unsigned char * decimal64FromVariableWidthEncoding(bdldfp::Decimal64 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1230
static void decimalToDPD(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1393
static Decimal32 decimal32FromBID(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1533
static Decimal64 decimal64FromMultiWidthEncodingRaw(const unsigned char *buffer, bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1073
static double decimalToDouble(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1313
static unsigned char * decimalToNetwork(unsigned char *buffer, Decimal64 decimal)
static bsls::Types::size_type decimal64ToMultiWidthEncodingRaw(unsigned char *buffer, bdldfp::Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:948
static void decimal64ToBID(unsigned char *buffer, Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1495
static void decimal128ToBID(unsigned char *buffer, Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1502
static int decimal64FromMultiWidthEncodingIfValid(Decimal64 *decimal, const unsigned char *buffer, bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1056
static void decimalFromBID(Decimal32 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1578
static Decimal32 decimal32FromDouble(double binary, int digits=0)
static unsigned char * decimalToNetwork(unsigned char *buffer, Decimal32 decimal)
static const unsigned char * decimal64FromNetwork(Decimal64 *decimal, const unsigned char *buffer)
static Decimal128 decimal128FromDouble(double binary, int digits=0)
static const unsigned char * decimal32FromNetwork(Decimal32 *decimal, const unsigned char *buffer)
static Decimal128 decimal128FromBID(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1563
static unsigned char * decimal32ToNetwork(unsigned char *buffer, Decimal32 decimal)
static Decimal64 decimal64FromFloat(float binary, int digits=0)
static bsls::Types::size_type decimal64ToMultiWidthEncoding(unsigned char *buffer, bdldfp::Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:920
static unsigned char * decimalToNetwork(unsigned char *buffer, Decimal128 decimal)
static void decimal64ToDPD(unsigned char *buffer, Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1379
static Decimal64 decimal64FromDouble(double binary, int digits=0)
static void decimal128ToDPD(unsigned char *buffer, Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1386
static float decimal128ToFloat(Decimal128 decimal)
[DEPRECATED] Use deciamalToFloat instead.
Definition bdldfp_decimalconvertutil.h:1345
static Decimal64 decimal64FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1432
static void decimal32ToDPD(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1372
static double decimal32ToDouble(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1295
static const unsigned char * decimalFromNetwork(Decimal32 *decimal, const unsigned char *buffer)
static float decimal32ToFloat(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1333
static const unsigned char * decimalFromNetwork(Decimal128 *decimal, const unsigned char *buffer)
static double decimal64ToDouble(Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1301
static Decimal32 decimal32FromFloat(float binary, int digits=0)
static void decimalFromDPD(Decimal32 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1462
static unsigned char * decimal64ToNetwork(unsigned char *buffer, Decimal64 decimal)
static unsigned char * decimal64ToVariableWidthEncoding(unsigned char *buffer, bdldfp::Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1140
static unsigned char * decimal128ToNetwork(unsigned char *buffer, Decimal128 decimal)
static float decimal64ToFloat(Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1339
static Decimal64 decimal64FromBID(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1548
static Decimal128 decimal128FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1447
static const unsigned char * decimalFromNetwork(Decimal64 *decimal, const unsigned char *buffer)
static void decimal32ToBID(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1488
static bool isValidMultiWidthSize(bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1025
static Decimal32 decimal32FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1417
static void decimalToBID(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1509
static double decimal128ToDouble(Decimal128 decimal)
[DEPRECATED] Use deciamalToDouble instead.
Definition bdldfp_decimalconvertutil.h:1307
static Decimal64 makeDecimalRaw64(int significand, int exponent)
Definition bdldfp_decimalutil.h:839
std::size_t size_type
Definition bsls_types.h:124
unsigned long long Uint64
Definition bsls_types.h:137