C programming style question

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C programming style question

Post by Dann Corbit »

mar wrote:
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
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).
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.
In my recent tests, fastcall gained nothing so I stopped using it.
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.
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: C programming style question

Post by mvk »

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]
as

Code: Select all

s[m[dirXX]]
and get exactly the same result.

P.S.:

Code: Select all

for (int i=0; putchar(i++[":-)\n"]); );
[Account deleted]
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C programming style question

Post by Dann Corbit »

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.
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: C programming style question

Post by Michael Sherwin »

kbhearn wrote:
Michael Sherwin wrote:
kbhearn wrote:
Michael Sherwin wrote:
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.
So I should be able to use uint64_t and still typedef to u64. I'll give it a try.
absolutely, if you'd prefer to write u64, that would be the portable way to go about it :)

as a side note, the clue on all these things that aren't portable: anything that starts with a double underscore is implementation-defined.
Since my compiler does not come with stdint.h or inttypes.h I will just include that in the #ifdef _MSC_VER block.
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).
MSVS 2005
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
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: C programming style question

Post by Michael Sherwin »

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.
When I used MSVC 6 __cdecl gave me faster executables. So I thought it would be the same way for MSVS 2005.
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
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: C programming style question

Post by Michael Sherwin »

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.
Thanks, I'll change it.
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
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: C programming style question

Post by Michael Sherwin »

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&#91;64&#93;;
u64 dirNN&#91;64&#93;;
u64 dirNE&#91;64&#93;;
u64 dirEE&#91;64&#93;;
u64 dirSE&#91;64&#93;;
u64 dirSS&#91;64&#93;;
u64 dirSW&#91;64&#93;;
u64 dirWW&#91;64&#93;;

void InitData&#40;void&#41;;
s32 main&#40;void&#41;;

void InitData&#40;) &#123;
  s32 i,j,k,sq64,sq0x88; 
  u64 *dirXX&#91;8&#93; = &#123;dirNW,dirNN,dirNE,dirEE,dirSE,dirSS,dirSW,dirWW&#125;;
  s32 board0x88Off&#91;8&#93; = &#123;15,16,17,1,-15,16,17,-1&#125;;
  s32 board64Off&#91;8&#93; = &#123;7,8,9,1,-7,-8,-9&#125;;
  for&#40;i = 0, k = -1; i <= 0x80; i++) &#123;
    if&#40;&#40;i & 0x88 ? false &#58; true&#41;) &#123; 
      k++;
      for&#40;j = 0; j < 8; j++) &#123;
        dirXX&#91;j&#93;&#91;k&#93; = 0;
        sq64 = k;
        sq0x88 = i + board0x88Off&#91;j&#93;;
        while&#40;&#40;sq0x88 & 0x88 ? false &#58; true&#41;) &#123;
          sq64 = sq64 + board64Off&#91;j&#93;;
          dirXX&#91;j&#93;&#91;k&#93; ^= &#40;u64&#41;1 << sq64;
          sq0x88 = sq0x88 + board0x88Off&#91;j&#93;;
        &#125;
      &#125;
    &#125;
  &#125;
&#125;

s32 main&#40;)&#123;
  InitData&#40;);
  return&#40;EXIT_SUCCESS&#41;;
&#125;
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
slm
Posts: 1
Joined: Thu Dec 03, 2015 11:28 pm

Re: C programming style question

Post by slm »

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!
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: C programming style question

Post by Michael Sherwin »

slm 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!
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. lol
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
abulmo
Posts: 151
Joined: Thu Nov 12, 2009 6:31 pm

Re: C programming style question

Post by abulmo »

[code"]
#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&#40;void&#41;;
As you will never call it, declaring main here is useless.

Code: Select all

s32 main&#40;)&#123;
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.
Richard