Short code to determine compiler's

mcdonald at uxe.cso.uiuc.edu mcdonald at uxe.cso.uiuc.edu
Sun Jul 23 09:03:01 AEST 1989


>>
>>... the way to find out how many registers the compiler will
>>use on your code is to compile your code and count how many registers
>>were used.  This is completely reliable, although not necessarily
>>repeatable.  It is also likely to be a useless statistic.
>>-- 

Yes, that's true.

>All true.  For amusement's sake though, here is a C program that finds
>out what register variables one can have by looking at their effect on
>execution time, which presumably is why one might want to know in the
>first place.  The following example works with gcc and cc on Sun
>3's and 4's running SunOS 3.5, and should work on most UNIX machines
>with little change.   As is frequently noted, timing routines are not
>very portable across OS's; sorry about that.

Except that his program was truly hopelessly non-portable. Here is
one that, I sincerely hope, is 100%  portable, unless you have on
obsolete compiler. It is not, unfortunately, 100% going to work
right on multitasking machines. There you will have to either go
single user and/or run it lots of times and take the maximum 
iterations.


#include <time.h>
#include <stdio.h>

#define T(A)      t = time(NULL); \
    while (t == time(NULL));\
    t = time(NULL);\
    for(i = 0; t == time(NULL); i++)\
       for(A = 0; A < 1000; A++) j |= 1; /* do something in loop */ \
    printf("register %s did %d loops\n", #A , i);

/* making these static and volatile will hopefully prevent putting
them in registers */

    volatile unsigned i,j;
    volatile time_t t;

int
main()
{
    register unsigned r01, r02, r03, r04, r05, r06, r07, r08, r09, r10,
                      r11, r12, r13, r14, r15, r16, r17, r18, r19, r20;

    if( time(NULL) == (time_t)-1) {
        puts("Sorry, the time function returned a -1.");
        exit(1);
    }


    T(r01);
    T(r02);
    T(r03);
    T(r04);
    T(r05);
    T(r06);
    T(r07);
    T(r08);
    T(r09);
    T(r10);
    T(r11);
    T(r12);
    T(r13);
    T(r14);
    T(r15);
    T(r16);
    T(r17);
    T(r18);
    T(r19);
    T(r20);

} 

It correctly reports two registers on my IBM PC.

Doug McDonald (mcdonald at uxe.cso.uiuc.edu)



More information about the Comp.lang.c mailing list