[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] C bit: modulo and divu.w



> To clear things up a bit:

I guess that's needed.

> 1) divu.w is an instruction that divides a LONG and NOT A WORD. This results

Naturally.
I got confused by the reasoning about the remainder being smaller than the
divisor. I don't think I got anything wrong with my explanation as to why
that doesn't matter, though.

> in a quotient and a restvalue, which must both be words.
> 2) divu.w can only correctly divide longs when quotient < 65536!!!!

That's of course quite true.
Normally, I do remember that...

> anyway.. I would recode the original code:

Certainly a good idea.

> uptime++;
> uptimetick += 200;
> if (uptime % 5) return;

vs

> uptimetick+=200;
> if (uptimetick++ == 5)
> {
> 	uptimetick = 0;
> 	return;
> }
>
> which ofcourse gets rid of the costly modulo operation alltoghether.

Except of course that it doesn't do nearly the same thing.  ;-)

The last two instances of 'uptimetick' in your new suggestion should,
naturally, be 'uptime'.
Even then, this is actually a 'modulo 6 operation', since you put the
++ after 'uptime', and you were supposed to return every time _except_
when 'uptime % 5 == 0'. That is, the code after this should execute
one out of five times.

To make it correct (and perhaps a bit faster since the subtraction
does an implicit compare with zero), try:

uptimetick += 200;
if (--uptime)
  return;
else
  uptime = 5;

> okay.. maybe I should stop writing now =)

I guess I should too...
It wouldn't surprise me if I managed to get something wrong here.  ;-)

-- 
  Chalmers University   | Why are these |  e-mail:   rand@cd.chalmers.se
     of Technology      |  .signatures  |            johan@rand.thn.htu.se
                        | so hard to do |  WWW/ftp:  rand.thn.htu.se
   Gothenburg, Sweden   |     well?     |            (MGIFv5, QLem, BAD MOOD)