In my recent tests, fastcall gained nothing so I stopped using it.mar wrote:Elementary types are always returned in registers. I think fastcall simply passes function args in registers if possible (but this is done automatically for x64).Dann Corbit wrote:I did not try it lately, but (for instance) if you did a fastcall compile (forces things to registers for returns) you would get an error message about main() with Visual Studio.
https://msdn.microsoft.com/en-us/library/6xa169sk.aspx
For performance critical paths tiny functions will be inlined anyway so I don't see what you really gain from using fastcall (except for adding extra ifdef/define CDECL)?
If it's not inlined (i.e. complicated) then you don't have enough registers anyway [x86] and most likely some of those args will be stored on stack anyway because you'll need to reuse registers to optimize inner loops etc.
So I would strongly discourage anyone from messing with default calling convention, it won't magically make your program run faster.
C programming style question
Moderators: hgm, Rebel, chrisw
-
- Posts: 12541
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C programming style question
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
-
- Posts: 589
- Joined: Tue Jun 04, 2013 10:15 pm
Re: C programming style question
abulmo wrote:Both methods are equivalent.
Note that *(a + i) is the same as a, so you can write dirXX[m][s] or *(dirxx[m] + s) anywhere in your code.
And as pointer addition is commutative in C, you can also conveniently write
Code: Select all
dirXX[m][s]
Code: Select all
s[m[dirXX]]
P.S.:
Code: Select all
for (int i=0; putchar(i++[":-)\n"]); );
[Account deleted]
-
- Posts: 12541
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C programming style question
See also:
http://c-faq.com/aryptr/joke.html
http://c-faq.com/aryptr/joke.html
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
-
- Posts: 3196
- Joined: Fri May 26, 2006 3:00 am
- Location: WY, USA
- Full name: Michael Sherwin
Re: C programming style question
MSVS 2005kbhearn wrote:dare i ask how old your compiler is? it's true microsoft didn't support it for a long while as it was part of C99 and microsoft didn't care about that but a few years back they finally implemented some of the core parts in VS2012 (probably at least in part because C++11 required certain C99 features to exist - the stdint.h header being one of those).Michael Sherwin wrote:Since my compiler does not come with stdint.h or inttypes.h I will just include that in the #ifdef _MSC_VER block.kbhearn wrote:absolutely, if you'd prefer to write u64, that would be the portable way to go about itMichael Sherwin wrote:So I should be able to use uint64_t and still typedef to u64. I'll give it a try.kbhearn wrote:the point is that uint64_t from <stdint.h> is a standard-defined way to request an exactly 64 bit unsigned int while __int64 is an implementation-defined token. It's not a huge difference though.
as a side note, the clue on all these things that aren't portable: anything that starts with a double underscore is implementation-defined.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
-
- Posts: 3196
- Joined: Fri May 26, 2006 3:00 am
- Location: WY, USA
- Full name: Michael Sherwin
Re: C programming style question
When I used MSVC 6 __cdecl gave me faster executables. So I thought it would be the same way for MSVS 2005.mar wrote:Hmm, I wonder why you need to specify __cdecl at all, not sure if it still holds for VS2015 (if would be very surprised if it wouldn't),
but __cdecl is the default calling convention (unless you mess with your project settings manually)
I also wonder why not simply int main() as you'll most likely never return anything outside -32k..32k range anyway.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
-
- Posts: 3196
- Joined: Fri May 26, 2006 3:00 am
- Location: WY, USA
- Full name: Michael Sherwin
Re: C programming style question
Thanks, I'll change it.Dann Corbit wrote:I like it better.
But this is a naughty no-no:
#define EXIT_SUCCESS 1
The macro EXIT_SUCCESS is included in header file
#include <stdlib.h>
and needs to be defined by the implementation.
Believe it or not, your implementation behaves badly on {for instance} OpenVMS and normally EXIT_SUCCESS will be zero for most systems.
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
-
- Posts: 3196
- Joined: Fri May 26, 2006 3:00 am
- Location: WY, USA
- Full name: Michael Sherwin
Re: C programming style question
If this is okay then I will be on to the next step. Thanks everyone!
Code: Select all
#include <stdlib.h>
#ifdef _MSC_VER
typedef unsigned __int32 s32;
typedef signed __int64 u64;
#else
#include <stdint.h>
//#include <inttypes.h>
#define sint32_t s32;
#define uint64_t u64;
#endif
u64 dirNW[64];
u64 dirNN[64];
u64 dirNE[64];
u64 dirEE[64];
u64 dirSE[64];
u64 dirSS[64];
u64 dirSW[64];
u64 dirWW[64];
void InitData(void);
s32 main(void);
void InitData() {
s32 i,j,k,sq64,sq0x88;
u64 *dirXX[8] = {dirNW,dirNN,dirNE,dirEE,dirSE,dirSS,dirSW,dirWW};
s32 board0x88Off[8] = {15,16,17,1,-15,16,17,-1};
s32 board64Off[8] = {7,8,9,1,-7,-8,-9};
for(i = 0, k = -1; i <= 0x80; i++) {
if((i & 0x88 ? false : true)) {
k++;
for(j = 0; j < 8; j++) {
dirXX[j][k] = 0;
sq64 = k;
sq0x88 = i + board0x88Off[j];
while((sq0x88 & 0x88 ? false : true)) {
sq64 = sq64 + board64Off[j];
dirXX[j][k] ^= (u64)1 << sq64;
sq0x88 = sq0x88 + board0x88Off[j];
}
}
}
}
}
s32 main(){
InitData();
return(EXIT_SUCCESS);
}
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
-
- Posts: 1
- Joined: Thu Dec 03, 2015 11:28 pm
Re: C programming style question
Your C style is a little, mmm, archaic.
There are a lot of guides on Modern C, like https://matt.sh/howto-c for example.
Good luck!
There are a lot of guides on Modern C, like https://matt.sh/howto-c for example.
Good luck!
-
- Posts: 3196
- Joined: Fri May 26, 2006 3:00 am
- Location: WY, USA
- Full name: Michael Sherwin
Re: C programming style question
I will read that. I am just a self taught programmer. My main documentation are the books that came with Watcom C. And my main tutorial is C Primer Plus 1987. So I guess you have a point. lolslm wrote:Your C style is a little, mmm, archaic.
There are a lot of guides on Modern C, like https://matt.sh/howto-c for example.
Good luck!
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
-
- Posts: 151
- Joined: Thu Nov 12, 2009 6:31 pm
Re: C programming style question
[code"]
#ifdef _MSC_VER
typedef unsigned __int32 s32;
typedef signed __int64 u64;
[/code]
I think you inverted signed and unsigned here.
As you will never call it, declaring main here is useless.
Here it is int main(void) or int main(int argc, char **argv).
The void is important here and your s32 is a wrong type if _MSC_VER is defined.
#ifdef _MSC_VER
typedef unsigned __int32 s32;
typedef signed __int64 u64;
[/code]
I think you inverted signed and unsigned here.
Code: Select all
s32 main(void);
Code: Select all
s32 main(){
The void is important here and your s32 is a wrong type if _MSC_VER is defined.
Richard