| Author |
Message |
Chris Bore
Guest
|
Posted:
Thu Dec 29, 2005 12:43 am Post subject:
Re: C values that are not known at compile time |
|
|
Thanks.
The compiler I am working with is in fact almost omniscient, so it is
extremely hard to find ways to calculate data that the compiler cannot
determine at compile time.
Indeed, if I could read in data (froma file or a device) then that
would do, but I am running these as very small example programs for
teaching and illustrative purposes, and in simulation with profiling
that can be slow: so I'd like to find a simple way that does not add
lots of code.
Making the variables volatile usually works (though not with all
compilers, wrongly I think..) but it also forces compiler behaviour
that is different - the values I want are only volatile at one point,
when they are first read, and not at all times.
Chris |
|
| Back to top |
|
 |
Ron N.
Guest
|
Posted:
Thu Dec 29, 2005 1:00 am Post subject:
Re: C values that are not known at compile time |
|
|
Chris Bore wrote:
| Quote: | The compiler I am working with is in fact almost omniscient, so it is
extremely hard to find ways to calculate data that the compiler cannot
determine at compile time.
Indeed, if I could read in data (froma file or a device) then that
would do, but I am running these as very small example programs for
teaching and illustrative purposes, and in simulation with profiling
that can be slow: so I'd like to find a simple way that does not add
lots of code.
Making the variables volatile usually works (though not with all
compilers, wrongly I think..) but it also forces compiler behaviour
that is different - the values I want are only volatile at one point,
when they are first read, and not at all times.
|
If you assign regular variables from a volatile variables, then
it would probably be quite difficult for an optimizer to turn those
variables into constants after those assignments, which might
only need to happen once near the beginning of your micro
benchmarks. Then just use regular variables.
IMHO. YMMV.
--
rhn A.T nicholson d.O.t C-o-M |
|
| Back to top |
|
 |
Ron N.
Guest
|
Posted:
Thu Dec 29, 2005 1:16 am Post subject:
Re: C values that are not known at compile time |
|
|
Randy Yates wrote:
| Quote: | I think I see what you're after now, and I would think you can solve the
problem by specifying the associated variables with the "volatile"
keyword, but you stated this won't work:
Typically, I make such constant values be volatile, which forces the
compiler to load them. But volatile also forces other different
behavior so it changes the way the program is compiled.
I'd like to know what "other different behavior" this causes.
|
In an attempt to follow the C language spec, the compiler might
not use a copy of a variable already in a register, recently fetched
but not in an ISA visible register, already in one of the caches, or
if fetchable out-of-order; all of which might result in a slower code
sequence on a modern CPU (with lots of registers, register renaming,
multiple levels of data caches, and superscalar out-of-order
instruction issue, etc., etc. Depends on the compiler, optimization
level, compiler flags, CPU cache invalidate/bypass capabilities,
system memory/IO/bus hierarchy, etc.
IMHO. YMMV.
--
rhn A.T nicholson d.O.t C-o-M |
|
| Back to top |
|
 |
Randy Yates
Guest
|
Posted:
Thu Dec 29, 2005 1:16 am Post subject:
Re: C values that are not known at compile time |
|
|
"Ron N." <rhnlogic@yahoo.com> writes:
| Quote: | Randy Yates wrote:
I think I see what you're after now, and I would think you can solve the
problem by specifying the associated variables with the "volatile"
keyword, but you stated this won't work:
Typically, I make such constant values be volatile, which forces the
compiler to load them. But volatile also forces other different
behavior so it changes the way the program is compiled.
I'd like to know what "other different behavior" this causes.
In an attempt to follow the C language spec, the compiler might
not use a copy of a variable already in a register, recently fetched
but not in an ISA visible register, already in one of the caches, or
if fetchable out-of-order; all of which might result in a slower code
sequence on a modern CPU (with lots of registers, register renaming,
multiple levels of data caches, and superscalar out-of-order
instruction issue, etc., etc. Depends on the compiler, optimization
level, compiler flags, CPU cache invalidate/bypass capabilities,
system memory/IO/bus hierarchy, etc.
|
That sounds impressive, Ron.
I follow your first point. What's an "ISA visible register"?
Regarding the comments on cache, I don't see how a compiler is going
to possibly know about such things. For example, the TI TMS320DM642
has 512kb of fast, internal memory that can be partitioned between
plain SRAM and L2 cache by other mechanisms the compiler would have no
idea about.
--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%% 919-577-9882 %
%%%% <yates@ieee.org> % 'Shangri-La', *A New World Record*, ELO
http://home.earthlink.net/~yatescr |
|
| Back to top |
|
 |
Randy Yates
Guest
|
Posted:
Thu Dec 29, 2005 1:16 am Post subject:
Re: C values that are not known at compile time |
|
|
Randy Yates <yates@ieee.org> writes:
Sorry, that should be "512KB."
--
% Randy Yates % "The dreamer, the unwoken fool -
%% Fuquay-Varina, NC % in dreams, no pain will kiss the brow..."
%%% 919-577-9882 %
%%%% <yates@ieee.org> % 'Eldorado Overture', *Eldorado*, ELO
http://home.earthlink.net/~yatescr |
|
| Back to top |
|
 |
Randy Yates
Guest
|
Posted:
Thu Dec 29, 2005 1:16 am Post subject:
Re: C values that are not known at compile time |
|
|
"Chris Bore" <chris.bore@gmail.com> writes:
| Quote: | Is the value indeed not known at compile time? If it is not, and
the compiler is assuming it is, I'd like to see the code. If it is,
and you'd like the compiler to assume it isn't, I'd ask why.
The value is known, but only because the program is in this sense
artificially simple.
My problem is, how to retain the simplicity of example but to simulate
the non-constant-ness.
|
Hi Chris,
I think I see what you're after now, and I would think you can solve the
problem by specifying the associated variables with the "volatile"
keyword, but you stated this won't work:
Typically, I make such constant values be volatile, which forces the
compiler to load them. But volatile also forces other different
behavior so it changes the way the program is compiled.
I'd like to know what "other different behavior" this causes.
The other obvious way is to use a different file for the
variables you want "volatile." For example, instead of
#include <stdint.h>
uint8_t x = 6;
void ExampleFunction(void)
{
printf("x^2 = %d\n", x*x);
}
you could write
#include <stdint.h>
extern uint8_t x;
void ExampleFunction(void)
{
printf("x^2 = %d\n", x*x);
}
in one file and then in another file write
#include <stdint.h>
uint8_t x = 6;
and then link the two together to produce the final program.
--
% Randy Yates % "So now it's getting late,
%% Fuquay-Varina, NC % and those who hesitate
%%% 919-577-9882 % got no one..."
%%%% <yates@ieee.org> % 'Waterfall', *Face The Music*, ELO
http://home.earthlink.net/~yatescr |
|
| Back to top |
|
 |
Michel Rouzic
Guest
|
Posted:
Thu Dec 29, 2005 7:19 am Post subject:
Re: C values that are not known at compile time |
|
|
Chris Bore wrote:
| Quote: | My interpretation of Chris' post is that he wants to test some computational
code with input parameters that de facto are known at compile time for his
test routine, since he defined them. The parameters are not known at
compile time for "the real program." I can understand that things change in
the two cases, depeding on how the test is implemented. I am very curious
about an application where such details become as important as they apparently are in this case.
That is a clear statement of what I intend, thank you.
If I could 'read in' real values from the 'outside world' then I would
have a fair test
(although I accept the results still depend on the test values).
But the code to read in values introduces libraries I do not want, and
makes the code larger and slower
(I am running in simulation). Hence the desire for some simple 'this is
what you might have got if you had read it in'.
Chris
|
umm.. i hope I won't be off topic, basically, you want to use values to
compute, values that you predetermined, but that are not supposed to be
known at compile time.
it may seem a lil bit weird, but, maybe you should include time.h
(provided that you can allow yourself to do that), and use the result
of time(NULL), which gives you a value that can't be known at compile
time, and modify it in a way that gets you to the values you want (you
can get to any value you desire with it, although you'll never get the
same value from that function, thus it cant be possibly known at
compile time)
or then if you can use a PRNG, and if you dont wanna take one from a
library, make your own, it's very simple. You'd still have to use
time.h tho
I hope I helped even a lil bit |
|
| Back to top |
|
 |
Chris Bore
Guest
|
Posted:
Thu Dec 29, 2005 5:16 pm Post subject:
Re: C values that are not known at compile time |
|
|
| Quote: | I follow your first point. What's an "ISA visible register"?
|
I think he means a register that is visible to the assembly programmer
(ISA being Instruction Set Architecture)?
| Quote: | Regarding the comments on cache, I don't see how a compiler is going
to possibly know about such things. For example, the TI TMS320DM642
has 512kb of fast, internal memory that can be partitioned between
plain SRAM and L2 cache by other mechanisms the compiler would have no
i>dea about. |
Cache-aware compilers would be extremely powerful in many applications.
IMEC have done a lot of work on code optimizations to use cache better,
and I think these will be included in compilers soon. It's in fact one
of the
motivations behind my original question, as I am interested in code and
data
transformations that make better use of cache.
Chris |
|
| Back to top |
|
 |
Randy Yates
Guest
|
Posted:
Thu Dec 29, 2005 5:16 pm Post subject:
Re: C values that are not known at compile time |
|
|
Randy Yates <yates@ieee.org> writes:
| Quote: | Randy Yates <yates@ieee.org> writes:
512kb
Sorry, that should be "512KB."
|
Sorry, that should be "256KB." !?!
--
% Randy Yates % "She's sweet on Wagner-I think she'd die for Beethoven.
%% Fuquay-Varina, NC % She love the way Puccini lays down a tune, and
%%% 919-577-9882 % Verdi's always creepin' from her room."
%%%% <yates@ieee.org> % "Rockaria", *A New World Record*, ELO
http://home.earthlink.net/~yatescr |
|
| Back to top |
|
 |
Chris Bore
Guest
|
Posted:
Thu Dec 29, 2005 5:16 pm Post subject:
Re: C values that are not known at compile time |
|
|
Thanks, Randy.
| Quote: | use a different file for the variables you want "volatile."
|
That is so beautifully obvious that I feel a fool to have missed it.
Chris |
|
| Back to top |
|
 |
Randy Yates
Guest
|
Posted:
Thu Dec 29, 2005 5:16 pm Post subject:
Re: C values that are not known at compile time |
|
|
"Chris Bore" <chris.bore@gmail.com> writes:
| Quote: | Thanks, Randy.
use a different file for the variables you want "volatile."
That is so beautifully obvious that I feel a fool to have missed it.
|
If I had a nickel for every time I missed something obvious...
--
% Randy Yates % "Remember the good old 1980's, when
%% Fuquay-Varina, NC % things were so uncomplicated?"
%%% 919-577-9882 % 'Ticket To The Moon'
%%%% <yates@ieee.org> % *Time*, Electric Light Orchestra
http://home.earthlink.net/~yatescr |
|
| Back to top |
|
 |
Ron N.
Guest
|
Posted:
Fri Dec 30, 2005 1:07 am Post subject:
Re: C values that are not known at compile time |
|
|
Chris Bore wrote:
| Quote: | I follow your first point. What's an "ISA visible register"?
I think he means a register that is visible to the assembly programmer
(ISA being Instruction Set Architecture)?
|
Yes. Modern superscalar out-of-order CPU implementations often have
a lot more result registers than can be referenced by the instruction
set (ISA). Read up on register renaming.
IMHO. YMMV.
--
rhn A.T nicholson d.O.t C-o-M |
|
| Back to top |
|
 |
|
|
|
|