diff options
author | Tom Musta | 2014-04-21 22:54:53 +0200 |
---|---|---|
committer | Alexander Graf | 2014-06-16 13:24:29 +0200 |
commit | 8e706db21ecfba75da3f9f843f1fa36276085742 (patch) | |
tree | 6f87bf3754b5c1ff98ee225cbe347695d52524eb /libdecnumber | |
parent | target-ppc: Enable Building of libdecnumber (diff) | |
download | qemu-8e706db21ecfba75da3f9f843f1fa36276085742.tar.gz qemu-8e706db21ecfba75da3f9f843f1fa36276085742.tar.xz qemu-8e706db21ecfba75da3f9f843f1fa36276085742.zip |
libdecnumber: Introduce decNumberFrom[U]Int64
Introduce two conversion functions to the libdecnumber library.
These conversions transform 64 bit integers to the internal decNumber
representation. Both a signed and unsigned version is added.
Signed-off-by: Tom Musta <tommusta@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>
Diffstat (limited to 'libdecnumber')
-rw-r--r-- | libdecnumber/decNumber.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libdecnumber/decNumber.c b/libdecnumber/decNumber.c index f6b6eb113a..6bd7565b34 100644 --- a/libdecnumber/decNumber.c +++ b/libdecnumber/decNumber.c @@ -436,6 +436,36 @@ uInt decNumberToUInt32(const decNumber *dn, decContext *set) { return 0; } /* decNumberToUInt32 */ +decNumber *decNumberFromInt64(decNumber *dn, int64_t in) +{ + uint64_t unsig = in; + if (in < 0) { + unsig = -unsig; + } + + decNumberFromUInt64(dn, unsig); + if (in < 0) { + dn->bits = DECNEG; /* sign needed */ + } + return dn; +} /* decNumberFromInt64 */ + +decNumber *decNumberFromUInt64(decNumber *dn, uint64_t uin) +{ + Unit *up; /* work pointer */ + decNumberZero(dn); /* clean */ + if (uin == 0) { + return dn; /* [or decGetDigits bad call] */ + } + for (up = dn->lsu; uin > 0; up++) { + *up = (Unit)(uin % (DECDPUNMAX + 1)); + uin = uin / (DECDPUNMAX + 1); + } + dn->digits = decGetDigits(dn->lsu, up-dn->lsu); + return dn; +} /* decNumberFromUInt64 */ + + /* ------------------------------------------------------------------ */ /* to-scientific-string -- conversion to numeric string */ /* to-engineering-string -- conversion to numeric string */ |