Open sjaeckel opened 1 year ago
I just thought about it a bit more and had a look into mp_cmp_d() and only then realized that
#define mp_isone(a) ( ((a)->used == 1u) && ((a)->dp[0] == 1u) )
would falsely claim -1 as one ...
Ah, that's why I have it as is_unity
elsewhere (mainly in my JS version). Shouldn't have just renamed it without thinking. My bad, sorry!
#define mp_isunity(a) ( ((a)->used == 1u) && ((a)->dp[0] == 1u) )
#define mp_isone(a) ( (!mp_isneg(a)) && mp_isunity(a) )
/* Or together */
#define mp_isone(a) ( ((a)->sign == MP_ZPOS) && ((a)->used == 1u) && ((a)->dp[0] == 1u) )
/* Generalizing it like this is better done with a full function (mp_cmp_d() in this case) */
#define mp_issmallnumber(a,b) ( ((a)->sign == MP_ZPOS) && ((a)->used == 1u) && ((a)->dp[0] == b) )
does the function-call overhead really matter that much?
Testing for zero or a small integer happens quite often in loops where a function-overhead would multiply. I am pretty sure that modern compilers would be able to handle it but we also support a lot of the older ones. We don't offer C89 support for nothing. On the other hand: the people who use the older compilers tend to know what they are doing, so...
Creating this issue in order to pull the discussion regarding a
mp_isone()
macro out of #532Following up on https://github.com/libtom/libtommath/pull/532#issuecomment-1265290821
I just thought about it a bit more and had a look into
mp_cmp_d()
and only then realized that#define mp_isone(a) ( ((a)->used == 1u) && ((a)->dp[0] == 1u) )
would falsely claim-1
asone
...So I have some follow up questions:
mp_cmp_d()
?