Searching \ for '[PIC]: Any clever implementations of rounding out' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Any clever implementations of rounding out'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Any clever implementations of rounding out '
2001\04\03@084532 by Bob Ammerman

picon face
[warning: untested code]

I don't know if this is clever, but it is simple.

Assuming positive values:

int round(int value, int granularity)
{
   value += granularity >> 1;
   value /= granularity;
   return value * granularity;
}

If time isn't an issue and you want to avoid multiply/divide:

int round(int value, int granularity)
{
   int result = 0;
   value += granularity >> 1;
   while (value > granularity)
   {
       result += granularity;
       value -= granularity;
   }
   return result;
}

Or, similarly:

int round(int value, int granularity)
{
   int result = 0;
   value += granularity >> 1;

   for (;;)
   {
       value -= granularity;
       if (value <= 0)
           break;
       result += granularity;
   }
}

Or even:

int round(int value, int granularity)
{
   value += granularity >> 1;
   result = 0;
   while (result <= value)
       result += granularity;
   return result;
}

Conversion to assembly is an exercise for the user (or compiler) :-)

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

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


2001\04\04@052147 by Roman Black

flavicon
face
Kübek Tony wrote:

> Bob Ammerman wrote:
> >I don't know if this is clever, but it is simple.
> >
> >Assuming positive values:
> <snip>
> Well pretty clever :) didn't think of that myself,
> was trying an ( proved to be a tad hard ) 'extraction'
> of the ones and tens digits from the 24 bit value,
> took about 150'ish cycles, then I had to add the actual
> add/subtract bit. Anyway these would probably take
> less cycles. I'll try them.
> Do not want to use div/mul way to many cycles.
> ( for 24 bit inputs,outputs )


Tony, the best way is if you can keep rounding
to a binary mulitple.
Just add half of the rounding total,
then clear the low bits.

Example, to round to the nearest 256;
add 128 to the number,
clear the bottom 8 bits.
-Roman

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


2001\04\04@073702 by Bob Ammerman

picon face
----- Original Message -----
From: Kübek Tony <spam_OUTtony.kubekTakeThisOuTspamFLINTAB.COM>
To: <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>
Sent: Wednesday, April 04, 2001 4:32 AM
Subject: Re: [PIC]: Any clever implementations of rounding out there ?


Hi,

Bob Ammerman wrote:
>I don't know if this is clever, but it is simple.
>
>Assuming positive values:
<snip>
Well pretty clever :) didn't think of that myself,
was trying an ( proved to be a tad hard ) 'extraction'
of the ones and tens digits from the 24 bit value,
took about 150'ish cycles, then I had to add the actual
add/subtract bit. Anyway these would probably take
less cycles. I'll try them.
Do not want to use div/mul way to many cycles.
( for 24 bit inputs,outputs )

Thanks,

/Tony

Tony,

Watch out!!!

The div/mul free techniques can be VERY slow because it is basically
simulating the division via repeated subtraction. As my original comment
said: if cycles are important, then these techniques wont work.

Ouch!

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)





Tony Kübek, Flintab AB
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: tony.kubekspamKILLspamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

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

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