Skip to content

Instantly share code, notes, and snippets.

@assyrianic
Last active August 30, 2024 16:42
Show Gist options
  • Save assyrianic/9ba5214ccc1ca2c6abba06b1d5385d57 to your computer and use it in GitHub Desktop.
Save assyrianic/9ba5214ccc1ca2c6abba06b1d5385d57 to your computer and use it in GitHub Desktop.
some functions that do Unsigned Comparison using Signed Ints for SourcePawn.
public class UCMP {
public static void main(String[] args) {
System.out.println("Hello World " + UInt32LT(1, -1));
}
/// x < y
public static boolean UInt32LT(int x, int y) {
boolean sign_x = (x & 0x80000000) != 0;
boolean sign_y = (y & 0x80000000) != 0;
if( sign_x==sign_y ) {
/// lower negative = higher positive.
return x < y;
}
return !sign_x && sign_y;
}
/// x > y
public static boolean UInt32GT(int x, int y) {
return UInt32LT(y, x);
}
/// x >= y
public static boolean UInt32GE(int x, int y) {
boolean sign_x = (x & 0x80000000) != 0;
boolean sign_y = (y & 0x80000000) != 0;
if( sign_x==sign_y ) {
/// lower negative = higher positive.
return x >= y;
}
return sign_x && !sign_y;
}
/// x <= y
public static boolean UInt32LE(int x, int y) {
return UInt32GE(y, x);
}
}
#if defined _unsigned_compare_included
#endinput
#endif
#define _unsigned_compare_included
/// x < y
stock bool UInt32LT(int x, int y) {
int sign_x = x & 0x80000000;
int sign_y = y & 0x80000000;
if( sign_x==sign_y ) {
/// lower negative = higher positive.
return x < y;
}
return ~sign_x && sign_y;
}
/// x > y
stock bool UInt32GT(int x, int y) {
return UInt32LT(y, x);
}
/// x >= y
stock bool UInt32GE(int x, int y) {
int sign_x = x & 0x80000000;
int sign_y = y & 0x80000000;
if( sign_x==sign_y ) {
/// lower negative = higher positive.
return x >= y;
}
return sign_x && ~sign_y;
}
/// x <= y
stock bool UInt32LE(int x, int y) {
return UInt32GE(y, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment