Branching and searching in memory are things that should be avoided when performing microoptimization on a piece of code. With the built-in build, you can simply use CMOV (Conditional MOV) to speed up work on x86 systems. The Java hotspot compiler can also be applied to this instruction. But since the fragment is very simple, performing too many operations to avoid branching or looking for memory can (in the end) be defeatist.
static int[] QUAD_LUT = new int[]{1, 2, 4, 3}; ...
When you think about the result, you are after
x.sign y.sign Quad 0 0 1 0 1 4 1 0 2 1 1 3
You can get to the formula
(x.sign XOR y.sign + y.sign + y.sign) + 1
So in Java
y = (y>>>31); return ((x>>>31) ^ y) + y + y + 1;
EDIT Only for people interested in embedded assembly ...
;; NASM/FASM syntax ;; GetQuadrant(int x, int y) ;; RETURN [1|2|3|4] in EAX register GetQuadrant: MOV eax, [esp+4] ;; = x MOV ecx, [esp+8] ;; = y SHR eax, 31 ;; = >> 31 SHR ecx, 31 ;; = >> 31 XOR eax, ecx ;; = x XOR y LEA eax, [eax + ecx * 2 + 1] ;; = x + y*2 + 1 RET 8 ;; correct stack and return
source share