### N-th root approximations

Continuing along with a previous post on fast calculation of cube roots, here are a couple of C++ template functions for bit hack n

template <int n>

__forceinline float nth_roota(float x)

{

const int ebits = 8;

const int fbits = 23;

int& i = (int&) x;

const int bias = (1 <<(ebits-1))-1;

i = (i - (bias <<fbits)) / n + (bias <<fbits);

return x;

}

template <int n>

__forceinline double nth_roota(double x)

{

const int ebits = 11;

const int fbits = 52;

_int64& i = (_int64&) x;

const _int64 bias = (1 <<(ebits-1))-1;

i = (i - (bias <<fbits)) / n + (bias <<fbits);

return x;

}

I'll discuss this more in a subsequent post. Sorry for the lack of indentation. (Blogger)And the handling of less than and greater than symbols which seems to make Blogger choke as well. If you see ampersands followed by 'gt' or 'lt', these need to be translated.

^{th}root approximations.template <int n>

__forceinline float nth_roota(float x)

{

const int ebits = 8;

const int fbits = 23;

int& i = (int&) x;

const int bias = (1 <<(ebits-1))-1;

i = (i - (bias <<fbits)) / n + (bias <<fbits);

return x;

}

template <int n>

__forceinline double nth_roota(double x)

{

const int ebits = 11;

const int fbits = 52;

_int64& i = (_int64&) x;

const _int64 bias = (1 <<(ebits-1))-1;

i = (i - (bias <<fbits)) / n + (bias <<fbits);

return x;

}

I'll discuss this more in a subsequent post. Sorry for the lack of indentation. (Blogger)And the handling of less than and greater than symbols which seems to make Blogger choke as well. If you see ampersands followed by 'gt' or 'lt', these need to be translated.

## 0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home