net.ranides.assira.math
Class MathUtils

java.lang.Object
  extended by net.ranides.assira.math.MathUtils

public final class MathUtils
extends Object

Author:
ranides

Method Summary
static double clip(double value, double min, double max)
          Przycina podaną wartość do podanego zakresu.
static float clip(float value, float min, float max)
          Przycina podaną wartość do podanego zakresu.
static int clip(int value, int min, int max)
          Przycina podaną wartość do podanego zakresu.
static boolean inRange(byte value, byte min, byte max)
          Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).
static boolean inRange(int value, int min, int max)
          Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).
static boolean inRange(long value, long min, long max)
          Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).
static int roundClip(double value, int min, int max)
          Przycina podaną wartość do podanego zakresu i zaokrągla do liczby całkowitej.
static long roundClip(double value, long min, long max)
          Przycina podaną wartość do podanego zakresu i zaokrągla do liczby całkowitej.
static int signum(long diff)
          Alternate to signum for use in compare.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

inRange

public static boolean inRange(int value,
                              int min,
                              int max)
Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).

Parameters:
value -
min -
max -
Returns:

inRange

public static boolean inRange(byte value,
                              byte min,
                              byte max)
Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).

Parameters:
value -
min -
max -
Returns:

inRange

public static boolean inRange(long value,
                              long min,
                              long max)
Sprawdza, czy podana wartość mieści się w określonym zakresie (domkniętym).

Parameters:
value -
min -
max -
Returns:

clip

public static int clip(int value,
                       int min,
                       int max)
Przycina podaną wartość do podanego zakresu. To znaczy, że jeśli value < min, to zwraca min, jeśli value > max, to zwraca max, jeśli wartość mieści się w podanym zakresie, to jest zwracana bez zmian.

Parameters:
value -
min -
max -
Returns:

clip

public static double clip(double value,
                          double min,
                          double max)
Przycina podaną wartość do podanego zakresu. To znaczy, że jeśli value < min, to zwraca min, jeśli value > max, to zwraca max, jeśli wartość mieści się w podanym zakresie, to jest zwracana bez zmian.

Parameters:
value -
min -
max -
Returns:

clip

public static float clip(float value,
                         float min,
                         float max)
Przycina podaną wartość do podanego zakresu. To znaczy, że jeśli value < min, to zwraca min, jeśli value > max, to zwraca max, jeśli wartość mieści się w podanym zakresie, to jest zwracana bez zmian.

Parameters:
value -
min -
max -
Returns:

roundClip

public static int roundClip(double value,
                            int min,
                            int max)
Przycina podaną wartość do podanego zakresu i zaokrągla do liczby całkowitej. To znaczy, że jeśli value < min, to zwraca min, jeśli value > max, to zwraca max, jeśli wartość mieści się w podanym zakresie, to jest zwracana bez zmian.

Parameters:
value -
min -
max -
Returns:

roundClip

public static long roundClip(double value,
                             long min,
                             long max)
Przycina podaną wartość do podanego zakresu i zaokrągla do liczby całkowitej. To znaczy, że jeśli value < min, to zwraca min, jeśli value > max, to zwraca max, jeśli wartość mieści się w podanym zakresie, to jest zwracana bez zmian.

Parameters:
value -
min -
max -
Returns:

signum

public static int signum(long diff)
Alternate to signum for use in compare. Not a true signum, since it returns ints other than +/-1. Where there is any possibility of overflow, you should compare two longs with < rather than subtraction.

In Pentium assembler you could implement this algorthm with following code:

  # diff = edx:eax 
  # result = eax
  mov ebx,eax
  shl eax,1
  or  eax,ebx
  slr eax,1
  or  eax,edx
 
which would take 5 cycles, 2 more that lohi. However, JET did even better, with code essentially this using a clever trick to implement piotr.
   lea    ecx,0(eax,eax)  ; shifts lo left by doubling, keeps copy of
   lo
   or     eax,ecx
   shr    eax,1
   or     eax,edx
 
This is 4 cycles, still one more than lohi. Why was Piotr so much faster on JET? Peter has no pipeline-confounding jumps. Further, the lo then high operands actually come from the ram-based stack. Piotr nicely separates the accesses giving plenty of for pre-emptive fetch of hi. lohi insists on having them both upfront, so it has to wait for memory access. Piotr does not have to wait. Modern CPUS hurry up and wait for RAM most of the time.

Parameters:
diff - number to be collapsed to an int preserving sign and zeroness. usually represents the difference of two long.
Returns:
sign of diff, some int < 0, 0 or some int > 0.


Copyright © 2013. All Rights Reserved.