aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorPaul Eggert2018-08-21 02:16:50 -0700
committerPaul Eggert2018-08-21 02:38:53 -0700
commitd6a497dd887cdbb35c5b4e2929e83962ba708159 (patch)
tree9f0441f9fe88419b71e568b05ef7f7bea0a0ff06 /doc
parent77fc2725985b4e5ef977ae6930835c7f0771c61c (diff)
downloademacs-d6a497dd887cdbb35c5b4e2929e83962ba708159.tar.gz
emacs-d6a497dd887cdbb35c5b4e2929e83962ba708159.zip
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its internal data structures. The numeric limit is large and platform-dependent; with 64-bit GMP 6.1.2 it is around 2**2**37. Work around the problem by refusing to call libgmp functions with arguments that would cause an abort. With luck libgmp will have a better way to do this in the future. Also, introduce a variable integer-width that lets the user control how large bignums can be. This currently defaults to 2**16, i.e., it allows bignums up to 2**2**16. This should be enough for ordinary computation, and should help Emacs to avoid thrashing or hanging. Problem noted by Pip Cet (Bug#32463#71). * doc/lispref/numbers.texi, etc/NEWS: Document recent bignum changes, including this one. Improve documentation for bitwise operations, in the light of bignums. * src/alloc.c (make_number): Enforce integer-width. (integer_overflow): New function. (xrealloc_for_gmp, xfree_for_gmp): Move here from emacs.c, as it's memory allocation. (init_alloc): Initialize GMP here, rather than in emacs.c. (integer_width): New var. * src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants. (emacs_mpz_size, emacs_mpz_mul) (emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions. (arith_driver, Fash, expt_integer): Use them. (expt_integer): New function, containing integer code that was out of place in floatfns.c. (check_bignum_size, xmalloc_for_gmp): Remove. * src/emacs.c (main): Do not initialize GMP here. * src/floatfns.c (Fexpt): Use expt_integer, which now contains integer code moved from here. * src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/numbers.texi314
1 files changed, 148 insertions, 166 deletions
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index 209e9f139a5..9c16b1a64c4 100644
--- a/doc/lispref/numbers.texi
+++ b/doc/lispref/numbers.texi
@@ -34,13 +34,21 @@ numbers have a fixed amount of precision.
34@node Integer Basics 34@node Integer Basics
35@section Integer Basics 35@section Integer Basics
36 36
37 Integers in Emacs Lisp can have arbitrary precision. 37 Integers in Emacs Lisp are not limited to the machine word size.
38 38
39 Under the hood, though, there are two kinds of integers: smaller 39 Under the hood, though, there are two kinds of integers: smaller
40ones, called @dfn{fixnums}, and larger ones, called @dfn{bignums}. 40ones, called @dfn{fixnums}, and larger ones, called @dfn{bignums}.
41Some functions in Emacs only accept fixnums. Also, while fixnums can 41Some functions in Emacs accept only fixnums. Also, while fixnums can
42always be compared for equality with @code{eq}, bignums require the 42always be compared for numeric equality with @code{eq}, bignums
43use of @code{eql}. 43require more-heavyweight equality predicates like @code{eql}.
44
45 The range of values for bignums is limited by the amount of main
46memory, by machine characteristics such as the size of the word used
47to represent a bignum's exponent, and by the @code{integer-width}
48variable. These limits are typically much more generous than the
49limits for fixnums. A bignum is never numerically equal to a fixnum;
50if Emacs computes an integer in fixnum range, it represents the
51integer as a fixnum, not a bignum.
44 52
45 The range of values for a fixnum depends on the machine. The 53 The range of values for a fixnum depends on the machine. The
46minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e., 54minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
@@ -97,33 +105,30 @@ For example:
97#24r1k @result{} 44 105#24r1k @result{} 44
98@end example 106@end example
99 107
100 An integer is read as a fixnum if it is in the correct range.
101Otherwise, it will be read as a bignum.
102
103 To understand how various functions work on integers, especially the 108 To understand how various functions work on integers, especially the
104bitwise operators (@pxref{Bitwise Operations}), it is often helpful to 109bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
105view the numbers in their binary form. 110view the numbers in their binary form.
106 111
107 In 30-bit binary, the decimal integer 5 looks like this: 112 In binary, the decimal integer 5 looks like this:
108 113
109@example 114@example
1100000...000101 (30 bits total) 115...000101
111@end example 116@end example
112 117
113@noindent 118@noindent
114(The @samp{...} stands for enough bits to fill out a 30-bit word; in 119(The @samp{...} stands for a conceptually infinite number of bits that
115this case, @samp{...} stands for twenty 0 bits. Later examples also 120match the leading bit; here, an infinite number of 0 bits. Later
116use the @samp{...} notation to make binary integers easier to read.) 121examples also use this @samp{...} notation.)
117 122
118 The integer @minus{}1 looks like this: 123 The integer @minus{}1 looks like this:
119 124
120@example 125@example
1211111...111111 (30 bits total) 126...111111
122@end example 127@end example
123 128
124@noindent 129@noindent
125@cindex two's complement 130@cindex two's complement
126@minus{}1 is represented as 30 ones. (This is called @dfn{two's 131@minus{}1 is represented as all ones. (This is called @dfn{two's
127complement} notation.) 132complement} notation.)
128 133
129 Subtracting 4 from @minus{}1 returns the negative integer @minus{}5. 134 Subtracting 4 from @minus{}1 returns the negative integer @minus{}5.
@@ -131,14 +136,7 @@ In binary, the decimal integer 4 is 100. Consequently,
131@minus{}5 looks like this: 136@minus{}5 looks like this:
132 137
133@example 138@example
1341111...111011 (30 bits total) 139...111011
135@end example
136
137 In this implementation, the largest 30-bit binary integer is
138536,870,911 in decimal. In binary, it looks like this:
139
140@example
1410111...111111 (30 bits total)
142@end example 140@end example
143 141
144 Many of the functions described in this chapter accept markers for 142 Many of the functions described in this chapter accept markers for
@@ -147,10 +145,10 @@ arguments to such functions may be either numbers or markers, we often
147give these arguments the name @var{number-or-marker}. When the argument 145give these arguments the name @var{number-or-marker}. When the argument
148value is a marker, its position value is used and its buffer is ignored. 146value is a marker, its position value is used and its buffer is ignored.
149 147
150@cindex largest Lisp integer 148@cindex largest fixnum
151@cindex maximum Lisp integer 149@cindex maximum fixnum
152@defvar most-positive-fixnum 150@defvar most-positive-fixnum
153The value of this variable is the largest ``small'' integer that Emacs 151The value of this variable is the greatest ``small'' integer that Emacs
154Lisp can handle. Typical values are 152Lisp can handle. Typical values are
155@ifnottex 153@ifnottex
1562**29 @minus{} 1 1542**29 @minus{} 1
@@ -168,11 +166,11 @@ on 32-bit and
168on 64-bit platforms. 166on 64-bit platforms.
169@end defvar 167@end defvar
170 168
171@cindex smallest Lisp integer 169@cindex smallest fixnum
172@cindex minimum Lisp integer 170@cindex minimum fixnum
173@defvar most-negative-fixnum 171@defvar most-negative-fixnum
174The value of this variable is the smallest small integer that Emacs 172The value of this variable is the numerically least ``small'' integer
175Lisp can handle. It is negative. Typical values are 173that Emacs Lisp can handle. It is negative. Typical values are
176@ifnottex 174@ifnottex
177@minus{}2**29 175@minus{}2**29
178@end ifnottex 176@end ifnottex
@@ -189,6 +187,19 @@ on 32-bit and
189on 64-bit platforms. 187on 64-bit platforms.
190@end defvar 188@end defvar
191 189
190@cindex bignum range
191@cindex integer range
192@defvar integer-width
193The value of this variable is a nonnegative integer that is an upper
194bound on the number of bits in a bignum. Integers outside the fixnum
195range are limited to absolute values less than 2@sup{@var{n}}, where
196@var{n} is this variable's value. Attempts to create bignums outside
197this range result in integer overflow. Setting this variable to zero
198disables creation of bignums; setting it to a large number can cause
199Emacs to consume large quantities of memory if a computation creates
200huge integers.
201@end defvar
202
192 In Emacs Lisp, text characters are represented by integers. Any 203 In Emacs Lisp, text characters are represented by integers. Any
193integer between zero and the value of @code{(max-char)}, inclusive, is 204integer between zero and the value of @code{(max-char)}, inclusive, is
194considered to be valid as a character. @xref{Character Codes}. 205considered to be valid as a character. @xref{Character Codes}.
@@ -378,17 +389,17 @@ comparison, and sometimes returns @code{t} when a non-numeric
378comparison would return @code{nil} and vice versa. @xref{Float 389comparison would return @code{nil} and vice versa. @xref{Float
379Basics}. 390Basics}.
380 391
381 In Emacs Lisp, each small integer is a unique Lisp object. 392 In Emacs Lisp, if two fixnums are numerically equal, they are the
382Therefore, @code{eq} is equivalent to @code{=} where small integers are 393same Lisp object. That is, @code{eq} is equivalent to @code{=} on
383concerned. It is sometimes convenient to use @code{eq} for comparing 394fixnums. It is sometimes convenient to use @code{eq} for comparing
384an unknown value with an integer, because @code{eq} does not report an 395an unknown value with a fixnum, because @code{eq} does not report an
385error if the unknown value is not a number---it accepts arguments of 396error if the unknown value is not a number---it accepts arguments of
386any type. By contrast, @code{=} signals an error if the arguments are 397any type. By contrast, @code{=} signals an error if the arguments are
387not numbers or markers. However, it is better programming practice to 398not numbers or markers. However, it is better programming practice to
388use @code{=} if you can, even for comparing integers. 399use @code{=} if you can, even for comparing integers.
389 400
390 Sometimes it is useful to compare numbers with @code{equal}, which 401 Sometimes it is useful to compare numbers with @code{eql} or @code{equal},
391treats two numbers as equal if they have the same data type (both 402which treat two numbers as equal if they have the same data type (both
392integers, or both floating point) and the same value. By contrast, 403integers, or both floating point) and the same value. By contrast,
393@code{=} can treat an integer and a floating-point number as equal. 404@code{=} can treat an integer and a floating-point number as equal.
394@xref{Equality Predicates}. 405@xref{Equality Predicates}.
@@ -830,142 +841,113 @@ Rounding a value equidistant between two integers returns the even integer.
830@cindex logical arithmetic 841@cindex logical arithmetic
831 842
832 In a computer, an integer is represented as a binary number, a 843 In a computer, an integer is represented as a binary number, a
833sequence of @dfn{bits} (digits which are either zero or one). A bitwise 844sequence of @dfn{bits} (digits which are either zero or one).
845Conceptually the bit sequence is infinite on the left, with the
846most-significant bits being all zeros or all ones. A bitwise
834operation acts on the individual bits of such a sequence. For example, 847operation acts on the individual bits of such a sequence. For example,
835@dfn{shifting} moves the whole sequence left or right one or more places, 848@dfn{shifting} moves the whole sequence left or right one or more places,
836reproducing the same pattern moved over. 849reproducing the same pattern moved over.
837 850
838 The bitwise operations in Emacs Lisp apply only to integers. 851 The bitwise operations in Emacs Lisp apply only to integers.
839 852
840@defun lsh integer1 count 853@defun ash integer1 count
841@cindex logical shift 854@cindex arithmetic shift
842@code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the 855@code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
843bits in @var{integer1} to the left @var{count} places, or to the right 856to the left @var{count} places, or to the right if @var{count} is
844if @var{count} is negative, bringing zeros into the vacated bits. If 857negative. Left shifts introduce zero bits on the right; right shifts
845@var{count} is negative, @code{lsh} shifts zeros into the leftmost 858discard the rightmost bits. Considered as an integer operation,
846(most-significant) bit, producing a nonnegative result even if 859@code{ash} multiplies @var{integer1} by 2@sup{@var{count}} and then
847@var{integer1} is negative fixnum. (If @var{integer1} is a negative 860converts the result to an integer by rounding downward, toward
848bignum, @var{count} must be nonnegative.) Contrast this with 861minus infinity.
849@code{ash}, below. 862
850 863Here are examples of @code{ash}, shifting a pattern of bits one place
851Here are two examples of @code{lsh}, shifting a pattern of bits one 864to the left and to the right. These examples show only the low-order
852place to the left. We show only the low-order eight bits of the binary 865bits of the binary pattern; leading bits all agree with the
853pattern; the rest are all zero. 866highest-order bit shown. As you can see, shifting left by one is
867equivalent to multiplying by two, whereas shifting right by one is
868equivalent to dividing by two and then rounding toward minus infinity.
854 869
855@example 870@example
856@group 871@group
857(lsh 5 1) 872(ash 7 1) @result{} 14
858 @result{} 10
859;; @r{Decimal 5 becomes decimal 10.}
86000000101 @result{} 00001010
861
862(lsh 7 1)
863 @result{} 14
864;; @r{Decimal 7 becomes decimal 14.} 873;; @r{Decimal 7 becomes decimal 14.}
86500000111 @result{} 00001110 874...000111
866@end group 875 @result{}
867@end example 876...001110
868
869@noindent
870As the examples illustrate, shifting the pattern of bits one place to
871the left produces a number that is twice the value of the previous
872number.
873
874Shifting a pattern of bits two places to the left produces results
875like this (with 8-bit binary numbers):
876
877@example
878@group
879(lsh 3 2)
880 @result{} 12
881;; @r{Decimal 3 becomes decimal 12.}
88200000011 @result{} 00001100
883@end group 877@end group
884@end example
885
886On the other hand, shifting one place to the right looks like this:
887 878
888@example
889@group 879@group
890(lsh 6 -1) 880(ash 7 -1) @result{} 3
891 @result{} 3 881...000111
892;; @r{Decimal 6 becomes decimal 3.} 882 @result{}
89300000110 @result{} 00000011 883...000011
894@end group 884@end group
895 885
896@group 886@group
897(lsh 5 -1) 887(ash -7 1) @result{} -14
898 @result{} 2 888...111001
899;; @r{Decimal 5 becomes decimal 2.} 889 @result{}
90000000101 @result{} 00000010 890...110010
901@end group 891@end group
902@end example
903
904@noindent
905As the example illustrates, shifting one place to the right divides the
906value of a positive integer by two, rounding downward.
907@end defun
908
909@defun ash integer1 count
910@cindex arithmetic shift
911@code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
912to the left @var{count} places, or to the right if @var{count}
913is negative.
914
915@code{ash} gives the same results as @code{lsh} except when
916@var{integer1} and @var{count} are both negative. In that case,
917@code{ash} puts ones in the empty bit positions on the left, while
918@code{lsh} puts zeros in those bit positions and requires
919@var{integer1} to be a fixnum.
920 892
921Thus, with @code{ash}, shifting the pattern of bits one place to the right
922looks like this:
923
924@example
925@group 893@group
926(ash -6 -1) @result{} -3 894(ash -7 -1) @result{} -4
927;; @r{Decimal @minus{}6 becomes decimal @minus{}3.} 895...111001
9281111...111010 (30 bits total)
929 @result{} 896 @result{}
9301111...111101 (30 bits total) 897...111100
931@end group 898@end group
932@end example 899@end example
933 900
934Here are other examples: 901Here are examples of shifting left or right by two bits:
935 902
936@c !!! Check if lined up in smallbook format! XDVI shows problem
937@c with smallbook but not with regular book! --rjc 16mar92
938@smallexample 903@smallexample
939@group 904@group
940 ; @r{ 30-bit binary values} 905 ; @r{ binary values}
941 906(ash 5 2) ; 5 = @r{...000101}
942(lsh 5 2) ; 5 = @r{0000...000101} 907 @result{} 20 ; = @r{...010100}
943 @result{} 20 ; = @r{0000...010100} 908(ash -5 2) ; -5 = @r{...111011}
944@end group 909 @result{} -20 ; = @r{...101100}
945@group
946(ash 5 2)
947 @result{} 20
948(lsh -5 2) ; -5 = @r{1111...111011}
949 @result{} -20 ; = @r{1111...101100}
950(ash -5 2)
951 @result{} -20
952@end group 910@end group
953@group 911@group
954(lsh 5 -2) ; 5 = @r{0000...000101} 912(ash 5 -2)
955 @result{} 1 ; = @r{0000...000001} 913 @result{} 1 ; = @r{...000001}
956@end group 914@end group
957@group 915@group
958(ash 5 -2) 916(ash -5 -2)
959 @result{} 1 917 @result{} -2 ; = @r{...111110}
960@end group 918@end group
919@end smallexample
920@end defun
921
922@defun lsh integer1 count
923@cindex logical shift
924@code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
925bits in @var{integer1} to the left @var{count} places, or to the right
926if @var{count} is negative, bringing zeros into the vacated bits. If
927@var{count} is negative, then @var{integer1} must be either a fixnum
928or a positive bignum, and @code{lsh} treats a negative fixnum as if it
929were unsigned by subtracting twice @code{most-negative-fixnum} before
930shifting, producing a nonnegative result. This quirky behavior dates
931back to when Emacs supported only fixnums; nowadays @code{ash} is a
932better choice.
933
934As @code{lsh} behaves like @code{ash} except when @var{integer1} and
935@var{count1} are both negative, the following examples focus on these
936exceptional cases. These examples assume 30-bit fixnums.
937
938@smallexample
961@group 939@group
962(lsh -5 -2) ; -5 = @r{1111...111011} 940 ; @r{ binary values}
963 @result{} 268435454 941(ash -7 -1) ; -7 = @r{...111111111111111111111111111001}
964 ; = @r{0011...111110} 942 @result{} -4 ; = @r{...111111111111111111111111111100}
943(lsh -7 -1)
944 @result{} 536870908 ; = @r{...011111111111111111111111111100}
965@end group 945@end group
966@group 946@group
967(ash -5 -2) ; -5 = @r{1111...111011} 947(ash -5 -2) ; -5 = @r{...111111111111111111111111111011}
968 @result{} -2 ; = @r{1111...111110} 948 @result{} -2 ; = @r{...111111111111111111111111111110}
949(lsh -5 -2)
950 @result{} 268435454 ; = @r{...001111111111111111111111111110}
969@end group 951@end group
970@end smallexample 952@end smallexample
971@end defun 953@end defun
@@ -999,23 +981,23 @@ because its binary representation consists entirely of ones. If
999 981
1000@smallexample 982@smallexample
1001@group 983@group
1002 ; @r{ 30-bit binary values} 984 ; @r{ binary values}
1003 985
1004(logand 14 13) ; 14 = @r{0000...001110} 986(logand 14 13) ; 14 = @r{...001110}
1005 ; 13 = @r{0000...001101} 987 ; 13 = @r{...001101}
1006 @result{} 12 ; 12 = @r{0000...001100} 988 @result{} 12 ; 12 = @r{...001100}
1007@end group 989@end group
1008 990
1009@group 991@group
1010(logand 14 13 4) ; 14 = @r{0000...001110} 992(logand 14 13 4) ; 14 = @r{...001110}
1011 ; 13 = @r{0000...001101} 993 ; 13 = @r{...001101}
1012 ; 4 = @r{0000...000100} 994 ; 4 = @r{...000100}
1013 @result{} 4 ; 4 = @r{0000...000100} 995 @result{} 4 ; 4 = @r{...000100}
1014@end group 996@end group
1015 997
1016@group 998@group
1017(logand) 999(logand)
1018 @result{} -1 ; -1 = @r{1111...111111} 1000 @result{} -1 ; -1 = @r{...111111}
1019@end group 1001@end group
1020@end smallexample 1002@end smallexample
1021@end defun 1003@end defun
@@ -1029,18 +1011,18 @@ passed just one argument, it returns that argument.
1029 1011
1030@smallexample 1012@smallexample
1031@group 1013@group
1032 ; @r{ 30-bit binary values} 1014 ; @r{ binary values}
1033 1015
1034(logior 12 5) ; 12 = @r{0000...001100} 1016(logior 12 5) ; 12 = @r{...001100}
1035 ; 5 = @r{0000...000101} 1017 ; 5 = @r{...000101}
1036 @result{} 13 ; 13 = @r{0000...001101} 1018 @result{} 13 ; 13 = @r{...001101}
1037@end group 1019@end group
1038 1020
1039@group 1021@group
1040(logior 12 5 7) ; 12 = @r{0000...001100} 1022(logior 12 5 7) ; 12 = @r{...001100}
1041 ; 5 = @r{0000...000101} 1023 ; 5 = @r{...000101}
1042 ; 7 = @r{0000...000111} 1024 ; 7 = @r{...000111}
1043 @result{} 15 ; 15 = @r{0000...001111} 1025 @result{} 15 ; 15 = @r{...001111}
1044@end group 1026@end group
1045@end smallexample 1027@end smallexample
1046@end defun 1028@end defun
@@ -1054,18 +1036,18 @@ result is 0, which is an identity element for this operation. If
1054 1036
1055@smallexample 1037@smallexample
1056@group 1038@group
1057 ; @r{ 30-bit binary values} 1039 ; @r{ binary values}
1058 1040
1059(logxor 12 5) ; 12 = @r{0000...001100} 1041(logxor 12 5) ; 12 = @r{...001100}
1060 ; 5 = @r{0000...000101} 1042 ; 5 = @r{...000101}
1061 @result{} 9 ; 9 = @r{0000...001001} 1043 @result{} 9 ; 9 = @r{...001001}
1062@end group 1044@end group
1063 1045
1064@group 1046@group
1065(logxor 12 5 7) ; 12 = @r{0000...001100} 1047(logxor 12 5 7) ; 12 = @r{...001100}
1066 ; 5 = @r{0000...000101} 1048 ; 5 = @r{...000101}
1067 ; 7 = @r{0000...000111} 1049 ; 7 = @r{...000111}
1068 @result{} 14 ; 14 = @r{0000...001110} 1050 @result{} 14 ; 14 = @r{...001110}
1069@end group 1051@end group
1070@end smallexample 1052@end smallexample
1071@end defun 1053@end defun
@@ -1078,9 +1060,9 @@ bit is one in the result if, and only if, the @var{n}th bit is zero in
1078@example 1060@example
1079(lognot 5) 1061(lognot 5)
1080 @result{} -6 1062 @result{} -6
1081;; 5 = @r{0000...000101} (30 bits total) 1063;; 5 = @r{...000101}
1082;; @r{becomes} 1064;; @r{becomes}
1083;; -6 = @r{1111...111010} (30 bits total) 1065;; -6 = @r{...111010}
1084@end example 1066@end example
1085@end defun 1067@end defun
1086 1068
@@ -1095,9 +1077,9 @@ its two's complement binary representation. The result is always
1095nonnegative. 1077nonnegative.
1096 1078
1097@example 1079@example
1098(logcount 43) ; 43 = #b101011 1080(logcount 43) ; 43 = @r{...000101011}
1099 @result{} 4 1081 @result{} 4
1100(logcount -43) ; -43 = #b111...1010101 1082(logcount -43) ; -43 = @r{...111010101}
1101 @result{} 3 1083 @result{} 3
1102@end example 1084@end example
1103@end defun 1085@end defun