Last active
December 3, 2024 22:23
-
-
Save JarkkoPFC/dcae77af8ee29b7f7b80e2d79d9563e4 to your computer and use it in GitHub Desktop.
Bitonic sort for 64 elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
void bitonic_sort64(T vals_[64]) | |
{ | |
int h=-1; | |
do | |
{ | |
for(unsigned i=0; i<32; ++i) | |
{ | |
unsigned idx0=2*i; | |
unsigned idx1=4*(i&h)-2*(i+h)-1; | |
T v0=vals_[idx0]; | |
T v1=vals_[idx1]; | |
if((idx0<idx1)^(v0<v1)) | |
{ | |
vals_[idx0]=v1; | |
vals_[idx1]=v0; | |
} | |
} | |
for(int hh=h; !(hh&1);) | |
{ | |
hh>>=1; | |
for(unsigned i=0; i<32; ++i) | |
{ | |
unsigned idx0=2*(i&hh)+(i&~hh); | |
unsigned idx1=idx0+1+~hh; | |
T v0=vals_[idx0]; | |
T v1=vals_[idx1]; | |
if(v0>v1) | |
{ | |
vals_[idx0]=v1; | |
vals_[idx1]=v0; | |
} | |
} | |
} | |
h*=2; | |
} while(h!=-64); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HLSL implementation: https://godbolt.org/z/xbefr6aMd