util.c (1179B)
1 #include "util.h" 2 #include "types.h" 3 #include <limits.h> 4 #include <stdint.h> 5 6 const u32 REGION_MASK[8] = { 7 /* KUSEG: 2048MB */ 8 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 9 /* KSEG0: 512MB */ 10 0x7fffffff, 11 /* KSEG1: 512MB */ 12 0x1fffffff, 13 /* KSEG2: 1024MB */ 14 0xffffffff, 0xffffffff 15 }; 16 17 u8 18 checked_addi32(i32 a, i32 b, i32* res) 19 { 20 if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b)) { 21 return 0; 22 } 23 *res = a + b; 24 return 1; 25 } 26 27 u8 28 checked_subi32(i32 a, i32 b, i32* res) 29 { 30 if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b) || (b == 0 && a == INT_MIN)) { 31 return 0; /* Overflow occurred */ 32 } 33 34 *res = a - b; 35 return 1; 36 } 37 38 void 39 swap_int(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } 40 41 void 42 swap_color(C *a, C *b) { C temp = *a; *a = *b; *b = temp; } 43 44 void 45 swap_vec2(ivec2 *a, ivec2 *b) { ivec2 temp = *a; *a = *b; *b = temp; } 46 47 void 48 UTIL_bin32(u32 x) { 49 for (int i = 31; i >= 0; i--) { 50 putchar((x & (1U << i)) ? '1' : '0'); 51 if (i % 8 == 0 && i != 0) putchar(' '); 52 } 53 } 54 55 void 56 UTIL_bin8(u8 x) { 57 for (int i = 7; i >= 0; i--) 58 putchar((x & (1 << i)) ? '1' : '0'); 59 } 60