A quick method to calculate the square root and power?

C # Math class makes roots and degrees only double. Various things can go a little faster if I add square-root functions and floating-point functions to my Math2 class (today is a day of rest, and I find the optimization relaxing).

So - the fast square root and power functions that I don’t need to worry about licensing, plsk thank you. Or a link that takes me there.

+3
source share
4 answers

I am going to consider it axiomatic that no software method will compete with hardware instruction for square roots. The only difficulty is that .NET does not give us direct control over the hardware, as in the days of inline assembler for C code.

First, let's discuss the overall hardware perspective of x86.

The x86 floating-point FSQRT command has three prefixes: single, double, and extended (built-in precision of 80-bit FP registers), and 25-40% less time for single or double precision. See here for 32-bit x86 instructions.

, . , . ++ ( ), , #.

. FSQRT , FDIV, Intel , , .

#, , SSE SIMD, . JIT- , .

Intel (15 2010 ), , .NET Framework 4 SIMD:

[ Intel ... SIMD- #]

Mono ​​ JIT SIMD Mono 2.2:

[Mono: Release Note Mono 2.2]

Mono SIMD MS #:

fooobar.com/questions/728748/...

( !), Mono SIMD:

fooobar.com/questions/1799423/...

+9

:

http://www.codecodex.com/wiki/Calculate_an_integer_square_root

.

:

// Finds the integer square root of a positive number  
public static int Isqrt(int num) {  
    if (0 == num) { return 0; }  // Avoid zero divide  
    int n = (num / 2) + 1;       // Initial estimate, never low  
    int n1 = (n + (num / n)) / 2;  
    while (n1 < n) {  
        n = n1;  
        n1 = (n + (num / n)) / 2;  
    } // end while  
    return n;  
} // end Isqrt()  

, C/++ , .

POW , , , .

private double Power(double a, int b) { 
    if (b<0) { 
        throw new ApplicationException("B must be a positive integer or zero"); 
    } 
    if (b==0) return 1; 
    if (a==0) return 0; 
    if (b%2==0) { 
        return Power(a*a, b/2); 
    } else if (b%2==1) { 
        return a*Power(a*a,b/2); 
    } 
    return 0; 
} 
+6

, - Managed ++. , , .

0

All Articles