-
+ 0418E5BDE9C91CCF99BC408F3088AA1EF37C3F8318EBC3BABC9D9AB283A2ED3B158F0057ED885B0C336C69C4F58AC1FF4721D9188072DEC45391C0F2CCA7B250
eucrypt/mpi/include/longlong.h
(0 . 0)(1 . 226)
890 /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
891 * Modified by No Such Labs. (C) 2015. See README.
892 *
893 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
894 * SHA256(gnupg-1.4.10.tar.gz):
895 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
896 * (C) 1994-2005 Free Software Foundation, Inc.
897 *
898 * This program is free software: you can redistribute it and/or modify
899 * it under the terms of the GNU General Public License as published by
900 * the Free Software Foundation, either version 3 of the License, or
901 * (at your option) any later version.
902 *
903 * This program is distributed in the hope that it will be useful,
904 * but WITHOUT ANY WARRANTY; without even the implied warranty of
905 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
906 * GNU General Public License for more details.
907 *
908 * You should have received a copy of the GNU General Public License
909 * along with this program. If not, see <http://www.gnu.org/licenses/>.
910 */
911
912 /* You have to define the following before including this file:
913
914 UWtype -- An unsigned type, default type for operations (typically a "word")
915 UHWtype -- An unsigned type, at least half the size of UWtype.
916 UDWtype -- An unsigned type, at least twice as large a UWtype
917 W_TYPE_SIZE -- size in bits of UWtype
918
919 SItype, USItype -- Signed and unsigned 32 bit types.
920 DItype, UDItype -- Signed and unsigned 64 bit types.
921
922 On a 32 bit machine UWtype should typically be USItype;
923 on a 64 bit machine, UWtype should typically be UDItype.
924 */
925
926 #define __BITS4 (W_TYPE_SIZE / 4)
927 #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
928 #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
929 #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
930
931 /* This is used to make sure no undesirable sharing between different libraries
932 that use this file takes place. */
933 #ifndef __MPN
934 #define __MPN(x) __##x
935 #endif
936
937 /***************************************
938 *********** Generic Versions ********
939 ***************************************/
940 #if !defined (umul_ppmm) && defined (__umulsidi3)
941 #define umul_ppmm(ph, pl, m0, m1) \
942 { \
943 UDWtype __ll = __umulsidi3 (m0, m1); \
944 ph = (UWtype) (__ll >> W_TYPE_SIZE); \
945 pl = (UWtype) __ll; \
946 }
947 #endif
948
949 #if !defined (__umulsidi3)
950 #define __umulsidi3(u, v) \
951 ({UWtype __hi, __lo; \
952 umul_ppmm (__hi, __lo, u, v); \
953 ((UDWtype) __hi << W_TYPE_SIZE) | __lo; })
954 #endif
955
956 /* If this machine has no inline assembler, use C macros. */
957
958 #if !defined (add_ssaaaa)
959 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
960 do { \
961 UWtype __x; \
962 __x = (al) + (bl); \
963 (sh) = (ah) + (bh) + (__x < (al)); \
964 (sl) = __x; \
965 } while (0)
966 #endif
967
968 #if !defined (sub_ddmmss)
969 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
970 do { \
971 UWtype __x; \
972 __x = (al) - (bl); \
973 (sh) = (ah) - (bh) - (__x > (al)); \
974 (sl) = __x; \
975 } while (0)
976 #endif
977
978 #if !defined (umul_ppmm)
979 #define umul_ppmm(w1, w0, u, v) \
980 do { \
981 UWtype __x0, __x1, __x2, __x3; \
982 UHWtype __ul, __vl, __uh, __vh; \
983 UWtype __u = (u), __v = (v); \
984 \
985 __ul = __ll_lowpart (__u); \
986 __uh = __ll_highpart (__u); \
987 __vl = __ll_lowpart (__v); \
988 __vh = __ll_highpart (__v); \
989 \
990 __x0 = (UWtype) __ul * __vl; \
991 __x1 = (UWtype) __ul * __vh; \
992 __x2 = (UWtype) __uh * __vl; \
993 __x3 = (UWtype) __uh * __vh; \
994 \
995 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
996 __x1 += __x2; /* but this indeed can */ \
997 if (__x1 < __x2) /* did we get it? */ \
998 __x3 += __ll_B; /* yes, add it in the proper pos. */ \
999 \
1000 (w1) = __x3 + __ll_highpart (__x1); \
1001 (w0) = (__ll_lowpart (__x1) << W_TYPE_SIZE/2) + __ll_lowpart (__x0);\
1002 } while (0)
1003 #endif
1004
1005 #if !defined (umul_ppmm)
1006 #define smul_ppmm(w1, w0, u, v) \
1007 do { \
1008 UWtype __w1; \
1009 UWtype __m0 = (u), __m1 = (v); \
1010 umul_ppmm (__w1, w0, __m0, __m1); \
1011 (w1) = __w1 - (-(__m0 >> (W_TYPE_SIZE - 1)) & __m1) \
1012 - (-(__m1 >> (W_TYPE_SIZE - 1)) & __m0); \
1013 } while (0)
1014 #endif
1015
1016 /* Define this unconditionally, so it can be used for debugging. */
1017 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
1018 do { \
1019 UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m; \
1020 __d1 = __ll_highpart (d); \
1021 __d0 = __ll_lowpart (d); \
1022 \
1023 __r1 = (n1) % __d1; \
1024 __q1 = (n1) / __d1; \
1025 __m = (UWtype) __q1 * __d0; \
1026 __r1 = __r1 * __ll_B | __ll_highpart (n0); \
1027 if (__r1 < __m) \
1028 { \
1029 __q1--, __r1 += (d); \
1030 if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
1031 if (__r1 < __m) \
1032 __q1--, __r1 += (d); \
1033 } \
1034 __r1 -= __m; \
1035 \
1036 __r0 = __r1 % __d1; \
1037 __q0 = __r1 / __d1; \
1038 __m = (UWtype) __q0 * __d0; \
1039 __r0 = __r0 * __ll_B | __ll_lowpart (n0); \
1040 if (__r0 < __m) \
1041 { \
1042 __q0--, __r0 += (d); \
1043 if (__r0 >= (d)) \
1044 if (__r0 < __m) \
1045 __q0--, __r0 += (d); \
1046 } \
1047 __r0 -= __m; \
1048 \
1049 (q) = (UWtype) __q1 * __ll_B | __q0; \
1050 (r) = __r0; \
1051 } while (0)
1052
1053 /* If the processor has no udiv_qrnnd but sdiv_qrnnd, go through
1054 __udiv_w_sdiv (defined in libgcc or elsewhere). */
1055 #if !defined (udiv_qrnnd) && defined (sdiv_qrnnd)
1056 #define udiv_qrnnd(q, r, nh, nl, d) \
1057 do { \
1058 UWtype __r; \
1059 (q) = __MPN(udiv_w_sdiv) (&__r, nh, nl, d); \
1060 (r) = __r; \
1061 } while (0)
1062 #endif
1063
1064 /* If udiv_qrnnd was not defined for this processor, use __udiv_qrnnd_c. */
1065 #if !defined (udiv_qrnnd)
1066 #define UDIV_NEEDS_NORMALIZATION 1
1067 #define udiv_qrnnd __udiv_qrnnd_c
1068 #endif
1069
1070 #if !defined (count_leading_zeros)
1071 extern
1072 #ifdef __STDC__
1073 const
1074 #endif
1075 unsigned char __clz_tab[];
1076 #define MPI_INTERNAL_NEED_CLZ_TAB 1
1077 #define count_leading_zeros(count, x) \
1078 do { \
1079 UWtype __xr = (x); \
1080 UWtype __a; \
1081 \
1082 if (W_TYPE_SIZE <= 32) \
1083 { \
1084 __a = __xr < ((UWtype) 1 << 2*__BITS4) \
1085 ? (__xr < ((UWtype) 1 << __BITS4) ? 0 : __BITS4) \
1086 : (__xr < ((UWtype) 1 << 3*__BITS4) ? 2*__BITS4 : 3*__BITS4);\
1087 } \
1088 else \
1089 { \
1090 for (__a = W_TYPE_SIZE - 8; __a > 0; __a -= 8) \
1091 if (((__xr >> __a) & 0xff) != 0) \
1092 break; \
1093 } \
1094 \
1095 (count) = W_TYPE_SIZE - (__clz_tab[__xr >> __a] + __a); \
1096 } while (0)
1097 /* This version gives a well-defined value for zero. */
1098 #define COUNT_LEADING_ZEROS_0 W_TYPE_SIZE
1099 #endif
1100
1101 #if !defined (count_trailing_zeros)
1102 /* Define count_trailing_zeros using count_leading_zeros. The latter might be
1103 defined in asm, but if it is not, the C version above is good enough. */
1104 #define count_trailing_zeros(count, x) \
1105 do { \
1106 UWtype __ctz_x = (x); \
1107 UWtype __ctz_c; \
1108 count_leading_zeros (__ctz_c, __ctz_x & -__ctz_x); \
1109 (count) = W_TYPE_SIZE - 1 - __ctz_c; \
1110 } while (0)
1111 #endif
1112
1113 #ifndef UDIV_NEEDS_NORMALIZATION
1114 #define UDIV_NEEDS_NORMALIZATION 0
1115 #endif