Skip to content

Instantly share code, notes, and snippets.

@jjrv
Created August 19, 2015 14:38
Show Gist options
  • Save jjrv/ea66719a2c4b60c66894 to your computer and use it in GitHub Desktop.
Save jjrv/ea66719a2c4b60c66894 to your computer and use it in GitHub Desktop.
Fast exact integer perp dot product sign in JavaScript
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.
#include "stdint.h"
double perpdotTwiddle64(int32_t a1, int32_t b1, int32_t b2, int32_t a2) {
double a = (double)a1 * (double)a2;
double b = (double)b1 * (double)b2;
int32_t delta;
if(a != b) return(a - b);
delta = (a1 & 0x1fff) * (a2 & 0x1fff)
- (b1 & 0x1fff) * (b2 & 0x1fff);
return(((delta & 0x1fff) ^ 0x1000) - 0x1000);
};
// Sign of the perp dot product of two vectors with 32-bit coordinates.
// It returns an arbitrary number with a sign matching the correct result.
// Faster version if Math.imul is available.
function perpdotImul64(a1, b1, b2, a2) {
var a = a1 * a2;
var b = b1 * b2;
// Multiplication results have 53 bits of accuracy while 64 may be needed.
// If their difference is nonzero, its sign is still guaranteed correct.
if(a != b) return(a - b);
// If the first 53 bits match, we must check the remaining 11 bits.
// Math.imul gives us the lowest 32 bits, with signs matching the full
// multiplication result. Finally, we need to make the result unsigned.
return((Math.imul(a1, a2) - Math.imul(b1, b2)) | 0);
};
// Slower version using bit twiddling.
function perpdotTwiddle64(a1, b1, b2, a2) {
var a = a1 * a2;
var b = b1 * b2;
var delta;
if(a != b) return(a - b);
// The products must match or be large enough to overflow 53 bits.
// Since they can differ only in their last 11 bits, their signs must
// match. We can work with just the lowest 13 bits ignoring signs.
delta = (a1 & 0x1fff) * (a2 & 0x1fff) - (b1 & 0x1fff) * (b2 & 0x1fff);
// Result low bits can be for example 7fff and 0000, while the actual
// difference is small (such as +/- 1). To fix this, return only the
// lowest 13 bits and sign extend. See:
// https://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend
return(((delta & 0x1fff) ^ 0x1000) - 0x1000);
};
var perpdot64;
// Use best version based on JavaScript engine.
if(Math.imul) perpdot64 = perpdotImul64;
else perpdot64 = perpdotTwiddle64;
@jjrv
Copy link
Author

jjrv commented Aug 19, 2015

Explanations in a blog post

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