1from ctypes import c_float, c_int32, cast, byref, POINTER
2
3def ctypes_isqrt(number):
4 threehalfs = 1.5
5 x2 = number * 0.5
6 y = c_float(number)
7
8 i = cast(byref(y), POINTER(c_int32)).contents.value
9 i = c_int32(0x5f3759df - (i >> 1))
10 y = cast(byref(i), POINTER(c_float)).contents.value
11
12 y = y * (1.5 - (x2 * y * y))
13 return y
14