Searching \ for '[PIC]: How to make 24 bits calculations with 16 bi' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'How to make 24 bits calculations with 16 bi'.

Exact match. Not showing close matches.
'[PIC]: How to make 24 bits calculations with 16 bi'
2001\09\28@200313 by

Hello,

I'm using CCS-PCM and I need to do this calculation:

/////////////////////////////////////////////////////////
byte a, b, c;    // byte variables are unsigned 8 bits
byte x, y, z;
word xx, yy;     // word variables are unsigned 16 bits
byte end_value;

xx = a * 6000 + b * 100 + c;
yy = x * 6000 + y * 100 + z;
end_value = yy * 255 / xx;
/////////////////////////////////////////////////////////

The largest possible value for xx and yy in this application is 599999
(a 24 bit value).

The variables a,b,c and x,y,z holds two time values as
minutes,seconds,cents.

The calculation is 24 bit, but the result is only 8 bit. Any ideas on
how can I make this using only 8 bits and 16 bits variables?

Thank you VERY much,

Brusque

-----------------------------------
Edson Brusque
Research and Development
C.I.Tronics Lighting Designers Ltda
(47) 323-2138
Blumenau  -  SC  -  Brazil
http://www.citronics.com.br
-----------------------------------

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

On Fri, 28 Sep 2001, Edson Brusque wrote:

{Quote hidden}

I suppose 32-bit variables are not an option?

If you want to simplify the expression, notice this approximation:

1/(M+N) = 1/M * (1 - N/M + (N/M)^2 -+ ...)

If you partition the denominator into two with M being the largest, then
you can convert the division by a big number into several divisions of
smaller numbers. The question is how do you partition the denominator?

First, rewrite xx:

xx = 6*a*10^3 + b*10^2 + 0 + c

In this form, you might even be able to implement a base ten long
division, but I don't recommend going that route.

If a>b, then it's a no-brainer: factor out the 6000*a term:

1/xx = 1/(6000*a) * ( 1 - ( b/(60*a) + c/(6000*a)) + ...)

If the term b/(60*a) < 255, then you can ignore it and the c/(6000*a)
terms (assuming of course a>0).

If a is zero or the term b/(60*a) is not less than 255, then you can
rewrite xx:

xx =  (6000*a + 6000*n) + (m*100) + c

where b is written as
b = 60*n + m

In other words, n<5 and m is between 0 and 59.

let a' = a+n then xx can be written

xx = 6000*a' + 100*m + c

1/xx = 1/(6000*a') * ( 1 - ( m/(60*a') + c/(6000*a')) + ...)

this guarantees that m/(60*a') is less than 1.

If a and b are zero, then all you have to do is divide by c.

It appears that the division can be reduced to (1/(6000*a')).

Scott

--

part 1 2357 bytes content-type:text/plain; charset=us-ascii (decoded 7bit)

Hi Edson,

I would say the simplest would be to use inline assembly, if the
compiler doesn't allow 32 bit variables (which kind of surprising!).
But it is possible to do a division function in C. It is less
efficient, but will work. I attached a short C-program which does
that on PC, but may need some modifications for CCS compiler.

It divides the time values directly, without conversion to binary. The
trick is to have special functions for a left shift and a subtraction.
This is similar to BCD operations, where you do some correction after
shift or subtraction. Subtraction is done once, so you may inline that
function. And shifts can also be moved in only one place. After that,
you can probably avoid using structures and pointers at all, to
improve the size and speed of PIC code in result.

Good luck!
Nikolai

---- Original Message ----
From: Edson Brusque <ebrusqueTERRA.COM.BR>
Sent: Friday, September 28, 2001 18:09:35
To: golovchenkomail.ru
Subj: [PIC]: How to make 24 bits calculations with 16 bits variables in              C???

{Quote hidden}

part 2 3368 bytes content-type:application/octet-stream; name="main.c"
(decode)

part 3 144 bytes
--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

According to the CCS doc the compiler has an int32 type. Is there a reason
for not using this?

Andy

{Original Message removed}
Sorry, I'm not familiar with the CCS-PCM compiler. If it has the 32
bit type, then great, no problem. Although, it seems to have a
downside - doing all those multiplications to convert time to 32 bit
integers and do the 32 bit division might be less efficient in
execution time, RAM, and ROM. So, I'm not really sure which method is
better. Anyway, it was an interesting problem! :)

Nikolai

Andy Shaw wrote:
> According to the CCS doc the compiler has an int32 type. Is there a reason
> for not using this?

> Andy

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

'[PIC]: How to make 24 bits calculations with 16 bi'
2001\10\01@105853 by
Hello Andy,

> According to the CCS doc the compiler has an int32 type. Is there a reason
> for not using this?

there's three reasons:

1) I'm using v2.734 and int32 is implemented in v3 only.

2) I'm not using v3 because it's still very buggy. My program is
very complex and I don't want to track down why it doesn't work
right now.

3) I have very few program space available.

Best regards,

Brusque

-----------------------------------
Edson Brusque
Research and Development
C.I.Tronics Lighting Designers Ltda
(47) 323-2138
Blumenau  -  SC  -  Brazil
http://www.citronics.com.br
-----------------------------------

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...