Skip to content

Simplify calculations of the nearest power of 2 #2069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions src/CalcManager/CEngine/sciset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RadixType radixtype, NUM_WIDTH numwidt
DisplayNum();
}

int32_t CCalcEngine::DwWordBitWidthFromNumWidth(NUM_WIDTH numwidth)
uint32_t CCalcEngine::DwWordBitWidthFromNumWidth(NUM_WIDTH numwidth)
{
switch (numwidth)
{
Expand Down Expand Up @@ -103,30 +103,15 @@ bool CCalcEngine::TryToggleBit(CalcEngine::Rational& rat, uint32_t wbitno)
}

// Returns the nearest power of two
int CCalcEngine::QuickLog2(int iNum)
unsigned int CCalcEngine::QuickLog2(unsigned int iNum)
{
int iRes = 0;
// Nearest power of 2 to 0 is 1
if (!iNum)
return 1;

// while first digit is a zero
while (!(iNum & 1))
{
iRes++;
iNum >>= 1;
}

// if our number isn't a perfect square
iNum = iNum >> 1;
if (iNum)
{
// find the largest digit
for (iNum = iNum >> 1; iNum; iNum = iNum >> 1)
++iRes;

// and then add two
iRes += 2;
}

return iRes;
// Count the number of leading 0's of iNum - 1,
// and then shift 1 by that amount
return 1U << (32 - __builtin_clz(iNum - 1));
}

////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/CalcManager/Header Files/CalcEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class CCalcEngine
CalcEngine::Rational SciCalcFunctions(CalcEngine::Rational const& rat, uint32_t op);
CalcEngine::Rational DoOperation(int operation, CalcEngine::Rational const& lhs, CalcEngine::Rational const& rhs);
void SetRadixTypeAndNumWidth(RadixType radixtype, NUM_WIDTH numwidth);
int32_t DwWordBitWidthFromNumWidth(NUM_WIDTH numwidth);
uint32_t DwWordBitWidthFromNumWidth(NUM_WIDTH numwidth);
uint32_t NRadixFromRadixType(RadixType radixtype);
double GenerateRandomNumber();

Expand All @@ -199,7 +199,7 @@ class CCalcEngine
static std::vector<uint32_t> DigitGroupingStringToGroupingVector(std::wstring_view groupingString);
std::wstring GroupDigits(std::wstring_view delimiter, std::vector<uint32_t> const& grouping, std::wstring_view displayString, bool isNumNegative = false);

static int QuickLog2(int iNum);
static unsigned int QuickLog2(unsigned int iNum);
static void ChangeBaseConstants(uint32_t radix, int maxIntDigits, int32_t precision);
void BaseOrPrecisionChanged();

Expand Down