Code: Show/Hide int Q_log2( int val ) {
int answer; answer = 0; while ( ( val>>=1 ) != 0 ) { answer++; } return answer; } |
Code: Show/Hide float Q_rsqrt( float number )
{ long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed #ifndef Q3_VM #ifdef __linux__ assert( !isnan(y) ); // bk010122 - FPE? #endif #endif return y; } |
Cyan~Fire wrote: |
Optimizers can't do that? I hate micro-optimized C. :-( |
Cyan~Fire wrote: |
Well, if I ever write an optimizing compiler, I'm going to detect if someone needs a floating pt/integer function and call different code. |
Cyan~Fire wrote: |
if someone needs a floating pt/integer function and call different code. |
Code: Show/Hide wire[6:0] add (wire [7:0] a, wire [7:0] b)
{ wire [7:0] c; c = a[7:1] + b[7:1]; add = c[6:0]; } |
Code: Show/Hide entity summer is
port(input1, input2: in std_logic_vector(2 downto 0); output: out std_logic_vector(3 downto 0)); end summer; architecture algorithmic of summer is begin process(input1, input2) begin -- the ampersand is concatenation. it's used here to change -- the three wide inputs to four wide. output <= ('0' & input1) + ('0' & input2); end process; end algorithmic; |
Code: Show/Hide int log2(unsigned long val) {
int ret = 0; if (val >> 16) { ret |= 16; val >>= 16; } if (val >> 8) { ret |= 8; val >>= 8; } if (val >> 4) { ret |= 4; val >>= 4; } if (val >> 2) { ret |= 2; val >>= 2; } ret |= val >> 1; return ret; } |
Code: Show/Hide int log2quick(unsigned long val) {
int ret; if (val >> 8) { val >>= 8; ret = 8; if (val >> 12) { ret = 20; val >>= 12; } if (val >> 6) { ret += 6; val >>= 6; } if (val >> 3) { ret += 3; val >>= 3; } ret += (val >> 1) & 1 + (val >> 2); } else { ret = 0; if (val >> 4) { ret = 4; val >>= 4; } if (val >> 2) { ret |= 2; val >>= 2; } ret |= val >> 1; } return ret; } |