MCPcopy Create free account
hub / github.com/davidgiven/fluxengine / fast_sqrt

Function fast_sqrt

dep/agg/include/agg_math.h:289–363  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

287#pragma warning(disable : 4035) // Disable warning "no return value"
288#endif
289 AGG_INLINE unsigned fast_sqrt(unsigned val)
290 {
291#if defined(_M_IX86) && defined(_MSC_VER) && !defined(AGG_NO_ASM)
292 // For Ix86 family processors this assembler code is used.
293 // The key command here is bsr - determination the number of the most
294 // significant bit of the value. For other processors
295 //(and maybe compilers) the pure C "#else" section is used.
296 __asm
297 {
298 mov ebx, val
299 mov edx, 11
300 bsr ecx, ebx
301 sub ecx, 9
302 jle less_than_9_bits
303 shr ecx, 1
304 adc ecx, 0
305 sub edx, ecx
306 shl ecx, 1
307 shr ebx, cl
308 less_than_9_bits:
309 xor eax, eax
310 mov ax, g_sqrt_table[ebx*2]
311 mov ecx, edx
312 shr eax, cl
313 }
314#else
315
316 // This code is actually pure C and portable to most
317 // arcitectures including 64bit ones.
318 unsigned t = val;
319 int bit = 0;
320 unsigned shift = 11;
321
322 // The following piece of code is just an emulation of the
323 // Ix86 assembler command "bsr" (see above). However on old
324 // Intels (like Intel MMX 233MHz) this code is about twice
325 // faster (sic!) then just one "bsr". On PIII and PIV the
326 // bsr is optimized quite well.
327 bit = t >> 24;
328 if (bit)
329 {
330 bit = g_elder_bit_table[bit] + 24;
331 }
332 else
333 {
334 bit = (t >> 16) & 0xFF;
335 if (bit)
336 {
337 bit = g_elder_bit_table[bit] + 16;
338 }
339 else
340 {
341 bit = (t >> 8) & 0xFF;
342 if (bit)
343 {
344 bit = g_elder_bit_table[bit] + 8;
345 }
346 else

Callers 5

calculateMethod · 0.85
calculateMethod · 0.85
calculateMethod · 0.85
semidot_hlineMethod · 0.85
pie_hlineMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected