Constant and variable
Moderator: Stef
Constant and variable
This question sounds stupid, but up to now (coding for PCs), I never wondered if an object must be stored in ROM or RAM. Everything was RAM
So must I take for granted that, if I declare something 'const', it will be created in ROM by the compiler. Or is there anything else ?
So must I take for granted that, if I declare something 'const', it will be created in ROM by the compiler. Or is there anything else ?
-
- Very interested
- Posts: 710
- Joined: Sat Feb 18, 2012 2:44 am
Also if you are using the array in only one file make sure to make it static. Same with functions. This is especially important for functions and applies regardless of platform you should always when appropriate declare make proper use of const and use it whenever what you are dealing with is const and when used in one file always declare as static. Don't think that better hardware should allow you to forget good coding practices. Making the function static allows the compiler to do additional optimizations based upon use of the function such as constant propagation. For example lets say you have a function
You always call the function with the same w value. w happens to be 256 always. If the compiler knew that w was always 256 it could replace the multiplication with a bit shift and inline it maybe even without declaring it as such if the decided that inlining has no negative tradeoffs. Despite enabling link time optimizations I have still got better code declaring functions as static when appropriate. I even checked the generated machine code.
Also while I am at it another tip make when appropriate make use of the restrict keyword and attributes such as pure for example lets say you wrote a square root function. Declare it pure, a square root function should be a pure function anyway.
Code: Select all
static inline void writePixel(unsigned x,unsigned y,unsigned w,unsigned val){
vram[x+y*w]=val;
}
Also while I am at it another tip make when appropriate make use of the restrict keyword and attributes such as pure for example lets say you wrote a square root function. Declare it pure, a square root function should be a pure function anyway.
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
Things like restrict might be why certain tests of new versions of gcc seem slower than old ones. Old gcc probably made lots of assumptions for speed that have since been deemed bugs that require things like restrict to enable. Using new versions of gcc requires more knowledge of attributes and how they affect optimizations to get similar results, but should result in more stable code.
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
When I started, all there was was K&R. Standards didn't come along until later.tryphon wrote:Thanks for the tips.
Last time I coded in C, it didn't have attributes
I remember when I finally got SAS/C - it came with this monster book on C as implemented by SAS. That was kinda the standard for a long time as SAS became popular on a lot of platforms.
Always glad to help.tryphon wrote:Thanks for the tips.
That is because this is a gcc extension see https://gcc.gnu.org/onlinedocs/gcc/Func ... butes.html.tryphon wrote: Last time I coded in C, it didn't have attributes
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
-
- Very interested
- Posts: 2984
- Joined: Fri Aug 17, 2007 9:33 pm
I was so fluent in 6502 (had an Atari 8-bitter back then), I could read the code from the hex dump, and change it via a hex-editor. I was nearly as good with 68000 assembly code - I can read the hex dump pretty good, but it's a bit of a stretch to convert from assembly back to hex in my head... other than some of the more common codes, like jumps and branches and the like.tryphon wrote:I actually did this with 6809 asm (not C), at the time I owned a MO6 (only French can know what it is).
Branchment instructions to compute in hex was such a fun
I was 13 then...
Time flies...
I know quite a few programmers on that level. It was easier to reach on 8-bit systems. Forget about it on new systems. Most don't bother even learning assembly these days. It's slowly passing away.