Skip to content

Instantly share code, notes, and snippets.

@jjrv
Created August 20, 2015 14:50
Show Gist options
  • Save jjrv/e425a33aeca4108e7265 to your computer and use it in GitHub Desktop.
Save jjrv/e425a33aeca4108e7265 to your computer and use it in GitHub Desktop.
Counting sign changes without branching
function countSlow(signList) {
var sign;
var signPrev = 0;
var count = 0;
for(var i = 0; i < 8; i++) {
sign = signList[i];
if(sign != 0) {
if(sign != signPrev && signPrev != 0) count++;
signPrev = sign;
}
}
return(count);
}
function packSigns(signList) {
var signs = 0;
for(var i = 0; i < 8; i++) {
x = signList[i];
signs = (signs << 4) | ((x > 0) - (x < 0) & 3);
}
return(signs);
}
function countNonZero() {
var number_of_input_values = 8;
// Define this depending on your case.
var shift = (number_of_input_values - 2) * 4;
// Just use the high bits indicating sign. If above you replace each
// ((x > 0) - (x < 0) & 3) with just (x < 0) then this can be computed
// directly.
negatives = (signs >>> 1) & 0x11111111;
// Count changes by XOR'ing adjacent signs.
// Multiplying by 111... converts each digit into a cumulative sum of
// less significant digits. Finally, we extract the second most significant
// digit originally containing input.
var count = (((negatives ^ (negatives >>> 4)) * 0x11111111) >>> shift) & 15;
return(count);
}
function countSignChanges(signs) {
var mask = signs & 0x11111111;
var negatives = (signs >>> 1) & mask;
var positives = ~(signs >>> 1) & mask;
var flips = ( (mask - (negatives << 1)) & negatives ) |
( (mask - (positives << 1)) & positives );
var count = ((flips * 0x11111111) >>> 28) - !!mask;
return(count);
}
var signList = [];
// Check every possible combination of 8 positive and negative values.
for(var i = 0; i < Math.pow(2, 8); i++) {
var n = i;
for(var j = 0; j < 8; j++) {
signList[j] = 1 - 2 * (n % 2);
n = ~~(n / 2);
}
var signs = packSigns(signList);
if(countSlow(signList) != countNonZero(signs)) console.log('ERROR');
}
// Check every possible combination of 8 positive, negative and zero values.
for(var i = 0; i < Math.pow(3, 8); i++) {
var n = i;
for(var j = 0; j < 8; j++) {
signList[j] = 1 - (n % 3);
n = ~~(n / 3);
}
var signs = packSigns(signList);
if(countSlow(signList) != countSignChanges(signs)) console.log('ERROR');
}
The MIT License (MIT)
Copyright (c) 2015 BusFaster Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@jjrv
Copy link
Author

jjrv commented Aug 20, 2015

Details are in a blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment