This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[g++] RFH: Is there a way to make gcc place global const class objects in .rodata ?


Hello,

Is there a way to place global const objects in .rodata, thus avoiding
construction at program start? 
The background is that I am trying to develop a fractional number class
template for more efficient numerics on targets that don't natively
support fast floating point (namely the arm-elf arm7tdmi targets).

The class looks very much like this:

/* START OF CODE EXAMPLE */
/// Class for implementing fractional numbers.
/// "basetype" should be some signed integer type.
/// We will shift the point "exp_m" bits to the left.
template<typename basetype, int exp_m> 
class fract {

  public: 
  // in this number we store the signed mantissa.
  // fixme: this should be a private member.
  basetype data;

  int exponent (void) {return exp_m;};

  fract<basetype, exp_m> & FRACT_AI operator= (const fract<basetype,
exp_m> &val)
  { data = val.data; return *this; };
  fract<basetype, exp_m> & FRACT_AI operator+= (const fract<basetype,
exp_m> &val)
  { data += val.data; return *this; };
  fract<basetype, exp_m> & FRACT_AI operator-= (const fract<basetype,
exp_m> &val)
  { data -= val.data; return *this; };

  fract<basetype, exp_m> FRACT_AI operator+ (const fract<basetype,
exp_m> &op)
  { return (fract<basetype, exp_m> (*this)) += op; }
  fract<basetype, exp_m> FRACT_AI operator- (const fract<basetype,
exp_m> &op)
  { return (fract<basetype, exp_m> (*this)) -= op; }

  /// conversion fract -> double
  FRACT_AI operator double (void)
  { return double (data) * pow (2.0, - exp_m); }

  /// constructor eating doubles 
  inline FRACT_AI __attribute__ ((always_inline)) fract<basetype, exp_m>
(const double & source)
    : data (basetype(source * pow (2.0, exp_m)))
  {};
};
/* END OF SAMPLE CODE */

What I would like to do is: defining objects like

static const fract<int32_t,28> interpolation_table [6] = 
{ -3.4,  -2.1, 0.5, 3.0, 5.0, -0.00234 };

presently such objects always end up in .bss and gcc initializes it
together with static objects. The code is correct, but inefficient.
Moreover it eats up precious ram memory, while the objects could readily
end up in the read-only flash. 

It would be very desireable if one could make g++ place it in .rodata
instead, thus removing any need for additional construction code.

After reading
http://gcc.gnu.org/ml/gcc/1998-08/msg00050.html
I come to the conclusion, that this should be possible in principle.

Any hint would be appreciated,

Bjoern.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]