-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dceda6a
commit b38eaaa
Showing
8 changed files
with
93 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import logging | ||
|
||
logger = logging.getLogger('quake_inverse_sq._sqrt_pure_python') | ||
|
||
logger.warning('No native extension found. Falling back to pure python implementation.') | ||
del logger, logging # epic memory safety for absolutely no fucking reasons | ||
|
||
from ctypes import c_float, c_int32, cast, byref, POINTER | ||
from math import sqrt | ||
|
||
def coarse_inv_sqrt(number: float): | ||
THREEHALFS = 1.5 | ||
x2 = number * 0.5 | ||
y = c_float(number) | ||
WHAT_THE_FUCK = 0x5f3759df | ||
i = cast(byref(y), POINTER(c_int32)).contents.value | ||
i = c_int32(WHAT_THE_FUCK - (i >> 1)) | ||
y = cast(byref(i), POINTER(c_float)).contents.value | ||
y = y * (THREEHALFS - (x2 * y * y)) | ||
return y | ||
def fined_inv_sqrt(number: float): | ||
return 1/sqrt(number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import unittest | ||
import sys | ||
|
||
try: | ||
|
||
if '--include_this_directory' in sys.argv: | ||
raise Exception('Avoiding removing current directory from sys.path') | ||
if '.' in sys.path: | ||
sys.path.remove('.') | ||
if '' in sys.path: | ||
sys.path.remove('') | ||
except: | ||
pass | ||
|
||
import quake_inverse_sq | ||
|
||
|
||
class TestCase(unittest.TestCase): | ||
def test_coarse_invert_squareroot(self) -> None: | ||
EXPECT = 0.49915357479239103 | ||
INPUT = 4 | ||
self.assertAlmostEqual(quake_inverse_sq.coarse_inv_sqrt(INPUT), EXPECT) | ||
|
||
def test_fined_invert_squareroot(self) -> None: | ||
EXPECT = 0.5 | ||
INPUT = 4 | ||
self.assertAlmostEqual(quake_inverse_sq.fined_inv_sqrt(INPUT), EXPECT) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters