I am writing to inquire a quick question regarding a wired C statement. The following statement is found in libgcrypt 1.7.3, line 680 of mpi/mpi-pow.c.
base_u_size |= (precomp_size[k] & ((mpi_size_t)0 - (k == e0)) );
So the appending 0
after mpi_size_t
seems so wired to me. I also show the following assembly code sequence, which corresponds to this source code statement:
mov -0x138(%ebp),%eax
mov -0x48(%ebp,%eax,4),%edx
mov -0x138(%ebp),%eax
cmp -0x114(%ebp),%eax <----- eax = k, -0x114(%ebp) = e0
jne 80b84b2 <_gcry_mpi_powm+0xeee>
mov $0xffffffff,%eax
jmp 80b84b7 <_gcry_mpi_powm+0xef3>
80b84b2: mov $0x0,%eax
80b84b7: and %edx,%eax
So somehow the 0
does not lead to any effect here?
So the appending 0 after
mpi_size_t
It's called type-casting.
By definition, integer constant values, are of type int
. To make is explicitly of type mpi_size_t
, you cast the value to the required type.
The 0
after mpi_size_t
is for typecasting it (0) to the type mpi_size_t
What does the appending zero mean in a C assignment statement?
base_u_size |= (precomp_size[k] & ((mpi_size_t)0 - (k == e0)) );
As well answer by @Sourav Ghosh, it casts the 0 to type mpi_size_t
.
To answer why code performs the cast?
In mpi/mpi-pow.c
, base_u_size
is of type mpi_size_t
.
To insure the right side of the assignment is done using at least the rank and width of mpi_size_t
math and quiet warnings of mixed signed types.
By including a mpi_size_t
on the right side, the subtraction and the &
use a width that does not lose information.
With the types of the objects involved as here ( I do not have exactly what OP is using), it is likely to make no difference here. base_u_size |= precomp_size[k] & -(k == e0);
would have been fine - yet bring up persnickety warnings.
OP's code is a common idiom (do right hand math at least as wide as left size and strive for same signnesss) to prevent problems.
User contributions licensed under CC BY-SA 3.0