[C++] is it possible to initialize static class objects by use of constant tables instead of explicit constructor calls?

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

 



Hi,

1.) Question

Is there a way to tell gcc to generate static initialization tables for static 
class objects instead of generating tons of constructor code?

2.) Background of the question

I am trying to write efficient class definitions for fractional numerics that 
I plan to contribute to the AVR-libc project. Main objective is to circumvent 
the use of floating point emulation for slow targets with little available 
memory. I am presently facing a serious inefficiency when trying to 
initialize static class objects. 

When using standard C++ types, e.g. when defining a global variable

uint16_t array[3] = {3,1,4};

one is used to get an array that is placed in a section that is initialized on 
program-startup. In particular, g++ does not generate additional application 
code for initializing the array to it's default value (except for one single 
startup sequence for all of the initialization data).
If you are dealing with class objects, e.g. a class similar to

class ufrac16_c {

[...]
	operator double ()
	{ 
	  return double(mantissa) * (1.0/65536.0);
	}
[...]

	ufrac16_c:ufrac16_c (double initialization_value) 
	__attribute__ ((always_inline) : 
		mantissa (initialization_value * 65536.0)
	{}

[...]

private:
  uint16_t mantissa;

};

(that is meant to store fractional numbers between [0 ... 1[ ) and when 
defining an initialized global variable

ufrac16_c a[3] = {0.5, 0.25, 65535.0/65536.0}

You will end up with a lot of explicit calls to individual constructors 
looking similar to

ldi r24,lo8(-32768)
ldi r25,hi8(-32768)
sts a+0,r24
sts a+1,r25

ldi r24,lo8(16384)
ldi r25,hi8(16384)
sts a+2,r24
sts a+3,r25

ldi r24,lo8(-1)
ldi r25,hi8(-1)
sts a+4,r24
sts a+5,r25

, so that it is not *really* useful to initialize static data this way. 
Admittedly, code is correct and one might consider using it for arrays with 3 
elements. But one would certainly stop using it for index numbers exceeding 
100, ...

3.) Question repeated more comprehensively

So there again is my question: Is there a way to tell gcc to generate static 
initialization tables for static class objects instead of generating tons of 
constructor code in case that all of the constructors have empty function 
bodies?

Thank you in advance,
Yours,

Björn

P.S.: 

I was in doubt whether the topic is suitable for "gcc-help" or rather "gcc" 
itself. 


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux