From: io_x on

"narke" <narkewoody(a)gmail.com> ha scritto nel messaggio
news:fv0ou5lu8t3m8m1i7kkn5j7e0ou1dvot14(a)4ax.com...
> 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++;
> }

i don't see why not write it in this way
%define MS_Bit 0x00008000

while((CorrValue & MS_Bit)==0)
{CorrValue <<= 1;
Leading0++;
if(CorrValue==0) return error2349;
}

i don't know if it is right...

> 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: narke on
On Thu, 13 May 2010 07:36:36 -0700 (PDT), Jason <cincydsp(a)gmail.com>
wrote:

>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?
>

The program is running on an high end electrical meter. The meter has
an embedded DSP chip than can measure current/voltage/energy that
inputed to the meter. every one seconds (actually even small), the
program in the meter talks with the chip, retrieve back its data and
do some post process. In this piece of code, the program is do a
calculation that has something to do with the correction value or
compensation value that will be laterly applied to the inputed current
quantities.

>Jason
From: narke on
On Thu, 13 May 2010 10:07:00 -0700 (PDT), William Hughes
<wpihughes(a)hotmail.com> wrote:

>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.

Interesting! Why do you think the second part of the CorrValue is an
index and where are the 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.

God, you are righ! the microprocess had upgraded from 16bit to 32bit
long time ago, so the original 16bit code had been ported to 32bit.

I want to know more about what you said, log transform. I mean, I do
know what is logarithm transform but I can not identify where is the
logarithm operation in the code. Can you help?

>
>
>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.

I don't get it. By 'MSB starts at 1', did you mean the position of
the MSB is the second couting from left? So, how will you call the
first bit counting from left?

>
>> 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.

Fully don't understand. Could you explain in more detail?

>
>
>> 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)

why:
one bit --> 16
two bit --> 32

>
>> 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.

Did you mean, if that is 2^15, then it can be ensure the correction
value will fill into a 16 bit unsigned? Is there a reasoning?

>
> - William Hughes
From: Ian Collins on
On 05/14/10 06:59 PM, io_x wrote:
> "narke"<narkewoody(a)gmail.com> ha scritto nel messaggio
> news:fv0ou5lu8t3m8m1i7kkn5j7e0ou1dvot14(a)4ax.com...
>> 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++;
>> }
>
> i don't see why not write it in this way
> %define MS_Bit 0x00008000
>
Why use a macro when a const uint32_t would do?

> while((CorrValue& MS_Bit)==0)
> {CorrValue<<= 1;
> Leading0++;
> if(CorrValue==0) return error2349;
> }

--
Ian Collins
From: narke on
On Fri, 14 May 2010 08:59:06 +0200, "io_x" <a(a)b.c.invalid> wrote:

>
>"narke" <narkewoody(a)gmail.com> ha scritto nel messaggio
>news:fv0ou5lu8t3m8m1i7kkn5j7e0ou1dvot14(a)4ax.com...
>> 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++;
>> }
>
>i don't see why not write it in this way
>%define MS_Bit 0x00008000
>
>while((CorrValue & MS_Bit)==0)
> {CorrValue <<= 1;
> Leading0++;
> if(CorrValue==0) return error2349;
> }
>
>i don't know if it is right...

It's the same but more clear. I also has a same feeling with you.

>
>> 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
>>
>
>
>