Re: [Vxl-maintainers] vnl and rounding

 Re: [Vxl-maintainers] vnl and rounding From: Peter Vanroose - 2008-02-06 11:23:11 ```> int() rounds towards zero. > I need a function that rounds towards -inf. If you know that all values are not smaller than (say) -50000, first add (and later subtract again) a suitable power of 2: inline long int my_floor(double x) { return (long int)(x+(1<<16))-(1<<16); } The "subtracting a power of 2" implementation on integers roughly swaps all bits. Don't know if this is faster than subtracting any other number. Also don't know whether this makes a difference on doubles. -- Peter. -- ___________________________________________________ Sök efter kärleken! Hitta din tvillingsjäl på Yahoo! Dejting: http://se.meetic.yahoo.net/index.php?mtcmk=148757 ```

 [Vxl-maintainers] vnl and rounding From: Amitha Perera - 2008-01-31 15:06:04 ```Folks, We've had a couple of messages on this list about vnl and rounding: http://sourceforge.net/mailarchive/message.php?msg_id=44C13A99.80709%40kitware.com http://sourceforge.net/mailarchive/message.php?msg_id=470E5E5A.9020605%40kitware.com The issue is about what the halfway cases round to, and about how fast one can do it. Right now, the comment on vnl_math_rnd says "0.5 rounds up", but the assembly implementation doesn't agree on Visual C++. I suggest the following as a permanent solution: vnl_math_rnd rounds away from zero (same as the C99 function lround). -13.5 -> -14 -12.5 -> -13 12.5 -> 13 13.5 -> 14 A new function vnl_math_int[*] rounds to an integer as efficiently as possible, with halfway cases being converted in an unspecified but consistent way. (Kinda like lrint.) Folks can use this when they are just converting to an integer, and aren't sensitive to the rounding on the halfway cases. And, of course, good old vcl_floor, vcl_ceil, and int() exist for the cases where you really care about the rounding. (Cast to int is guaranteed to truncate. -1.5 -> -1, 1.5 -> 1.) Thoughts? Amitha. [*] Maybe name it vnl_math_fast_rnd instead? ```
 Re: [Vxl-maintainers] vnl and rounding From: Ian Scott - 2008-02-01 09:36:54 ```I like the name vnl_math_fast_round. BTW: Does anyone know of a fast implementation of int vnl_math_floor(double x); Ian. Amitha Perera wrote: > Folks, > > We've had a couple of messages on this list about vnl and rounding: > http://sourceforge.net/mailarchive/message.php?msg_id=44C13A99.80709%40kitware.com > http://sourceforge.net/mailarchive/message.php?msg_id=470E5E5A.9020605%40kitware.com > > The issue is about what the halfway cases round to, and about how fast > one can do it. Right now, the comment on vnl_math_rnd says "0.5 rounds > up", but the assembly implementation doesn't agree on Visual C++. I > suggest the following as a permanent solution: > > vnl_math_rnd rounds away from zero (same as the C99 function lround). > -13.5 -> -14 > -12.5 -> -13 > 12.5 -> 13 > 13.5 -> 14 > > A new function vnl_math_int[*] rounds to an integer as efficiently as > possible, with halfway cases being converted in an unspecified but > consistent way. (Kinda like lrint.) Folks can use this when they are > just converting to an integer, and aren't sensitive to the rounding on > the halfway cases. > > And, of course, good old vcl_floor, vcl_ceil, and int() exist for the > cases where you really care about the rounding. (Cast to int is > guaranteed to truncate. -1.5 -> -1, 1.5 -> 1.) > > Thoughts? > > Amitha. > > [*] Maybe name it vnl_math_fast_rnd instead? > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Vxl-maintainers mailing list > Vxl-maintainers@... > https://lists.sourceforge.net/lists/listinfo/vxl-maintainers ```
 Re: [Vxl-maintainers] vnl and rounding From: Peter Vanroose - 2008-02-02 15:42:08 ```> Right now, the comment on vnl_math_rnd says "0.5 rounds up" > ... > I suggest the following as a permanent solution: > "vnl_math_rnd rounds away from zero" Of course. That's how it was always meant to be. "Rounds up" is a misformulation indeed, but correct for 0.5 (so the formulation was correct but incomplete). > A new function vnl_math_int[*] rounds to an integer as > efficiently as possible, with halfway cases being converted > in an unspecified but consistent way. OK, agreed. The name "fast_rnd" in indeed probably better, since the "int" seems to indicate "truncate" rather than "round". -- Peter. -- __________________________________________________________ Ta semester! - sök efter resor hos Yahoo! Shopping. Jämför pris på flygbiljetter och hotellrum här: http://shopping.yahoo.se/c-169901-resor-biljetter.html?partnerId=96914052 ```
 Re: [Vxl-maintainers] vnl and rounding From: Amitha Perera - 2008-02-05 23:49:43 ```Ian Scott wrote: > BTW: Does anyone know of a fast implementation of > > int vnl_math_floor(double x); Isn't that called "int()"? From my limited knowledge, there is no fast way to do it. You could set the floating point rounding flags to round to -ve inf, and then use the rounding functions. Apparently this is the code that gcc emits for "int()", with additional code to restore the flags afterward. Apparently setting and restoring the flags is the slow part, because it flushes the FP pipeline. Amitha. ```
 Re: [Vxl-maintainers] vnl and rounding From: Ian Scott - 2008-02-06 10:04:59 ```AFAIK int() rounds towards zero. I need a function that rounds towards -inf. It is used heavily in sampling from and reconstructing images, which is the main bottleneck in many of our applications. I've know about the pipeline problem, but have never been fully convinced that there isn't another way, especially since all the discussion of the pipeline problem focuses on int() rather than my function. Thanks anyway. Ian. Amitha Perera wrote: > Ian Scott wrote: >> BTW: Does anyone know of a fast implementation of >> >> int vnl_math_floor(double x); > > Isn't that called "int()"? From my limited knowledge, there is no fast > way to do it. You could set the floating point rounding flags to round > to -ve inf, and then use the rounding functions. Apparently this is the > code that gcc emits for "int()", with additional code to restore the > flags afterward. Apparently setting and restoring the flags is the slow > part, because it flushes the FP pipeline. > > Amitha. ```
 Re: [Vxl-maintainers] vnl and rounding From: Peter Vanroose - 2008-02-06 11:23:11 ```> int() rounds towards zero. > I need a function that rounds towards -inf. If you know that all values are not smaller than (say) -50000, first add (and later subtract again) a suitable power of 2: inline long int my_floor(double x) { return (long int)(x+(1<<16))-(1<<16); } The "subtracting a power of 2" implementation on integers roughly swaps all bits. Don't know if this is faster than subtracting any other number. Also don't know whether this makes a difference on doubles. -- Peter. -- ___________________________________________________ Sök efter kärleken! Hitta din tvillingsjäl på Yahoo! Dejting: http://se.meetic.yahoo.net/index.php?mtcmk=148757 ```