Archived

This topic is now archived and is closed to further replies.

User defined operators = functions? (+ More)

This topic is 5779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When you use a user defined operator, (eg multiplication on a vector structure), does the compiler place a call to the operator ''function'' (Slow)? Does it actually place the operator code in with the rest of the code (so you have one copy of the operator code per use of the operator? - Fast), or does it do something in between, like a Jump? At the moment I use macros for all vertex and matrix structure operations, but they''re a lot uglier and more difficult than operators. Yet speed is very important. (I''m using Visual C++ 6.0 if that makes a difference) Secondly, the register keyword: if you don''t use it, does the compiler decide to place in registers whatever variables it thinks best, or does it not use register variable storage and use stack/ram storage instead?. Thirdly, can you stuff your program up by using the register keyword - eg if you have a register variable and you want a pointer to it. (Pointers to registers don''t exist, right?) Thanks for any assistance Simon Hill

Share this post


Link to post
Share on other sites
1) As TerranFury said, operators are functions and they can be inlined.

2) If you don''t use the register keyword, the variables are stored in memory, the RAM stack. Even if you do use the register keyword, it may be ignored if the compiler thinks it will need all available registers for processing.

3) You can''t make a pointer to a register, however this issue should not even arise because if you do decide to use the register keyword, you should only use it for temporary primitives whose lifespan is very short.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
If you''re using Visual C++, it silently ignores the register keyword anyway. There''s no way you could allocate registers as well as it does, and it knows it.

Also don''t assume that inlining is necessarily the fastest alternative; again, let the compiler choose. Just structure your program well and optimize the hot spots later once you''ve profiled the whole app.

--
Eric

Share this post


Link to post
Share on other sites
Again, not necessarily. If you can keep your code very small you can fit more of it onto the CPU L1 cache; if you can get all your critical code into that cache you''ll be flying (or at least not CPU bound).

You can force a function to be inlined by rewriting it as a macro, but the resulting code bloat may screw up that caching or (conceivably) your paging behavior. Choosing whether to inline is the kind of thing that compilers really excel at, which is why I said to let the compiler choose. Go ahead and mark your frequently used utility functions inline so the compiler can inline if that would be optimal, but don''t force the issue.

--
Eric

Share this post


Link to post
Share on other sites