From: Vladimir Vassilevsky on


Clay wrote:


> // Simple binary logarithm routine by Clay S. Turner
> // Calculates binary logarithm of x assuming 1<= x < 2
> // Algo is designed to operate efficiently with binary integers
> although
> // this version also uses floating point for illustarting the algo.
>
> #define PRECISION 10 // # of bits the log will be calculated to
>
> double BinLog(double x)
> {
> int p=0,q=1,i;
>
> for (i=0;i<PRECISION;i++) {
> p<<=1;
> q<<=1;
> x=x*x;
> if (x>=2.0) {
> x/=2.0;
> p|=1;
> }
> }
> return (double)p / q;
> }

Very elegant indeed!

Here is a 7-bit accurate log2 function calculated from 8-bit mantessa.
It uses only 8-bit multiplications.

u8 log2_8bit(u8 x)
{
return (u8)(((x*((44045 - ((x*(u16)179)>>2)) >> 8)) + 165) >> 7);
}

(The accuracy of quadratic approximation of log2 is ~5e-3 anyway).


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com



From: Clay on
On Feb 2, 8:10 pm, Manny <mlou...(a)hotmail.com> wrote:
> On Feb 3, 12:40 am, Randy Yates <ya...(a)ieee.org> wrote:
>
>
>
>
>
> > Clay <c...(a)claysturner.com> writes:
> > > [...]
> > > // This algo implements a simple y^x where x is an integer.
> > > // Here PRECISION needs to be big enough to cover all of the "1" bits
> > > in the exponent.
> > > double Y2X(double radix, int x)
> > > {
> > > double y=1.0;
> > > int i,mask;
>
> > > mask=1<<PRECISION-1;
>
> > > for (i=0;i<PRECISION;i++) {
> > >    y=y*y;
> > >    if (x&mask)
> > >            y*=radix;
> > >    mask>>=1;
> > >    }
> > > return y;
> > > }
>
> > Wow, now that is tricky!
>
> > I had to write this up and try it out to see what was actually going
> > on. Now I see it's in how you factor the exponent, and taking advantage
> > of base 2 representation. I learnt sumtin'!
> > --
> > Randy Yates                      % "The dreamer, the unwoken fool -
> > Digital Signal Labs              %  in dreams, no pain will kiss the brow..."
> > mailto://ya...(a)ieee.org          %  http://www.digitalsignallabs.com%'Eldorado Overture', *Eldorado*, ELO
>
> Pretty cool all in all! And it's so cool that I promise - when I can
> find time of course - to benchmark this against Xilinx's AccelDSP
> cores which are supposedly as efficient as it gets. We can also cheat
> a bit and input an algorithmic description of this and see what the
> tools would readily spit out. Will report back on this though no way
> before end of Feb.
>
> -Momo- Hide quoted text -
>
> - Show quoted text -

I'd be interested is seeing how this turns out.

Clay

From: Clay on
On Feb 2, 7:40 pm, Randy Yates <ya...(a)ieee.org> wrote:
> Clay <c...(a)claysturner.com> writes:
> > [...]
> > // This algo implements a simple y^x where x is an integer.
> > // Here PRECISION needs to be big enough to cover all of the "1" bits
> > in the exponent.
> > double Y2X(double radix, int x)
> > {
> > double y=1.0;
> > int i,mask;
>
> > mask=1<<PRECISION-1;
>
> > for (i=0;i<PRECISION;i++) {
> >    y=y*y;
> >    if (x&mask)
> >            y*=radix;
> >    mask>>=1;
> >    }
> > return y;
> > }
>
> Wow, now that is tricky!
>
> I had to write this up and try it out to see what was actually going
> on. Now I see it's in how you factor the exponent, and taking advantage
> of base 2 representation. I learnt sumtin'!
> --
> Randy Yates                      % "The dreamer, the unwoken fool -
> Digital Signal Labs              %  in dreams, no pain will kiss the brow..."
> mailto://ya...(a)ieee.org          %  http://www.digitalsignallabs.com% 'Eldorado Overture', *Eldorado*, ELO- Hide quoted text -
>
> - Show quoted text -

Hello Randy, yes it is neat. In fact all of the CORDIC stuff exploits
the binary represention of the argument. By around 1969 or so, the
CORDIC stuff was expanded (a unification of algos was demonstrated) to
put together, trig functions, exponential, log, square root, and
hyperbolic functions all into basically one type of algo.

CORDIC -> COordinate Rotation DIgital Computer ---- Volder.

Volder did his early work with formulations for sines and cosines and
starting publicly showing his ideas back around 1956.

Clay



From: Jerry Avins on
abhijitk wrote:
> Hello everyone..
>
> How to calculate Logarithm of a signal in FPGA? Is there any hardware
> efficient method for it? Can CORDIC core be used to calculate log
> function?

I don't know, but there are probably several ways, come cheaper and less
accurate than others. You would do well to specify the accuracy you hope
to achieve.

Jerry
--
Engineering is the art of making what you want from things you can get.
�����������������������������������������������������������������������