generated from admin/template
задание 3.fast inverse square root
quake lll arena
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
int Q_rsqrt(float number){
|
||||||
|
long i;
|
||||||
|
float x2, y;
|
||||||
|
const float threehalfs = 1.5f;
|
||||||
|
x2 = number * 0.5f;
|
||||||
|
y = number;
|
||||||
|
// 1. "магический" битовый хак
|
||||||
|
memcpy(&i, &y, sizeof(i));
|
||||||
|
i = 0x5f3759df - (i >> 1); // <- магия
|
||||||
|
memcpy(&y, &i, sizeof(y)); // обратно в float
|
||||||
|
y = y * (threehalfs - (x2 * y * y));
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
for (float x = 0.5f; x <= 4.0f; x += 0.5f){
|
||||||
|
printf("x=%.1f 1/sqrt(x) ~= %.6f\n", x, Q_rsqrt(x));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user