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

SortUtils: fix termination condition & enforce unsigned value #28411

Merged
merged 1 commit into from
May 19, 2024
Merged
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
13 changes: 8 additions & 5 deletions examples/jsm/utils/SortUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Hybrid radix sort from
// - https://gist.github.com/sciecode/93ed864dd77c5c8803c6a86698d68dab
// - https://github.com/mrdoob/three.js/pull/27202#issuecomment-1817640271
//
// expects unsigned 32b integer values

const POWER = 3;
const BIT_MAX = 32;
const BIN_BITS = 1 << POWER;
Expand Down Expand Up @@ -102,11 +105,11 @@ export const radixSort = ( arr, opt ) => {

for ( let j = start + 1; j < start + len; j ++ ) {

const p = a[ j ], t = get( p );
const p = a[ j ], t = get( p ) >>> 0;
let i = j;
while ( i > 0 ) {
while ( i > start ) {

if ( compare( get( a[ i - 1 ] ), t ) )
if ( compare( get( a[ i - 1 ] ) >>> 0, t ) )
a[ i ] = a[ -- i ];
else
break;
Expand Down Expand Up @@ -140,14 +143,14 @@ export const radixSort = ( arr, opt ) => {
bin.fill( 0 );

for ( let j = start; j < end; j ++ )
bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ++;
bin[ ( get( a[ j ] ) >>> shift ) & BIN_MAX ] ++;

accumulate( bin );

cache.set( bin );

for ( let j = end - 1; j >= start; j -- )
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ] = a[ j ];
b[ start + -- bin[ ( get( a[ j ] ) >>> shift ) & BIN_MAX ] ] = a[ j ];

if ( depth == ITERATIONS - 1 ) return;

Expand Down