1
+ /**
2
+ * detect_product_overflow.c
3
+ * detect_product_overflow_u64 detects numeric overflow in the multiplication
4
+ * of two uint64_t numbers without relying on potentially non-portable
5
+ * 128-bit integer types.
6
+ *
7
+ * Written in 2019 by Ben Tesch.
8
+ * Originally distributed at https://github.com/slugrustle/numerical_routines
9
+ *
10
+ * To the extent possible under law, the author has dedicated all copyright
11
+ * and related and neighboring rights to this software to the public domain
12
+ * worldwide.This software is distributed without any warranty.
13
+ * The text of the CC0 Public Domain Dedication should be reproduced at the
14
+ * end of this file. If not, see http ://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #include "detect_product_overflow.h"
18
+
19
+ /**
20
+ * Returns true if the product a * b would overflow the range
21
+ * of a uin64_t and false otherwise.
22
+ */
23
+ bool detect_product_overflow_u64 (const uint64_t a , const uint64_t b ) {
24
+ /**
25
+ * a * b = 2^64 * (a_hi * b_hi) +
26
+ * 2^32 * (a_hi * b_lo + a_lo * b_hi) +
27
+ * a_lo * b_lo
28
+ * All of the products a_hi*b_hi, a_hi*b_lo,
29
+ * a_lo*b_hi, and a_lo*b_lo will be on the range
30
+ * [0,2^64-2^33+1].
31
+ */
32
+ uint64_t a_hi = (a & 0xFFFFFFFF00000000ull ) >> 32 ;
33
+ uint64_t a_lo = a & 0x00000000FFFFFFFFull ;
34
+ uint64_t b_hi = (b & 0xFFFFFFFF00000000ull ) >> 32 ;
35
+ uint64_t b_lo = b & 0x00000000FFFFFFFFull ;
36
+
37
+ if (a_hi * b_hi > 0ull ) return true;
38
+ /**
39
+ * Now we know that a_hi * b_hi == 0.
40
+ * Consequently,
41
+ * a * b = 2^32 * (a_hi * b_lo + a_lo * b_hi) +
42
+ * a_lo * b_lo
43
+ */
44
+
45
+ uint64_t mid_prod_1 = a_hi * b_lo ;
46
+ uint64_t mid_prod_2 = a_lo * b_hi ;
47
+
48
+ if (UINT64_MAX - mid_prod_2 < mid_prod_1 ) return true;
49
+ /* Now we know that mid_prod_1 + mid_prod_2 <= UINT64_MAX. */
50
+ uint64_t mid_prod = mid_prod_1 + mid_prod_2 ;
51
+
52
+ if (mid_prod >= (1ull << 32 )) return true;
53
+ /* Now we know that 2^32 * mid_prod < UINT64_MAX. */
54
+ if (UINT64_MAX - a_lo * b_lo < mid_prod << 32 ) return true;
55
+
56
+ return false;
57
+ }
58
+
59
+ /*
60
+ Creative Commons Legal Code
61
+
62
+ CC0 1.0 Universal
63
+
64
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
65
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
66
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
67
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
68
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
69
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
70
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
71
+ HEREUNDER.
72
+
73
+ Statement of Purpose
74
+
75
+ The laws of most jurisdictions throughout the world automatically confer
76
+ exclusive Copyright and Related Rights (defined below) upon the creator
77
+ and subsequent owner(s) (each and all, an "owner") of an original work of
78
+ authorship and/or a database (each, a "Work").
79
+
80
+ Certain owners wish to permanently relinquish those rights to a Work for
81
+ the purpose of contributing to a commons of creative, cultural and
82
+ scientific works ("Commons") that the public can reliably and without fear
83
+ of later claims of infringement build upon, modify, incorporate in other
84
+ works, reuse and redistribute as freely as possible in any form whatsoever
85
+ and for any purposes, including without limitation commercial purposes.
86
+ These owners may contribute to the Commons to promote the ideal of a free
87
+ culture and the further production of creative, cultural and scientific
88
+ works, or to gain reputation or greater distribution for their Work in
89
+ part through the use and efforts of others.
90
+
91
+ For these and/or other purposes and motivations, and without any
92
+ expectation of additional consideration or compensation, the person
93
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
94
+ is an owner of Copyright and Related Rights in the Work, voluntarily
95
+ elects to apply CC0 to the Work and publicly distribute the Work under its
96
+ terms, with knowledge of his or her Copyright and Related Rights in the
97
+ Work and the meaning and intended legal effect of CC0 on those rights.
98
+
99
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
100
+ protected by copyright and related or neighboring rights ("Copyright and
101
+ Related Rights"). Copyright and Related Rights include, but are not
102
+ limited to, the following:
103
+
104
+ i. the right to reproduce, adapt, distribute, perform, display,
105
+ communicate, and translate a Work;
106
+ ii. moral rights retained by the original author(s) and/or performer(s);
107
+ iii. publicity and privacy rights pertaining to a person's image or
108
+ likeness depicted in a Work;
109
+ iv. rights protecting against unfair competition in regards to a Work,
110
+ subject to the limitations in paragraph 4(a), below;
111
+ v. rights protecting the extraction, dissemination, use and reuse of data
112
+ in a Work;
113
+ vi. database rights (such as those arising under Directive 96/9/EC of the
114
+ European Parliament and of the Council of 11 March 1996 on the legal
115
+ protection of databases, and under any national implementation
116
+ thereof, including any amended or successor version of such
117
+ directive); and
118
+ vii. other similar, equivalent or corresponding rights throughout the
119
+ world based on applicable law or treaty, and any national
120
+ implementations thereof.
121
+
122
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
123
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
124
+ irrevocably and unconditionally waives, abandons, and surrenders all of
125
+ Affirmer's Copyright and Related Rights and associated claims and causes
126
+ of action, whether now known or unknown (including existing as well as
127
+ future claims and causes of action), in the Work (i) in all territories
128
+ worldwide, (ii) for the maximum duration provided by applicable law or
129
+ treaty (including future time extensions), (iii) in any current or future
130
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
131
+ including without limitation commercial, advertising or promotional
132
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
133
+ member of the public at large and to the detriment of Affirmer's heirs and
134
+ successors, fully intending that such Waiver shall not be subject to
135
+ revocation, rescission, cancellation, termination, or any other legal or
136
+ equitable action to disrupt the quiet enjoyment of the Work by the public
137
+ as contemplated by Affirmer's express Statement of Purpose.
138
+
139
+ 3. Public License Fallback. Should any part of the Waiver for any reason
140
+ be judged legally invalid or ineffective under applicable law, then the
141
+ Waiver shall be preserved to the maximum extent permitted taking into
142
+ account Affirmer's express Statement of Purpose. In addition, to the
143
+ extent the Waiver is so judged Affirmer hereby grants to each affected
144
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
145
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
146
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
147
+ maximum duration provided by applicable law or treaty (including future
148
+ time extensions), (iii) in any current or future medium and for any number
149
+ of copies, and (iv) for any purpose whatsoever, including without
150
+ limitation commercial, advertising or promotional purposes (the
151
+ "License"). The License shall be deemed effective as of the date CC0 was
152
+ applied by Affirmer to the Work. Should any part of the License for any
153
+ reason be judged legally invalid or ineffective under applicable law, such
154
+ partial invalidity or ineffectiveness shall not invalidate the remainder
155
+ of the License, and in such case Affirmer hereby affirms that he or she
156
+ will not (i) exercise any of his or her remaining Copyright and Related
157
+ Rights in the Work or (ii) assert any associated claims and causes of
158
+ action with respect to the Work, in either case contrary to Affirmer's
159
+ express Statement of Purpose.
160
+
161
+ 4. Limitations and Disclaimers.
162
+
163
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
164
+ surrendered, licensed or otherwise affected by this document.
165
+ b. Affirmer offers the Work as-is and makes no representations or
166
+ warranties of any kind concerning the Work, express, implied,
167
+ statutory or otherwise, including without limitation warranties of
168
+ title, merchantability, fitness for a particular purpose, non
169
+ infringement, or the absence of latent or other defects, accuracy, or
170
+ the present or absence of errors, whether or not discoverable, all to
171
+ the greatest extent permissible under applicable law.
172
+ c. Affirmer disclaims responsibility for clearing rights of other persons
173
+ that may apply to the Work or any use thereof, including without
174
+ limitation any person's Copyright and Related Rights in the Work.
175
+ Further, Affirmer disclaims responsibility for obtaining any necessary
176
+ consents, permissions or other rights required for any use of the
177
+ Work.
178
+ d. Affirmer understands and acknowledges that Creative Commons is not a
179
+ party to this document and has no duty or obligation with respect to
180
+ this CC0 or use of the Work.
181
+ */
0 commit comments