From 09437dfee0851851e78d0c0e891a4060e41fece9 Mon Sep 17 00:00:00 2001 From: anastasosmoho Date: Thu, 1 Oct 2026 16:33:37 +0000 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=203.?= =?UTF-8?q?fast=20inverse=20square=20root?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit quake lll arena --- .../main3.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 works/pr2/3fast_inverse_square_root_quacklll/main3.cpp diff --git a/works/pr2/3fast_inverse_square_root_quacklll/main3.cpp b/works/pr2/3fast_inverse_square_root_quacklll/main3.cpp new file mode 100644 index 0000000..81cbed0 --- /dev/null +++ b/works/pr2/3fast_inverse_square_root_quacklll/main3.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +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; + +} + + \ No newline at end of file