Initializing constants



The code block below initialized a r/w variable (usually .bss) to the
value of pi. One, of many, problem is any linked compilation unit may
change the global variable. Adjusting

// rodata
const long double const_pi=0.0;

lines to

// rodata
const long double const_pi=init_ldbl_pi();

would add additional protections, but is not legal C, and, rightly so,
fails on GCC/i386.

Do any C standards define a means to initalize constants to values
obtained from hardware, or does the total number of constants and/or
cross-compiling prohibit it completely (although, when cross-compiling,
the compiler could create a value using the resources available i.e.
emulation)?

I'm hoping something like this is possible...

// rodata
const long double const_pi= LDBL_PI;

When compiling on i386 systems for i386 targets and using the
coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction,
and when compiling on non i386 systems for i386 targets, an emulation
value is calculated from available resources.

BEGIN CODE
#include <stdio.h>

// prototype
long double init_ldbl_pi();

// rodata
const long double const_pi=0.0;

// global function
long double init_ldbl_pi() {
long double ldbl=3.14;

#if (defined __GNUC__) && (defined __i386__)
asm("fldpi":::"st");
asm("fstpt %0":"=m" (ldbl)::"st");
#endif
return ldbl;
}

// main function
int main() {
long double ldbl_pi=init_ldbl_pi();
printf("%Le\n", ldbl_pi);
return 0;
}
END CODE

.



Relevant Pages

  • Re: Initializing constants
    ... const long double const_pi=0.0; ... When compiling on i386 systems for i386 targets and using the ... you must be sure to call the initialization function ...
    (comp.lang.c)
  • Re: Initializing constants
    ... When compiling on i386 systems for i386 targets and using the ... coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction, ... You must understand the Tao before transcending structure." ...
    (comp.lang.c)
  • Re: Initializing constants
    ... When compiling on i386 systems for i386 targets and using the ... coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction, ...
    (comp.lang.c)
  • Re: Compilation error without stating any reason
    ... Windows. ... While compiling one of the targets it stops on an ...
    (comp.unix.programmer)