N
newsposter0123
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
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