On Fri, Mar 15, 2019 at 12:38:59PM -0700, Guenter Roeck wrote:
> On Fri, Mar 15, 2019 at 11:29:45AM -0700, Guenter Roeck wrote:
> > On Fri, Mar 15, 2019 at 10:19:33AM +0000, Grönke, Christian wrote:
> > > Hello Guenter,
> > > 
> > > > -----Ursprüngliche Nachricht-----
> > > > Von: Guenter Roeck <[email protected]> Im Auftrag von Guenter Roeck
> > > > Gesendet: Donnerstag, 14. März 2019 17:53
> > > > An: Grönke, Christian <[email protected]>
> > > > Cc: [email protected]
> > > > Betreff: Re: PMBus driver for FSP/3Y Power device with non-standard VOUT
> > > > values (LINEAR11 vs LINEAR16)
> > > > 
> > > > Hi Christian,
> > > > On Thu, Mar 14, 2019 at 04:08:32PM +0000, Grönke, Christian wrote:
> > > > >
> > > > > The framework code seemed to work fine. I used your code for the
> > > > conversion:
> > > > >     linear11 -> 'scaled integer' -> ieee754 It provided a way to test
> > > > > the code and was easy for me as my tries to do some other bit magic
> > > > > weren't successful. That means I partly tested the code from
> > > > > pmbus_data2reg_ieee754 as my read_word function uses this for the
> > > > > conversion. Of course not the module local function...
> > > > >
> > > > 
> > > > Wondering ... I would have thought that it should be possible to
> > > > implement a simplified linear11 <--> ieee754 conversion, without
> > > > converting to a scaled integer first. Have you tried that ?
> > > 
> > > There might be a more efficient and elegant way. I toyed around with
> > > the conversion for a moment but didn't figure out a working way 
> > > quickly.
> > 
> > This should work.
> > 

Somewhat optimized versions, with additional boundary checks, kernel types,
and one less while loop.

Guenter

---
u16 lin2ieee(u16 lin11)
{
        s16 mantissa = ((s16)((lin11 & 0x7ff) << 5)) >> 5;
        int exponent = (((s16)lin11) >> 11) + 25;
        u16 sign = 0;

        /* simple case */
        if (mantissa == 0)
                return 0;

        if (mantissa < 0) {
                sign = 0x8000;
                mantissa = -mantissa;
        }
        while (mantissa < 0x400 && exponent > 1) {
                mantissa <<= 1;
                exponent--;
        }

        /* boundary checks */
        if (exponent > 30)
                return sign | 0x7bff;
        if (mantissa < 0x400)
                return sign | 0x0400;

        return sign | (exponent << 10) | (mantissa & 0x3ff);
}

u16 ieee2lin(u16 ieee)
{
        s16 mantissa = (ieee & 0x3ff) | 0x400;
        int exponent = ((ieee >> 10) & 0x1f); 
        u16 sign = ieee & 0x8000;
        int shift;

        /* 0 or too small */
        if (ieee == sign || exponent == 0)
                return 0;

        /* NaN */
        if (exponent == 0x1f)
                return sign ? 0xffff : 0xfbff;

        exponent -= 25;

        /* map into lin11 number space */
        mantissa >>= 1;
        exponent++;

        shift = -15 - exponent;
        if (shift > 0) {
                exponent = -15;
                mantissa >>= shift;
        }

        /* boundary checks */
        if (mantissa == 0)      /* number too small */
                return 0;

        if (sign)
                mantissa = -mantissa;

        return (exponent << 11) | (mantissa & 0x7ff);
}

Reply via email to