Advertisement

Does the CPU or compiler do this conversion?

Started by November 16, 2000 10:09 PM
7 comments, last by Densun 24 years, 2 months ago
Let''s say I wrote the following statement in a program: i = 5.0/5; The 5 will get converted into a floating point number, but is that conversion done while running the program or when compiling it? What about with enclosed parantheses: i = ((5*6)/(4+9)); I know the compiler puts the code into assembly format which does only one action per statement, so I would think the compiler would sort that out. --- my website
I''m sure if your compiler isn''t absolutely worthless, it will do those calculations before it compiles the code.
Matt Teiken
Advertisement
DOH! I just realized I presented the problem in a way that made my point unclear. Here''s the statements rewritten:

float i, f;

i = f/5;

and,

int i, a, b, c, d;

i = ((a*b)/(c+d));

I was not meaning the actual computation of the numbers, just how the CPU would receive the code. Would it get it in the above forms, or like this after compilation (well, not quite like this):

i = f/5.0;

and,

i = a*b;
i /= (c+d);

---
my website
ok well I didn''t write the book on optimization or any thing but I think it would be a little bit of both.... from what I''ve heard about pentiums( atlest the first ones)... is that basicaly its 2 processers blended into a single chip.... heh sorta... one handles integeral math and the other floating point... and the could act semi-independent of each other... one could be scanning ahead for more intructions dealing with eather floating point or integeral math while the other was hashing out another... or like say:

double d;
int i;

d=3.9;
i=7;
d*=4.5;
i+=9;

the floating point would start working on the first line and the integeral would start working on the second... lets say that the integeral takes less time... so instead of sitting around twitiling its thumbs the integeral math proc. grabs the 4th line... and starts working on it... until the result of one operation depends on the result of another operation that the other sub-processer hasn''t finnished yet like say:

5th line: i=i/d;

ok to figure out what i/d equals the value of d must be known... well the fpu hasn''t got to that point yet so it waits... anyways some optimizations are done by the processer at run time others like getting the instructions in an order that will alow for this game of catch-up and still output the correct results requiers some help by the compiler... and the programmer =)... ummm yea the a*b/c+d thing would probably be the same as
i=a*b;
c=c+d;
i=i/c;
or whatever... higher level lanuages like c/c++ abstract you to a point from the fact that the cpu can only do one thing at a time by letting you put multiple statements on a single line... so other than easy of programming theres no diference between the 2 snipits... the produce the same result and probably the same machine code...



Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Jesus Christ...I don''t know who the Great M88&^%$#@@ is who is posting here or any of his circus flying cousins, but optimization is primarily a compiler function which can be set. Hardware pipelining is only discretionary about flushing instructions not needed (I think). Multiple CPU capabilities can distribute applications for better performance, which is different from optimization.
I''m not sure if I made myself clear enough in the second post either, but what I''m wanting to know does not have to do with math. I''m just wondering if the compiler seperates any nested parantheses into different statements, or if the CPU is given that task.

---
my website
Advertisement
It doesn''t do any parens. What is does is performs the individual operations in the order required to acheive the result which may require saving intermediate results. Basically just like you would have to with a calculator that doesn''t support parens. It will effectively remove any unneeded parens. So (((((a*b))))) is the same as a*b.
Keys to success: Ability, ambition and opportunity.
To put it simple the compiler splits it into one instruction chunks performed by the CPU.
Your statement:
i = ((5*6)/(4+9));

will get converted to
temp_var = 5*6
temp_var2 = 4+9
temp_var = temp_var / temp_var2
i = temp_var

ok, the temp_var''s will probably be registers but the idea is right. i.e the cpu will basicly only handle two values at once, and that is the compilers task to see to it that that is the case. well... if you ain''t using SIMD or other nasty tricks...
But i think it answers the question
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Yes, the compiler takes your code and breaks it into multiple instructions. So you code would look something like this in assembler when converted by the compiler:

// C Code
i = ((a*b)/(c+d));

// assembler(x86)
mul a, b ; eax = a*b
mov i, eax ; i = eax
add c, d ; eax = c+d
div i, eax ; eax = i/eax
mov i, eax ; i = eax

The cpu gets the converted set of instructions. Hope that helps.

-------
My Website
----------------------------------------Implementation is everything. Period.

This topic is closed to new replies.

Advertisement