Permalink
Cannot retrieve contributors at this time
| #include "types.h" | |
| #include "util.h" | |
| u32 rnd(void) { | |
| x1 = x1 * c1 + a1; | |
| x2 = x2 * c2 + a2; | |
| return (x1>>16) | (x2>>16); | |
| } | |
| u16 rotl(u16 value, u16 shift) { | |
| return (value << shift) | (value >> (16 - shift)); | |
| } | |
| // http://www.jb.man.ac.uk/~slowe/cpp/itoa.html | |
| // http://embeddedgurus.com/stack-overflow/2009/06/division-of-integers-by-constants/ | |
| // http://codereview.blogspot.com/2009/06/division-of-integers-by-constants.html | |
| // http://homepage.cs.uiowa.edu/~jones/bcd/divide.html | |
| /** | |
| * C++ version 0.4 char* style "itoa": | |
| * Written by Lukás Chmela | |
| * Released under GPLv3. | |
| */ | |
| char* itoa(int value, char* result, int base) { | |
| // check that the base if valid | |
| // removed for optimization | |
| // if (base < 2 || base > 36) { *result = '\0'; return result; } | |
| char* ptr = result, *ptr1 = result, tmp_char; | |
| int tmp_value; | |
| uint8_t inv = 0; | |
| // add: opt crashes on negatives | |
| if(value<0) { | |
| value = -value; | |
| inv++; | |
| } | |
| do { | |
| tmp_value = value; | |
| // opt-hack for base 10 assumed | |
| // value = (((uint16_t)value * (uint16_t)0xCD) >> 8) >> 3; | |
| value = (((uint32_t)value * (uint32_t)0xCCCD) >> 16) >> 3; | |
| // value /= base; | |
| *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; | |
| } while ( value ); | |
| // Apply negative sign | |
| if(inv) *ptr++ = '-'; | |
| *ptr-- = '\0'; | |
| while(ptr1 < ptr) { | |
| tmp_char = *ptr; | |
| *ptr--= *ptr1; | |
| *ptr1++ = tmp_char; | |
| } | |
| return result; | |
| } | |
| u32 uclip(u32 value, u32 low, u32 high) { | |
| if (value < low) return low; | |
| if (value > high) return high; | |
| return value; | |
| } | |
| s32 sclip(s32 value, s32 low, s32 high) { | |
| if (value < low) return low; | |
| if (value > high) return high; | |
| return value; | |
| } |