replace mp_set_long() implementation by macro
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
diff --git a/bn_mp_set_long.c b/bn_mp_set_long.c
index 2050e9a..60e12b2 100644
--- a/bn_mp_set_long.c
+++ b/bn_mp_set_long.c
@@ -16,32 +16,7 @@
*/
/* set a platform dependent unsigned long int */
-int mp_set_long (mp_int * a, unsigned long b)
-{
- unsigned int x;
- int res;
-
- mp_zero (a);
-
- /* set four bits at a time */
- for (x = 0; x < sizeof(unsigned long) * 2; x++) {
- /* shift the number up four bits */
- if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {
- return res;
- }
-
- /* OR in the top four bits of the source */
- a->dp[0] |= (b >> ((sizeof(unsigned long)) * 8 - 4)) & 15;
-
- /* shift the source up to the next four bits */
- b <<= 4;
-
- /* ensure that digits are not clamped off */
- a->used += 1;
- }
- mp_clamp (a);
- return MP_OKAY;
-}
+MP_SET_XLONG(mp_set_long, unsigned long)
#endif
/* $Source$ */
diff --git a/tommath.h b/tommath.h
index a3faf35..051bdcb 100644
--- a/tommath.h
+++ b/tommath.h
@@ -596,6 +596,40 @@ void bn_reverse(unsigned char *s, int len);
extern const char *mp_s_rmap;
+/* Fancy macro to set an MPI from another type.
+ * There are several things assumed:
+ * x is the counter and unsigned
+ * a is the pointer to the MPI
+ * b is the original value that should be set in the MPI.
+ */
+#define MP_SET_XLONG(func_name, type) \
+int func_name (mp_int * a, type b) \
+{ \
+ unsigned int x; \
+ int res; \
+ \
+ mp_zero (a); \
+ \
+ /* set four bits at a time */ \
+ for (x = 0; x < sizeof(type) * 2; x++) { \
+ /* shift the number up four bits */ \
+ if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \
+ return res; \
+ } \
+ \
+ /* OR in the top four bits of the source */ \
+ a->dp[0] |= (b >> ((sizeof(type)) * 8 - 4)) & 15; \
+ \
+ /* shift the source up to the next four bits */ \
+ b <<= 4; \
+ \
+ /* ensure that digits are not clamped off */ \
+ a->used += 1; \
+ } \
+ mp_clamp (a); \
+ return MP_OKAY; \
+}
+
#ifdef __cplusplus
}
#endif