How much error can you stand? 125 is pretty close to 128. :-) -----Original Message----- >From: "William \"Chops\" Westfield" >Sent: Apr 29, 2009 3:14 AM >To: "Microcontroller discussion list - Public." >Subject: [AVR] divide 48bit number by (constant) 125 ? > >I want to calculate: > > (ticks * ) / 1000 > >to return the number of milliseconds elapsed based on a count >maintained by a timer ISR. microsecondsPerTick is a nice constant >number like 1024 or 2048, so the multiplication becomes a shift, and >factoring out twos I get: > > (ticks * (microsecondsPerTick/8)) / 125 > >"ticks" is at least 40 bits, however, and the multiplication MUST NOT >OVERFLOW (been there, trying to fix that!), so I'm looking at at at >least a 48bit intermediate value. > >It would be nice if the code were in C. Avr gcc, in particular. >There are 8, 16, 32, and even 64 bit math functions available, but of >course they're *C*, so they don't let you actually do mixed-sized >math, nor are they likely to optimized > >The code is not particularly time critical, but it would be nice if it >were as small and fast as possible (using 64bit long longs seems >excessive.) > >Thoughts? Any pointers to mixed-size C math functions in general? In >principle, I think I understand how to do this, but ... Is there a >way to take advantage of the constant? Of 125 in particular? (lots >of one bits there...) > >Thanks >Bill W > >-- >http://www.piclist.com PIC/SX FAQ & list archive >View/change your membership options at >http://mailman.mit.edu/mailman/listinfo/piclist -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist