Skip to content

Commit f5cdbd7

Browse files
committed
Use Ui32tonum instead of i32tonum
It is more appropriate considering we are dealing with unsigned numbers in the context of the code.
1 parent 59b3934 commit f5cdbd7

File tree

1 file changed

+64
-73
lines changed

1 file changed

+64
-73
lines changed

src/CalcManager/Ratpack/conv.cpp

Lines changed: 64 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include <algorithm>
2121
#include "winerror_cross_platform.h"
22-
#include <sstream>
2322
#include <cstring> // for memmove, memcpy
2423
#include "ratpak.h"
2524

@@ -85,7 +84,7 @@ namespace
8584

8685
if (ullOperand <= UINT32_MAX)
8786
{
88-
*pulResult = (uint32_t)ullOperand;
87+
*pulResult = static_cast<uint32_t>(ullOperand);
8988
hr = S_OK;
9089
}
9190

@@ -130,7 +129,7 @@ void* zmalloc(size_t a)
130129

131130
void _dupnum(_In_ PNUMBER dest, _In_ const NUMBER* const src)
132131
{
133-
memcpy(dest, src, (int)(sizeof(NUMBER) + ((src)->cdigit) * (sizeof(MANTTYPE))));
132+
memcpy(dest, src, sizeof(NUMBER) + src->cdigit * sizeof(MANTTYPE));
134133
}
135134

136135
//-----------------------------------------------------------------------------
@@ -148,10 +147,12 @@ void _dupnum(_In_ PNUMBER dest, _In_ const NUMBER* const src)
148147
void _destroynum(_Frees_ptr_opt_ PNUMBER pnum)
149148

150149
{
151-
if (pnum != nullptr)
150+
if (pnum == nullptr)
152151
{
153-
free(pnum);
152+
return;
154153
}
154+
155+
free(pnum);
155156
}
156157

157158
//-----------------------------------------------------------------------------
@@ -170,12 +171,13 @@ void _destroynum(_Frees_ptr_opt_ PNUMBER pnum)
170171
void _destroyrat(_Frees_ptr_opt_ PRAT prat)
171172

172173
{
173-
if (prat != nullptr)
174+
if (prat == nullptr)
174175
{
175-
destroynum(prat->pp);
176-
destroynum(prat->pq);
177-
free(prat);
176+
return;
178177
}
178+
destroynum(prat->pp);
179+
destroynum(prat->pq);
180+
free(prat);
179181
}
180182

181183
//-----------------------------------------------------------------------------
@@ -200,7 +202,7 @@ PNUMBER _createnum(_In_ uint32_t size)
200202
if (SUCCEEDED(Calc_ULongAdd(size, 1, &cbAlloc)) && SUCCEEDED(Calc_ULongMult(cbAlloc, sizeof(MANTTYPE), &cbAlloc))
201203
&& SUCCEEDED(Calc_ULongAdd(cbAlloc, sizeof(NUMBER), &cbAlloc)))
202204
{
203-
pnumret = (PNUMBER)zmalloc(cbAlloc);
205+
pnumret = static_cast<PNUMBER>(zmalloc(cbAlloc));
204206
if (pnumret == nullptr)
205207
{
206208
throw(CALC_E_OUTOFMEMORY);
@@ -227,12 +229,9 @@ PNUMBER _createnum(_In_ uint32_t size)
227229
//
228230
//-----------------------------------------------------------------------------
229231

230-
PRAT _createrat(void)
231-
232+
PRAT _createrat()
232233
{
233-
PRAT prat = nullptr;
234-
235-
prat = (PRAT)zmalloc(sizeof(RAT));
234+
PRAT prat = static_cast<PRAT>(zmalloc(sizeof(RAT)));
236235

237236
if (prat == nullptr)
238237
{
@@ -303,16 +302,16 @@ PRAT numtorat(_In_ PNUMBER pin, uint32_t radix)
303302
PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
304303

305304
{
306-
PNUMBER sum = i32tonum(0, radix);
307-
PNUMBER powofnRadix = i32tonum(BASEX, radix);
305+
PNUMBER sum = Ui32tonum(0, radix);
306+
PNUMBER powofnRadix = Ui32tonum(BASEX, radix);
308307

309308
// A large penalty is paid for conversion of digits no one will see anyway.
310309
// limit the digits to the minimum of the existing precision or the
311310
// requested precision.
312-
uint32_t cdigits = precision + 1;
313-
if (cdigits > (uint32_t)a->cdigit)
311+
int32_t cdigits = precision + 1;
312+
if (cdigits > a->cdigit)
314313
{
315-
cdigits = (uint32_t)a->cdigit;
314+
cdigits = a->cdigit;
316315
}
317316

318317
// scale by the internal base to the internal exponent offset of the LSD
@@ -322,7 +321,7 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
322321
for (MANTTYPE* ptr = &(a->mant[a->cdigit - 1]); cdigits > 0; ptr--, cdigits--)
323322
{
324323
// Loop over all the bits from MSB to LSB
325-
for (uint32_t bitmask = BASEX / 2; bitmask > 0; bitmask /= 2)
324+
for (auto bitmask = BASEX >> 1; bitmask > 0; bitmask >>= 1)
326325
{
327326
addnum(&sum, sum, radix);
328327
if (*ptr & bitmask)
@@ -356,8 +355,8 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision)
356355

357356
PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix)
358357
{
359-
PNUMBER pnumret = i32tonum(0, BASEX); // pnumret is the number in internal form.
360-
PNUMBER num_radix = i32tonum(radix, BASEX);
358+
PNUMBER pnumret = Ui32tonum(0, BASEX); // pnumret is the number in internal form.
359+
PNUMBER num_radix = Ui32tonum(radix, BASEX);
361360
MANTTYPE* ptrdigit = a->mant; // pointer to digit being worked on.
362361

363362
// Digits are in reverse order, back over them LSD first.
@@ -370,7 +369,7 @@ PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix)
370369
// WARNING:
371370
// This should just smack in each digit into a 'special' thisdigit.
372371
// and not do the overhead of recreating the number type each time.
373-
thisdigit = i32tonum(*ptrdigit--, BASEX);
372+
thisdigit = Ui32tonum(*ptrdigit--, BASEX);
374373
addnum(&pnumret, thisdigit, BASEX);
375374
destroynum(thisdigit);
376375
}
@@ -611,12 +610,12 @@ wchar_t NormalizeCharDigit(wchar_t c, uint32_t radix)
611610

612611
PNUMBER StringToNumber(wstring_view numberString, uint32_t radix, int32_t precision)
613612
{
614-
int32_t expSign = 1L; // expSign is exponent sign ( +/- 1 )
615-
int32_t expValue = 0L; // expValue is exponent mantissa, should be unsigned
613+
int32_t expSign = 1; // expSign is exponent sign ( +/- 1 )
614+
int32_t expValue = 0; // expValue is exponent mantissa, should be unsigned
616615

617616
PNUMBER pnumret = nullptr;
618617
createnum(pnumret, static_cast<uint32_t>(numberString.length()));
619-
pnumret->sign = 1L;
618+
pnumret->sign = 1;
620619
pnumret->cdigit = 0;
621620
pnumret->exp = 0;
622621
MANTTYPE* pmant = pnumret->mant + numberString.length() - 1;
@@ -824,7 +823,7 @@ PNUMBER i32tonum(int32_t ini32, uint32_t radix)
824823

825824
do
826825
{
827-
*pmant++ = (MANTTYPE)(ini32 % radix);
826+
*pmant++ = static_cast<MANTTYPE>(ini32 % radix);
828827
ini32 /= radix;
829828
pnumret->cdigit++;
830829
} while (ini32);
@@ -859,7 +858,7 @@ PNUMBER Ui32tonum(uint32_t ini32, uint32_t radix)
859858

860859
do
861860
{
862-
*pmant++ = (MANTTYPE)(ini32 % radix);
861+
*pmant++ = static_cast<MANTTYPE>(ini32 % radix);
863862
ini32 /= radix;
864863
pnumret->cdigit++;
865864
} while (ini32);
@@ -972,7 +971,7 @@ uint64_t rattoUi64(_In_ PRAT prat, uint32_t radix, int32_t precision)
972971
destroyrat(prat32);
973972
destroyrat(pint);
974973

975-
return (((uint64_t)hi << 32) | lo);
974+
return (static_cast<uint64_t>(hi) << 32) | lo;
976975
}
977976

978977
//-----------------------------------------------------------------------------
@@ -992,17 +991,16 @@ int32_t numtoi32(_In_ PNUMBER pnum, uint32_t radix)
992991
{
993992
int32_t lret = 0;
994993

995-
MANTTYPE* pmant = pnum->mant;
996-
pmant += pnum->cdigit - 1;
994+
MANTTYPE* pmant = pnum->mant + pnum->cdigit - 1;
997995

998-
int32_t expt = pnum->exp;
999-
for (int32_t length = pnum->cdigit; length > 0 && length + expt > 0; length--)
996+
auto expt = pnum->exp;
997+
for (auto length = pnum->cdigit; length > 0 && length + expt > 0; length--)
1000998
{
1001999
lret *= radix;
10021000
lret += *(pmant--);
10031001
}
10041002

1005-
while (expt-- > 0)
1003+
for (; expt > 0; --expt)
10061004
{
10071005
lret *= radix;
10081006
}
@@ -1025,37 +1023,34 @@ int32_t numtoi32(_In_ PNUMBER pnum, uint32_t radix)
10251023

10261024
bool stripzeroesnum(_Inout_ PNUMBER pnum, int32_t starting)
10271025
{
1028-
bool fstrip = false;
10291026
// point pmant to the LeastCalculatedDigit
10301027
MANTTYPE* pmant = pnum->mant;
10311028
int32_t cdigits = pnum->cdigit;
10321029
// point pmant to the LSD
10331030
if (cdigits > starting)
10341031
{
1035-
pmant += cdigits - starting;
1032+
pmant = pmant + cdigits - starting;
10361033
cdigits = starting;
10371034
}
10381035

10391036
// Check we haven't gone too far, and we are still looking at zeros.
1040-
while ((cdigits > 0) && !(*pmant))
1037+
if (cdigits < 1 || *pmant != 0)
1038+
return false;
1039+
1040+
do
10411041
{
10421042
// move to next significant digit and keep track of digits we can
10431043
// ignore later.
10441044
pmant++;
10451045
cdigits--;
1046-
fstrip = true;
1047-
}
1048-
1049-
// If there are zeros to remove.
1050-
if (fstrip)
1051-
{
1052-
// Remove them.
1053-
memmove(pnum->mant, pmant, (int)(cdigits * sizeof(MANTTYPE)));
1054-
// And adjust exponent and digit count accordingly.
1055-
pnum->exp += (pnum->cdigit - cdigits);
1056-
pnum->cdigit = cdigits;
1057-
}
1058-
return (fstrip);
1046+
} while (cdigits > 0 && *pmant == 0);
1047+
1048+
// Remove the zeros
1049+
memmove(pnum->mant, pmant, cdigits * sizeof(MANTTYPE));
1050+
// And adjust exponent and digit count accordingly.
1051+
pnum->exp += pnum->cdigit - cdigits;
1052+
pnum->cdigit = cdigits;
1053+
return true;
10591054
}
10601055

10611056
//-----------------------------------------------------------------------------
@@ -1099,7 +1094,7 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
10991094
if (!zernum(pnum) && (pnum->cdigit >= precision || (length - exponent > precision && exponent >= -MAX_ZEROS_AFTER_DECIMAL)))
11001095
{
11011096
// Otherwise round.
1102-
round = i32tonum(radix, radix);
1097+
round = Ui32tonum(radix, radix);
11031098
divnum(&round, num_two, radix, precision);
11041099

11051100
// Make round number exponent one below the LSD for the number.
@@ -1202,7 +1197,7 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
12021197
result = L'-';
12031198
}
12041199

1205-
if (exponent <= 0 && !useSciForm)
1200+
if (exponent < 1 && !useSciForm)
12061201
{
12071202
result += L'0';
12081203
result += g_decimalSeparator;
@@ -1217,26 +1212,24 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, NumberFormat format, uint32_t radi
12171212

12181213
while (length > 0)
12191214
{
1220-
exponent--;
12211215
result += DIGITS[*pmant--];
12221216
length--;
12231217

12241218
// Be more regular in using a decimal point.
1225-
if (exponent == 0)
1219+
if (--exponent == 0)
12261220
{
12271221
result += g_decimalSeparator;
12281222
}
12291223
}
12301224

1231-
while (exponent > 0)
1225+
if (exponent > 0)
12321226
{
1233-
result += L'0';
1234-
exponent--;
1235-
// Be more regular in using a decimal point.
1236-
if (exponent == 0)
1227+
do
12371228
{
1238-
result += g_decimalSeparator;
1239-
}
1229+
result += L'0';
1230+
} while (--exponent > 0);
1231+
// Be more regular in using a decimal point.
1232+
result += g_decimalSeparator;
12401233
}
12411234

12421235
if (useSciForm)
@@ -1439,15 +1432,16 @@ PNUMBER i32prodnum(int32_t start, int32_t stop, uint32_t radix)
14391432

14401433
lret = i32tonum(1, radix);
14411434

1442-
while (start <= stop)
1435+
for (; start <= stop; start++)
14431436
{
1444-
if (start)
1437+
if (!start)
14451438
{
1446-
tmp = i32tonum(start, radix);
1447-
mulnum(&lret, tmp, radix);
1448-
destroynum(tmp);
1439+
continue;
14491440
}
1450-
start++;
1441+
1442+
tmp = i32tonum(start, radix);
1443+
mulnum(&lret, tmp, radix);
1444+
destroynum(tmp);
14511445
}
14521446
return (lret);
14531447
}
@@ -1470,15 +1464,14 @@ void numpowi32(_Inout_ PNUMBER* proot, int32_t power, uint32_t radix, int32_t pr
14701464
{
14711465
PNUMBER lret = i32tonum(1, radix);
14721466

1473-
while (power > 0)
1467+
for (; power > 0; power >>= 1)
14741468
{
14751469
if (power & 1)
14761470
{
14771471
mulnum(&lret, *proot, radix);
14781472
}
14791473
mulnum(proot, *proot, radix);
14801474
TRIMNUM(*proot, precision);
1481-
power >>= 1;
14821475
}
14831476
destroynum(*proot);
14841477
*proot = lret;
@@ -1511,9 +1504,7 @@ void ratpowi32(_Inout_ PRAT* proot, int32_t power, int32_t precision)
15111504
}
15121505
else
15131506
{
1514-
PRAT lret = nullptr;
1515-
1516-
lret = i32torat(1);
1507+
PRAT lret = i32torat(1);
15171508

15181509
while (power > 0)
15191510
{

0 commit comments

Comments
 (0)