From: narke on
Hi,

I have a big code, and I cannot get understand some parts of it. Below
is a piece of them:

void determind_corr_value(void)
{
int8_t Leading0;
uint32_t CorrValue;

...

if( CorrValue>0)
{
// Detect bit number of MSB of CorrValue (Leading 0's are
counted)
Leading0 = 1;

while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0
{
CorrValue <<= 1;
Leading0++;
}

Leading0 -= 8;

// increase the resolution for compensation steps to 0.5Bit by
checking the 2nd bit
//lint -save -e701
Leading0 <<= 1;
if(*((int16_t*) &CorrValue + 1) & 0x4000)
{
Leading0--;
}

// Calculate now the correction value
if( Current < CalibrationData.I_Limit_Corr) {
// low range
Leading0-= (int8_t)Data.Leading0_Corr_Low;

CorrValue = (unsigned)(long)(32768L + ((int16_t)
(CalibrationData.Correction_Low * Leading0)));
} else {
// High range
Leading0-= (int8_t)Data.Leading0_Corr_High;

CorrValue = (unsigned)(long)(32768L + ((int16_t)
(CalibrationData.Correction_High * Leading0)));
}

} else
CorrValue = 32768;

...
}


I guess it was doing some kind of DSP, but not sure what was exactly
going on. Especially, I hope some one have a guess and give me some
hints for my below questions so far:

1. Why Leading0 was assigned as 1 at begin of the process.
2. Why 8 was substracted from the Leading0
3. What means 0.5bit and checking for 2nd bit?
4. Why 32768?

I hope the code indeed implemented some algorithm that is familiar to
some of you.


Regards,
woody

From: Jason on
On May 13, 10:00 am, narke <narkewo...(a)gmail.com> wrote:
> Hi,
>
> I have a big code, and I cannot get understand some parts of it. Below
> is a piece of them:
>
> void determind_corr_value(void)
> {
>    int8_t Leading0;
>    uint32_t CorrValue;
>
>    ...
>
>    if( CorrValue>0)
>    {
>       // Detect bit number of MSB of CorrValue (Leading 0's are
> counted)
>       Leading0 = 1;
>
>       while(!(*((int16_t*) &CorrValue + 1) < 0))   // MS_Bit = 0
>       {
>          CorrValue <<= 1;
>          Leading0++;
>       }
>
>       Leading0 -= 8;
>
>       // increase the resolution for compensation steps to 0.5Bit by
> checking the 2nd bit
>       //lint -save -e701
>       Leading0 <<= 1;
>       if(*((int16_t*) &CorrValue + 1) & 0x4000)
>       {
>          Leading0--;
>       }
>
>       // Calculate now the correction value
>       if( Current < CalibrationData.I_Limit_Corr) {
>          // low range
>          Leading0-= (int8_t)Data.Leading0_Corr_Low;
>
>          CorrValue = (unsigned)(long)(32768L + ((int16_t)
> (CalibrationData.Correction_Low * Leading0)));
>       } else {
>          // High range
>          Leading0-= (int8_t)Data.Leading0_Corr_High;
>
>          CorrValue = (unsigned)(long)(32768L + ((int16_t)
> (CalibrationData.Correction_High * Leading0)));
>       }
>
>    } else
>       CorrValue = 32768;
>
>    ...
>
> }
>
> I guess it was doing some kind of DSP, but not sure what was exactly
> going on.  Especially, I hope some one have a guess and give me some
> hints for my below questions so far:
>
> 1. Why Leading0 was assigned as 1 at begin of the process.
> 2. Why 8 was substracted from the Leading0
> 3. What means 0.5bit and checking for 2nd bit?
> 4. Why 32768?
>
> I hope the code indeed implemented some algorithm that is familiar to
> some of you.
>
> Regards,
> woody

You may not ever get any meaningful comments on this abomination of a
function, but without any kind of context, it's even more difficult to
figure out what is going on. Do you have any idea what it's used for?

Jason
From: William Hughes on
On May 13, 11:00 am, narke <narkewo...(a)gmail.com> wrote:
> Hi,
>
> I have a big code, and I cannot get understand some parts of it. Below
> is a piece of them:
>
> void determind_corr_value(void)
> {
>    int8_t Leading0;
>    uint32_t CorrValue;
>
>    ...
>
>    if( CorrValue>0)
>    {
>       // Detect bit number of MSB of CorrValue (Leading 0's are
> counted)
>       Leading0 = 1;
>
>       while(!(*((int16_t*) &CorrValue + 1) < 0))   // MS_Bit = 0
>       {
>          CorrValue <<= 1;
>          Leading0++;
>       }
>
>       Leading0 -= 8;
>
>       // increase the resolution for compensation steps to 0.5Bit by
> checking the 2nd bit
>       //lint -save -e701
>       Leading0 <<= 1;
>       if(*((int16_t*) &CorrValue + 1) & 0x4000)
>       {
>          Leading0--;
>       }
>
>       // Calculate now the correction value
>       if( Current < CalibrationData.I_Limit_Corr) {
>          // low range
>          Leading0-= (int8_t)Data.Leading0_Corr_Low;
>
>          CorrValue = (unsigned)(long)(32768L + ((int16_t)
> (CalibrationData.Correction_Low * Leading0)));
>       } else {
>          // High range
>          Leading0-= (int8_t)Data.Leading0_Corr_High;
>
>          CorrValue = (unsigned)(long)(32768L + ((int16_t)
> (CalibrationData.Correction_High * Leading0)));
>       }
>
>    } else
>       CorrValue = 32768;
>
>    ...
>
> }
>
> I guess it was doing some kind of DSP, but not sure what was exactly
> going on.

This is horrible. Basically the idea is to take a 16 bit
CorrValue (stored in the second half of a 32 bit integer)
and use it to choose one of 32 correction values.

There is also a sort of a truncated log transform. The
coder had decided to use bit manipulation, probably as
a dubious offering to the great god efficiency.
It also has the feel of 16 bit code shoehorned into
a 32 bit system.


If there is any way you can get away from this
code do so. Find out what needs to be done and code
from scratch.


>  Especially, I hope some one have a guess and give me some
> hints for my below questions so far:
>
> 1. Why Leading0 was assigned as 1 at begin of the process.

Leading0 starts out as the MSB. The MSB starts at 1.

> 2. Why 8 was substracted from the Leading0

Leading 0 has to become a signed quantity. If
the MSB is small, then the correction value will
be less than 32768 and Leading0 has to be negative.
Note the correction value will be changed
by some multiple of the (shifted) MSB. This
is a log transform.


> 3. What means 0.5bit and checking for 2nd bit?

Rather that using one bit (16 possible positions)
the code uses two bits (32 possible positions)

> 4. Why 32768?

This is 2^15, half of an unsigned 16 bit quantity. The correction
values are meant to fill a 16 bit unsigned integer.

- William Hughes
From: io_x on

"Jason" <cincydsp(a)gmail.com> ha scritto nel messaggio
news:87ef5391-d194-4e7c-908e-3af03868b5ca(a)h39g2000yqn.googlegroups.com...
On May 13, 10:00 am, narke <narkewo...(a)gmail.com> wrote:
> Hi,
>
> I have a big code, and I cannot get understand some parts of it. Below
> is a piece of them:
>
> void determind_corr_value(void)
> {
> int8_t Leading0;
> uint32_t CorrValue;
>
> ...
>
> if( CorrValue>0)
> {
> // Detect bit number of MSB of CorrValue (Leading 0's are
> counted)
> Leading0 = 1;
>
> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0

the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)?
i think it is the second one
if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false)
so this while is always false (no iteration)

you people get wrong when the over think for the C language grammar,
get you to lost the think on the problem to resolve :)

> {
> CorrValue <<= 1;
> Leading0++;
> }
>
> Leading0 -= 8;

time for pray




From: io_x on

"io_x" <a(a)b.c.invalid> ha scritto nel messaggio
news:4bec39a4$0$18987$4fafbaef(a)reader5.news.tin.it...
>> while(!(*((int16_t*) &CorrValue + 1) < 0)) // MS_Bit = 0
>
> the problem i have here: !*a<0 means !(*a<0), or it means ((!*a)<0)?
> i think it is the second one
> if it is the second one !*a could be 0 (0<0 false) or 1 (1<0 false)
> so this while is always false (no iteration)

all wrong i never read the second "("

> you people get wrong when the over think for the C language grammar,
> get you to lost the think on the problem to resolve :)

this is right

>> {
>> CorrValue <<= 1;
>> Leading0++;
>> }
>>
>> Leading0 -= 8;
>
> time for pray
>
>
>
>