#define mask 0x80000000
INT64 x = 0xffffffffffffffff;
x &= ~mask;
I expected the result 0xffffffff7fffffff but with my compilor the result is 0x000000007fffffff
No problem if mask = 0x40000000 or mask = 0x100000000.
Obviously the problem occur when the ~ operator change a negative 32 bits value into a positive one.
In order to solve the problem I wrote
#define mask (INT64)0x80000000
No matter how many digits you put, this is a (signed) 32-bit literal. You need to append ULL (or ui64 for MS compilers) to extend that. "1 << 32" would fall into the same trap (although the compiler is allowed to do the right thing in this case), etc ...
Don't get me wrong: your code and my example should work in a well-designed language; after all, it should be the same as maths. It's just that C and C++ have a different view of our code.
No matter how many digits you put, this is a (signed) 32-bit literal. You need to append ULL (or ui64 for MS compilers) to extend that. "1 << 32" would fall into the same trap (although the compiler is allowed to do the right thing in this case), etc ...
Don't get me wrong: your code and my example should work in a well-designed language; after all, it should be the same as maths. It's just that C and C++ have a different view of our code.
Fabien.
The real problem is the macro which operates completely outside the type system. Just use a constexpr variable with the type you want:
No matter how many digits you put, this is a (signed) 32-bit literal. You need to append ULL (or ui64 for MS compilers) to extend that. "1 << 32" would fall into the same trap (although the compiler is allowed to do the right thing in this case), etc ...
Don't get me wrong: your code and my example should work in a well-designed language; after all, it should be the same as maths. It's just that C and C++ have a different view of our code.
Fabien.
The real problem is the macro which operates completely outside the type system. Just use a constexpr variable with the type you want:
Rein Halbersma wrote:
The real problem is the macro which operates completely outside the type system. Just use a constexpr variable with the type you want:
Rein Halbersma wrote:
The real problem is the macro which operates completely outside the type system. Just use a constexpr variable with the type you want:
Just a question for clarification.
When you use a constexpr does that mean that a memory word is used in memory? As a consequence does that mean less speed for your program due to the memory access to this constant?
Just a question for clarification.
When you use a constexpr does that mean that a memory word is used in memory? As a consequence does that mean less speed for your program due to the memory access to this constant?
The variable is typically stored in memory, but the access is substituted at the place-of-call by the compiler whenever it knows that at compile-time (i.e. in other constant expressions). So masking a runtime bitboard of kings with a constexpr mask does not cost a memory access.
Just a question for clarification.
When you use a constexpr does that mean that a memory word is used in memory? As a consequence does that mean less speed for your program due to the memory access to this constant?
The variable is typically stored in memory, but the access is substituted at the place-of-call by the compiler whenever it knows that at compile-time (i.e. in other constant expressions). So masking a runtime bitboard of kings with a constexpr mask does not cost a memory access.
TAILLE wrote:
Just a question for clarification.
When you use a constexpr does that mean that a memory word is used in memory? As a consequence does that mean less speed for your program due to the memory access to this constant?
The variable is typically stored in memory, but the access is substituted at the place-of-call by the compiler whenever it knows that at compile-time (i.e. in other constant expressions). So masking a runtime bitboard of kings with a constexpr mask does not cost a memory access.
I would recommend using const which is not allocated in memory, but is followed by a type such that the compiler can perform type checking.
Also I would recommend to use unsigned integers when doing bit mask operations. Actually this is more-or-less a must when you start to shift bits in case of bitboards, otherwise the high bit, which is used for the sign in signed integers, will not shift like the other bits.
I would recommend using const which is not allocated in memory, but is followed by a type such that the compiler can perform type checking.
Also I would recommend to use unsigned integers when doing bit mask operations. Actually this is more-or-less a must when you start to shift bits in case of bitboards, otherwise the high bit, which is used for the sign in signed integers, will not shift like the other bits.
Jelle
I agree with you Jelle.
I will go through all my macros in order to improve the security of my program by introducing const.
Thank you all.