From c28dc2bb13ea2d2f278533533158d563c2adfa38 Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Wed, 25 Apr 2012 09:12:25 -0500 Subject: [PATCH] I was disappointed when I disassembled the code using the KiROUND() inline function solution to see that it was not pre-computing constants when used in static initializers. So we have two use cases, and need two solutions to be optimal, since the compiler is too stupid to do the right thing. I think we need something else for compile time computable constants, to be optimal in both use cases. There is quite a bit of code savings by using a macro for that situation from my testing. To fully capitalize on this, we need to go back and make Mm2mils() and what not macros also, or have MACRO versions of them too. --- TODO.txt | 2 +- include/common.h | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/TODO.txt b/TODO.txt index 14e4022afc..3e842a8f00 100644 --- a/TODO.txt +++ b/TODO.txt @@ -63,7 +63,7 @@ Dick's Peronal TODO Items (Last Update: 24-April-2012) viewport within the virtual IU space and also a change in size of the virtual IU space. Once this happens, you can end up thinking there are problems in functions like EDA_DRAW_PANE::DrawCrossHair(), but this may be an artifact - of having travelled outside a limited virtual IU space. + of having traveled outside a limited virtual IU space. 2) Check that the new load visibility BOARD settings is properly setting the toolbar diff --git a/include/common.h b/include/common.h index e9d7274255..ff33adfafa 100644 --- a/include/common.h +++ b/include/common.h @@ -127,11 +127,24 @@ enum pseudokeys { * In Debug build an assert fires if will not fit into an int. */ -#if defined( DEBUG ) +#if !defined( DEBUG ) -// DEBUG: a macro to capture line and file, then calls this inline +/// KiROUND: a function so v is not evaluated twice. Unfortunately, compiler +/// is unable to pre-compute constants using this. +static inline int KiROUND( double v ) +{ + return int( v < 0 ? v - 0.5 : v + 0.5 ); +} -static inline int KiRound( double v, int line, const char* filename ) +/// KIROUND: a macro so compiler can pre-compute constants. Use this with compile +/// time constants rather than the inline function above. +#define KIROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) + +#else + +// DEBUG: KiROUND() is a macro to capture line and file, then calls this inline + +static inline int kiRound_( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) @@ -145,13 +158,10 @@ static inline int KiRound( double v, int line, const char* filename ) return int( v ); } -#define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) +#define KiROUND( v ) kiRound_( v, __LINE__, __FILE__ ) -#else - -// RELEASE: a macro so compile can pre-compute constants. - -#define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) +// in Debug build, use the overflow catcher since code size is immaterial +#define KIROUND( v ) KiROUND( v ) #endif