Compiler optimization snafu???
From: Charles Sullivan (cwsulliv_at_triad.rr.com)
Date: 11/28/05
- Next message: Tauno Voipio: "Re: Compiler optimization snafu???"
- Previous message: Kasper Dupont: "Re: notifying change to shared memory"
- Next in thread: Tauno Voipio: "Re: Compiler optimization snafu???"
- Reply: Tauno Voipio: "Re: Compiler optimization snafu???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 28 Nov 2005 19:21:08 GMT
I need to calibrate a timing loop for a delay which is substantially
shorter than the resolution available with nanosleep().
Consider the short test program "loopcal.c" using alarm() which
follows. It works if compiled and executed like this:
$ gcc -Wall loopcal.c -o loopcal
$ ./loopcal
Beginning 1 second calibration.
Alarm flag = 0, Loop count = 61702367
But if compiled with the -O switch:
$ gcc -O -Wall loopcal.c -o loopcal
$ ./loopcal
Beginning 1 second calibration.
Alarm flag = 0, Loop count = 0
Calibration failed.
(After grinding away for about 20 seconds. It looks like the
compiler optimizer decided that the alarmflag could be ignored.)
Can anyone explain how to overcome this obstacle?
(The calibrate function is intended to be included in a larger
program which is compiled with the -O switch and which I'm
reluctant to change.)
Regards,
Charles Sullivan
================== loopcal.c ===================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
/*--------------------------+
| Timing loop calibration. |
+--------------------------*/
static int alarmflag;
static void alarm_isr ()
{
alarmflag = 0;
return;
}
unsigned long loop_calibrate ( void )
{
unsigned long count;
alarmflag = 1;
count = 0;
signal(SIGALRM, alarm_isr);
alarm(1);
while ( alarmflag && ++count );
alarm(0);
return count;
}
int main ( void )
{
unsigned long count;
printf("Beginning 1 second calibration.\n");
count = loop_calibrate();
printf("Alarm flag = %d, Loop count = %lu\n",
alarmflag, count);
if ( count == 0 )
printf("Calibration failed.\n");
return 0;
}
================================================
- Next message: Tauno Voipio: "Re: Compiler optimization snafu???"
- Previous message: Kasper Dupont: "Re: notifying change to shared memory"
- Next in thread: Tauno Voipio: "Re: Compiler optimization snafu???"
- Reply: Tauno Voipio: "Re: Compiler optimization snafu???"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|