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>
401#if defined(BDLDFP_DECIMALPLATFORM_INTELDFP)
402 typedef DecimalConvertUtil_IntelDfp Imp;
404 BDLDFP_DECIMALPLATFORM_COMPILER_ERROR;
411 static const int k_EXPONENT_SHIFT_SMALL64 = 53;
412 static const int k_EXPONENT_MASK64 = 0x3ff;
413 static const unsigned long long k_SPECIAL_ENCODING_MASK64 =
414 0x6000000000000000ull;
415 static const unsigned long long k_SMALL_COEFF_MASK64 =
416 0x001fffffffffffffull;
430 static int decimal64ToUnpackedSpecial(
bool *isNegative,
642 const unsigned char *buffer);
644 const unsigned char *buffer);
646 const unsigned char *buffer);
656 const unsigned char *buffer);
658 const unsigned char *buffer);
660 const unsigned char *buffer);
701 const unsigned char *buffer);
703 const unsigned char *buffer);
705 const unsigned char *buffer);
715 const unsigned char *buffer);
717 const unsigned char *buffer);
719 const unsigned char *buffer);
752 const unsigned char *buffer);
755 const unsigned char *buffer);
758 const unsigned char *buffer);
774 const unsigned char *buffer);
777 const unsigned char *buffer);
780 const unsigned char *buffer);
789 unsigned char *buffer,
803 unsigned char *buffer,
817 const unsigned char *buffer,
833 const unsigned char *buffer,
847 const unsigned char *buffer,
857 unsigned char *buffer,
869 const unsigned char *buffer);
880int DecimalConvertUtil::decimal64ToUnpackedSpecial(
886#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
897 (bidValue & k_SPECIAL_ENCODING_MASK64) == k_SPECIAL_ENCODING_MASK64)) {
903 *isNegative = (bidValue & 0x8000000000000000ull) ? 1 : 0;
905 *biasedExponent =
static_cast<int>(
906 (bidValue >> k_EXPONENT_SHIFT_SMALL64) & k_EXPONENT_MASK64);
908 *mantissa = bidValue & k_SMALL_COEFF_MASK64;
914Decimal64 DecimalConvertUtil::decimal64FromUnpackedSpecial(
919#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
921 result.
data()->d_raw = (isNegative ? 0x8000000000000000ull : 0) |
922 (
static_cast<BID_UINT64
>(exponent + 398)
923 << k_EXPONENT_SHIFT_SMALL64) |
929 -
static_cast<long long>(mantissa),
938Decimal64 DecimalConvertUtil::decimal64FromUnpackedSpecial(
int mantissa,
941#ifdef BDLDFP_DECIMALPLATFORM_INTELDFP
943 result.
data()->d_raw = (
static_cast<BID_UINT64
>(exponent + 398)
944 << k_EXPONENT_SHIFT_SMALL64) |
955 unsigned char *buffer,
968 bsl::memcpy(
reinterpret_cast<unsigned char *
>(&encoded),
975 reinterpret_cast<unsigned char*
>(&encoded),
983 unsigned char *buffer,
993 decimal64ToUnpackedSpecial(&isNegative,
998 if (395 <= exponent && exponent < 399) {
999 if (mantissa < (1u << 14)) {
1000 unsigned short squished =
static_cast<unsigned short>(
1001 mantissa | (exponent - 395) << 14);
1004 bsl::memcpy(buffer, &squishedN, 2);
1008 if (392 <= exponent && exponent < 400) {
1009 if (mantissa < (1u << 21)) {
1014 unsigned int squished =
static_cast<unsigned int>(
1015 (mantissa << 8) | (exponent - 392) << 29);
1019 reinterpret_cast<unsigned char*
>(&squishedN),
1026 if (382 <= exponent && exponent < 414) {
1027 if (mantissa < (1u << 26)) {
1028 unsigned int squished =
static_cast<unsigned int>(
1029 mantissa | (exponent - 382) << 26);
1031 squished |= 1u << 31;
1034 bsl::memcpy(buffer, &squishedN, 4);
1037 if (mantissa < (1ull << 34)) {
1043 squished |= 1ull << 63;
1048 reinterpret_cast<unsigned char*
>(&squishedN),
1061 return (size > 0 && size <= 5) || size == 8;
1066 const unsigned char *buffer,
1079 bsl::memcpy(&encoded, buffer, 8);
1083 bsl::memcpy(&decimal, &encoded,
sizeof(decimal));
1092 const unsigned char *buffer,
1108 const unsigned char *buffer,
1116 int exponent = (buffer[0] >> 6) - 3;
1117 int mantissa =
static_cast<int>(((buffer[0] & 0x3F) << 8) |
1118 static_cast<int>(buffer[1]));
1120 return decimal64FromUnpackedSpecial(mantissa, exponent);
1123 int exponent = (buffer[0] >> 5) - 6;
1124 int mantissa =
static_cast<int>(((buffer[0] & 0x1F) << 16) |
1125 static_cast<int>(buffer[1]) << 8 |
1126 static_cast<int>(buffer[2]));
1127 return decimal64FromUnpackedSpecial(mantissa, exponent);
1130 bool isNegative = buffer[0] >> 7;
1131 int exponent = ((buffer[0] & 0x7F) >> 2) - 16;
1132 int mantissa =
static_cast<int>(((buffer[0] & 0x03) << 24) |
1133 static_cast<int>(buffer[1]) << 16 |
1134 static_cast<int>(buffer[2]) << 8 |
1135 static_cast<int>(buffer[3]));
1136 return decimal64FromUnpackedSpecial(isNegative, mantissa, exponent);
1140 int exponent = (buffer[0] >> 7) - 2;
1141 int mantissa =
static_cast<int>(buffer[0] & 0x7F);
1142 return decimal64FromUnpackedSpecial(mantissa, exponent);
1144#ifdef BSLS_PLATFORM_CMP_IBM
1153 bool isNegative = buffer[0] >> 7;
1154 int exponent = ((buffer[0] & 0x7F) >> 2) - 16;
1161 return decimal64FromUnpackedSpecial(isNegative, mantissa, exponent);
1166#ifdef BSLS_PLATFORM_CMP_IBM
1175 unsigned char *buffer,
1185 decimal64ToUnpackedSpecial(&isNegative,
1190 if (396 <= exponent && exponent < 400) {
1191 if (mantissa < (1u << 13)) {
1195 unsigned short squished =
static_cast<unsigned short>(
1196 mantissa | (exponent - 396) << 13);
1199 bsl::memcpy(buffer, &squishedN, 2);
1204 if (394 <= exponent && exponent < 402) {
1205 if (mantissa < (1u << 19)) {
1210 unsigned int squished =
static_cast<unsigned int>(
1211 (mantissa << 8) | (exponent - 394) << 27);
1215 squished |= 1u << 31;
1220 reinterpret_cast<unsigned char*
>(&squishedN),
1231 if (382 <= exponent &&
1232 (exponent < 413 || (!isNegative && exponent == 413))) {
1233 if (mantissa < (1u << 24)) {
1234 unsigned int squished =
static_cast<unsigned int>(
1235 mantissa | (exponent - 382) << 24);
1237 squished |= 1u << 29;
1241 squished |= 3u << 30;
1243 bsl::memcpy(buffer, &squishedN, 4);
1252 bsl::memcpy(
reinterpret_cast<unsigned char *
>(&encoded),
1258 bsl::memcpy(buffer,
reinterpret_cast<unsigned char*
>(&encoded), 8);
1266 const unsigned char *buffer)
1268 if (!(*buffer & 0x80)) {
1272 int exponent = (buffer[0] >> 5) - 2;
1273 int mantissa =
static_cast<int>(((buffer[0] & 0x1F) << 8) |
1274 static_cast<int>(buffer[1]));
1276 *decimal = decimal64FromUnpackedSpecial(mantissa, exponent);
1279 else if ((*buffer & 0xC0) == 0x80) {
1283 unsigned char eByte1 = buffer[0] & 0x3F;
1285 int exponent = (eByte1 >> 3) - 4;
1286 int mantissa =
static_cast<int>(((eByte1 & 0x07) << 16) |
1287 static_cast<int>(buffer[1] << 8) |
1288 static_cast<int>(buffer[2]));
1289 *decimal = decimal64FromUnpackedSpecial(mantissa, exponent);
1292 else if (*buffer == 0xFF) {
1298 bsl::memcpy(&encoded, buffer, 8);
1301 bsl::memcpy(decimal,
1302 reinterpret_cast<unsigned char *
>(&encoded),
1311 unsigned char eByte1 = buffer[0] & 0x3F;
1312 bool isNegative = eByte1 >> 5;
1313 int exponent = (eByte1 & 0x1F) - 16;
1315 static_cast<int>(
static_cast<int>(buffer[1] << 16) |
1316 static_cast<int>(buffer[2] << 8) |
1317 static_cast<int>(buffer[3]));
1319 *decimal = decimal64FromUnpackedSpecial(isNegative,
1331 return Imp::decimalToDouble(decimal);
1337 return Imp::decimalToDouble(decimal);
1343 return Imp::decimalToDouble(decimal);
1349 return Imp::decimalToDouble(decimal);
1355 return Imp::decimalToDouble(decimal);
1361 return Imp::decimalToDouble(decimal);
1369 return Imp::decimalToFloat(decimal);
1375 return Imp::decimalToFloat(decimal);
1381 return Imp::decimalToFloat(decimal);
1387 return Imp::decimalToFloat(decimal);
1393 return Imp::decimalToFloat(decimal);
1399 return Imp::decimalToFloat(decimal);
1409 Imp::decimalToDPD(buffer, decimal);
1416 Imp::decimalToDPD(buffer, decimal);
1423 Imp::decimalToDPD(buffer, decimal);
1430 Imp::decimalToDPD(buffer, decimal);
1437 Imp::decimalToDPD(buffer, decimal);
1444 Imp::decimalToDPD(buffer, decimal);
1453 return Imp::decimal32FromDPD(buffer);
1459 const unsigned char *buffer)
1461 *decimal = Imp::decimal32FromDPD(buffer);
1468 return Imp::decimal64FromDPD(buffer);
1474 const unsigned char *buffer)
1476 *decimal = Imp::decimal64FromDPD(buffer);
1483 return Imp::decimal128FromDPD(buffer);
1489 const unsigned char *buffer)
1491 *decimal = Imp::decimal128FromDPD(buffer);
1497 const unsigned char *buffer)
1499 Imp::decimalFromDPD(decimal, buffer);
1505 const unsigned char *buffer)
1507 Imp::decimalFromDPD(decimal, buffer);
1513 const unsigned char *buffer)
1515 Imp::decimalFromDPD(decimal, buffer);
1525 Imp::decimalToBID(buffer, decimal);
1532 Imp::decimalToBID(buffer, decimal);
1539 Imp::decimalToBID(buffer, decimal);
1546 Imp::decimalToBID(buffer, decimal);
1553 Imp::decimalToBID(buffer, decimal);
1560 Imp::decimalToBID(buffer, decimal);
1569 return Imp::decimal32FromBID(buffer);
1575 const unsigned char *buffer)
1577 *decimal = Imp::decimal32FromBID(buffer);
1584 return Imp::decimal64FromBID(buffer);
1590 const unsigned char *buffer)
1592 *decimal = Imp::decimal64FromBID(buffer);
1599 return Imp::decimal128FromBID(buffer);
1605 const unsigned char *buffer)
1607 *decimal = Imp::decimal128FromBID(buffer);
1613 const unsigned char *buffer)
1615 Imp::decimalFromBID(decimal, buffer);
1621 const unsigned char *buffer)
1623 Imp::decimalFromBID(decimal, buffer);
1629 const unsigned char *buffer)
1631 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:3101
Definition bdldfp_decimal.h:765
Definition bdldfp_decimal.h:1890
DecimalImpUtil::ValueType64 * data()
Return a modifiable pointer to the underlying implementation.
Definition bdldfp_decimal.h:6425
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#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)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdldfp_decimal.h:747
Decimal_Type64 Decimal64
Definition bdldfp_decimal.h:750
Definition bdldfp_decimalconvertutil.h:397
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:1065
static float decimalToFloat(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1385
static const unsigned char * decimal64FromVariableWidthEncoding(bdldfp::Decimal64 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1264
static void decimalToDPD(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1427
static Decimal32 decimal32FromBID(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1567
static Decimal64 decimal64FromMultiWidthEncodingRaw(const unsigned char *buffer, bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1107
static double decimalToDouble(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1347
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:982
static void decimal64ToBID(unsigned char *buffer, Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1529
static void decimal128ToBID(unsigned char *buffer, Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1536
static int decimal64FromMultiWidthEncodingIfValid(Decimal64 *decimal, const unsigned char *buffer, bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1090
static void decimalFromBID(Decimal32 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1612
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:1597
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:954
static unsigned char * decimalToNetwork(unsigned char *buffer, Decimal128 decimal)
static void decimal64ToDPD(unsigned char *buffer, Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1413
static Decimal64 decimal64FromDouble(double binary, int digits=0)
static void decimal128ToDPD(unsigned char *buffer, Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1420
static float decimal128ToFloat(Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1379
static Decimal64 decimal64FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1466
static void decimal32ToDPD(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1406
static double decimal32ToDouble(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1329
static const unsigned char * decimalFromNetwork(Decimal32 *decimal, const unsigned char *buffer)
static float decimal32ToFloat(Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1367
static const unsigned char * decimalFromNetwork(Decimal128 *decimal, const unsigned char *buffer)
static double decimal64ToDouble(Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1335
static Decimal32 decimal32FromFloat(float binary, int digits=0)
static void decimalFromDPD(Decimal32 *decimal, const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1496
static unsigned char * decimal64ToNetwork(unsigned char *buffer, Decimal64 decimal)
static unsigned char * decimal64ToVariableWidthEncoding(unsigned char *buffer, bdldfp::Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1174
static unsigned char * decimal128ToNetwork(unsigned char *buffer, Decimal128 decimal)
static float decimal64ToFloat(Decimal64 decimal)
Definition bdldfp_decimalconvertutil.h:1373
static Decimal64 decimal64FromBID(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1582
static Decimal128 decimal128FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1481
static const unsigned char * decimalFromNetwork(Decimal64 *decimal, const unsigned char *buffer)
static void decimal32ToBID(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1522
static bool isValidMultiWidthSize(bsls::Types::size_type size)
Definition bdldfp_decimalconvertutil.h:1059
static Decimal32 decimal32FromDPD(const unsigned char *buffer)
Definition bdldfp_decimalconvertutil.h:1451
static void decimalToBID(unsigned char *buffer, Decimal32 decimal)
Definition bdldfp_decimalconvertutil.h:1543
static double decimal128ToDouble(Decimal128 decimal)
Definition bdldfp_decimalconvertutil.h:1341
static Decimal64 makeDecimalRaw64(int significand, int exponent)
Definition bdldfp_decimalutil.h:856
std::size_t size_type
Definition bsls_types.h:126
unsigned long long Uint64
Definition bsls_types.h:139