Skip to content
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

Compute next highest power of 2 function #374

Merged
merged 1 commit into from Dec 7, 2020

Conversation

bvernoux
Copy link
Contributor

@bvernoux bvernoux commented Dec 7, 2020

Compute next highest power of 2 to fix issues when using code (in different parts):
const size_t npoints = pow(2, ceil(log2(depth))); (see scopehal\TestWaveformSource.cpp => TestWaveformSource::DegradeSerialData()...)
The original code (pow(2, ceil(log2())) has some border effects when built with MSYS2 mingw64 "Release" mode as it does not compute correctly the highest power of 2 for example with parameter 100000 it was returning 131071 (instead of 131072) which crashed ffts.
This implementation fix issue ngscopeclient/scopehal-apps#295
The function next_pow2() shall replace pow(2, ceil(log2())) in following code:
scopehal\TestWaveformSource.cpp
Line 282: //const size_t npoints = pow(2, ceil(log2(depth)));
scopeprotocols\DeEmbedFilter.cpp
Line 230: const size_t npoints = pow(2, ceil(log2(npoints_raw)));
scopeprotocols\FFTFilter.cpp
Line 179: const size_t npoints = pow(2, ceil(log2(npoints_raw)));
scopeprotocols\JitterSpectrumFilter.cpp
Line 195: const size_t npoints = pow(2, ceil(log2(npoints_raw)));

Compute next highest power of 2 to fix issues when using code (in different parts):
  const size_t npoints = pow(2, ceil(log2(depth))); (see scopehal\TestWaveformSource.cpp => TestWaveformSource::DegradeSerialData()...)
The original code (pow(2, ceil(log2())) has some border effects when built with MSYS2 mingw64 "Release" mode as it does not compute correctly the highest power of 2 for example with parameter 100000 it was returning 131071 (instead of 131072) which crashed ffts.
This implementation fix issue ngscopeclient/scopehal-apps#295
The function next_pow2() shall replace pow(2, ceil(log2())) in following code:
scopehal\TestWaveformSource.cpp
	Line 282: 	//const size_t npoints = pow(2, ceil(log2(depth)));
scopeprotocols\DeEmbedFilter.cpp
	Line 230: 	const size_t npoints = pow(2, ceil(log2(npoints_raw)));
scopeprotocols\FFTFilter.cpp
	Line 179: 	const size_t npoints = pow(2, ceil(log2(npoints_raw)));
scopeprotocols\JitterSpectrumFilter.cpp
	Line 195: 	const size_t npoints = pow(2, ceil(log2(npoints_raw)));
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an extra shift by 32 to work properly for 64bit inputs. The input 0x100000001 would fail currently, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done a fast test and actual version seems to work fine up to value 9223372036854775807

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm your fix is good
see small tests here https://www.onlinegdb.com/mw3ZHYZsQ

@azonenberg azonenberg merged commit 45d156e into ngscopeclient:master Dec 7, 2020
Copy link
Contributor Author

@bvernoux bvernoux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fixed to
uint64_t next_pow2(uint64_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
v++;
return v;
}

@bvernoux
Copy link
Contributor Author

bvernoux commented Dec 7, 2020

That is a variant of https://jameshfisher.com/2018/03/30/round-up-power-2/ see uint64_t next_pow2m1(uint64_t x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants