<ahref="saturated__arithmetic_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="comment">// Copyright 2010-2018 Google LLC</span></div>
<divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> <spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aname="l00003"></a><spanclass="lineno"> 3</span> <spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aname="l00004"></a><spanclass="lineno"> 4</span> <spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aname="l00008"></a><spanclass="lineno"> 8</span> <spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aname="l00009"></a><spanclass="lineno"> 9</span> <spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aname="l00010"></a><spanclass="lineno"> 10</span> <spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aname="l00011"></a><spanclass="lineno"> 11</span> <spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aname="l00012"></a><spanclass="lineno"> 12</span> <spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aname="l00024"></a><spanclass="lineno"> 24</span> <spanclass="comment">// Implement two's complement addition and subtraction on int64s.</span></div>
<divclass="line"><aname="l00026"></a><spanclass="lineno"> 26</span> <spanclass="comment">// The C and C++ standards specify that the overflow of signed integers is</span></div>
<divclass="line"><aname="l00027"></a><spanclass="lineno"> 27</span> <spanclass="comment">// undefined. This is because of the different possible representations that may</span></div>
<divclass="line"><aname="l00028"></a><spanclass="lineno"> 28</span> <spanclass="comment">// be used for signed integers (one's complement, two's complement, sign and</span></div>
<divclass="line"><aname="l00029"></a><spanclass="lineno"> 29</span> <spanclass="comment">// magnitude). Such overflows are detected by Address Sanitizer with</span></div>
<divclass="line"><aname="l00032"></a><spanclass="lineno"> 32</span> <spanclass="comment">// Simple, portable overflow detection on current machines relies on</span></div>
<divclass="line"><aname="l00033"></a><spanclass="lineno"> 33</span> <spanclass="comment">// these two functions. For example, if the sign of the sum of two positive</span></div>
<divclass="line"><aname="l00034"></a><spanclass="lineno"> 34</span> <spanclass="comment">// integers is negative, there has been an overflow.</span></div>
<divclass="line"><aname="l00036"></a><spanclass="lineno"> 36</span> <spanclass="comment">// Note that the static assert will break if the code is compiled on machines</span></div>
<divclass="line"><aname="l00037"></a><spanclass="lineno"> 37</span> <spanclass="comment">// which do not use two's complement.</span></div>
<divclass="line"><aname="l00040"></a><spanclass="lineno"> 40</span> <spanclass="stringliteral">"The target architecture does not use two's complement."</span>);</div>
<divclass="line"><aname="l00046"></a><spanclass="lineno"> 46</span> <spanclass="stringliteral">"The target architecture does not use two's complement."</span>);</div>
<divclass="line"><aname="l00050"></a><spanclass="lineno"> 50</span> <spanclass="comment">// Helper function that returns true if an overflow has occured in computing</span></div>
<divclass="line"><aname="l00051"></a><spanclass="lineno"> 51</span> <spanclass="comment">// sum = x + y. sum is expected to be computed elsewhere.</span></div>
<divclass="line"><aname="l00052"></a><spanclass="lineno"><aclass="line"href="namespaceoperations__research.html#aa1a2e7e0cdde05b5ef898461b90eede0"> 52</a></span> <spanclass="keyword">inline</span><spanclass="keywordtype">bool</span><aclass="code"href="namespaceoperations__research.html#aa1a2e7e0cdde05b5ef898461b90eede0">AddHadOverflow</a>(<aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> x, <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> y, <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> sum) {</div>
<divclass="line"><aname="l00053"></a><spanclass="lineno"> 53</span> <spanclass="comment">// Overflow cannot occur if operands have different signs.</span></div>
<divclass="line"><aname="l00054"></a><spanclass="lineno"> 54</span> <spanclass="comment">// It can only occur if sign(x) == sign(y) and sign(sum) != sign(x),</span></div>
<divclass="line"><aname="l00055"></a><spanclass="lineno"> 55</span> <spanclass="comment">// which is equivalent to: sign(x) != sign(sum) && sign(y) != sign(sum).</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// This is captured when the expression below is negative.</span></div>
<divclass="line"><aname="l00061"></a><spanclass="lineno"><aclass="line"href="namespaceoperations__research.html#a5fd04dbb1346d8877b5ed54aa291ffbc"> 61</a></span> <spanclass="keyword">inline</span><spanclass="keywordtype">bool</span><aclass="code"href="namespaceoperations__research.html#a5fd04dbb1346d8877b5ed54aa291ffbc">SubHadOverflow</a>(<aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> x, <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> y, <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> diff) {</div>
<divclass="line"><aname="l00062"></a><spanclass="lineno"> 62</span> <spanclass="comment">// This is the same reasoning as for AddHadOverflow. We have x = diff + y.</span></div>
<divclass="line"><aname="l00063"></a><spanclass="lineno"> 63</span> <spanclass="comment">// The formula is the same, with 'x' and diff exchanged.</span></div>
<divclass="line"><aname="l00065"></a><spanclass="lineno"> 65</span> <spanclass="keywordflow">return</span><aclass="code"href="namespaceoperations__research.html#aa1a2e7e0cdde05b5ef898461b90eede0">AddHadOverflow</a>(diff, y, x);</div>
<divclass="line"><aname="l00068"></a><spanclass="lineno"> 68</span> <spanclass="comment">// A note on overflow treatment.</span></div>
<divclass="line"><aname="l00069"></a><spanclass="lineno"> 69</span> <spanclass="comment">// kint64min and kint64max are treated as infinity.</span></div>
<divclass="line"><aname="l00070"></a><spanclass="lineno"> 70</span> <spanclass="comment">// Thus if the computation overflows, the result is always kint64m(ax/in).</span></div>
<divclass="line"><aname="l00072"></a><spanclass="lineno"> 72</span> <spanclass="comment">// Note(user): this is actually wrong: when computing A-B, if A is kint64max</span></div>
<divclass="line"><aname="l00073"></a><spanclass="lineno"> 73</span> <spanclass="comment">// and B is finite, then A-B won't be kint64max: overflows aren't sticky.</span></div>
<divclass="line"><aname="l00074"></a><spanclass="lineno"> 74</span> <spanclass="comment">// TODO(user): consider making some operations overflow-sticky, some others</span></div>
<divclass="line"><aname="l00075"></a><spanclass="lineno"> 75</span> <spanclass="comment">// not, but make an explicit choice throughout.</span></div>
<divclass="line"><aname="l00077"></a><spanclass="lineno"> 77</span> <spanclass="keywordflow">return</span><aclass="code"href="namespaceoperations__research.html#aa1a2e7e0cdde05b5ef898461b90eede0">AddHadOverflow</a>(x, y, <aclass="code"href="namespaceoperations__research.html#a7765571e7ec374ba667a3ccd16dcd124">TwosComplementAddition</a>(x, y));</div>
<divclass="line"><aname="l00081"></a><spanclass="lineno"> 81</span> <spanclass="keywordflow">return</span><aclass="code"href="namespaceoperations__research.html#a5fd04dbb1346d8877b5ed54aa291ffbc">SubHadOverflow</a>(x, y, <aclass="code"href="namespaceoperations__research.html#a2510acb08f15d9d639b286e91dfae6f2">TwosComplementSubtraction</a>(x, y));</div>
<divclass="line"><aname="l00084"></a><spanclass="lineno"> 84</span> <spanclass="comment">// Performs *b += a and returns false iff the addition overflow or underflow.</span></div>
<divclass="line"><aname="l00085"></a><spanclass="lineno"> 85</span> <spanclass="comment">// This function only works for typed integer type (IntType<>).</span></div>
<divclass="line"><aname="l00088"></a><spanclass="lineno"> 88</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> x = <aclass="code"href="constraint__solver_2table_8cc.html#af730895c6c6ef6e03caaf6251192dfd2">a</a>.value();</div>
<divclass="line"><aname="l00089"></a><spanclass="lineno"> 89</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> y = <aclass="code"href="constraint__solver_2table_8cc.html#a344010e26426d6a13411648d988bc9b6">b</a>->value();</div>
<divclass="line"><aname="l00090"></a><spanclass="lineno"> 90</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> sum = <aclass="code"href="namespaceoperations__research.html#a7765571e7ec374ba667a3ccd16dcd124">TwosComplementAddition</a>(x, y);</div>
<divclass="line"><aname="l00091"></a><spanclass="lineno"> 91</span> <spanclass="keywordflow">if</span> (<aclass="code"href="namespaceoperations__research.html#aa1a2e7e0cdde05b5ef898461b90eede0">AddHadOverflow</a>(x, y, sum)) <spanclass="keywordflow">return</span><spanclass="keyword">false</span>;</div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// Returns kint64max if x >= 0 and kint64min if x < 0.</span></div>
<divclass="line"><aname="l00098"></a><spanclass="lineno"> 98</span> <spanclass="comment">// return kint64max if x >= 0 or kint64max + 1 (== kint64min) if x < 0.</span></div>
<divclass="line"><aname="l00108"></a><spanclass="lineno"> 108</span> <spanclass="comment">// TODO(user): port this to other architectures.</span></div>
<divclass="line"><aname="l00110"></a><spanclass="lineno"> 110</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> cap = <aclass="code"href="namespaceoperations__research.html#a29f503f49388eeb29b06c6423d0aaeb6">CapWithSignOf</a>(x);</div>
<divclass="line"><aname="l00111"></a><spanclass="lineno"> 111</span> <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> result = x;</div>
<divclass="line"><aname="l00138"></a><spanclass="lineno"> 138</span> <spanclass="comment">// TODO(user): port this to other architectures.</span></div>
<divclass="line"><aname="l00140"></a><spanclass="lineno"> 140</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> cap = <aclass="code"href="namespaceoperations__research.html#a29f503f49388eeb29b06c6423d0aaeb6">CapWithSignOf</a>(x);</div>
<divclass="line"><aname="l00141"></a><spanclass="lineno"> 141</span> <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> result = x;</div>
<divclass="line"><aname="l00166"></a><spanclass="lineno"> 166</span> <spanclass="comment">// Returns an unsigned int equal to the absolute value of n, in a way that</span></div>
<divclass="line"><aname="l00167"></a><spanclass="lineno"> 167</span> <spanclass="comment">// will not produce overflows.</span></div>
<divclass="line"><aname="l00173"></a><spanclass="lineno"> 173</span> <spanclass="comment">// The generic algorithm computes a bound on the number of bits necessary to</span></div>
<divclass="line"><aname="l00174"></a><spanclass="lineno"> 174</span> <spanclass="comment">// store the result. For this it uses the position of the most significant bits</span></div>
<divclass="line"><aname="l00175"></a><spanclass="lineno"> 175</span> <spanclass="comment">// of each of the arguments.</span></div>
<divclass="line"><aname="l00176"></a><spanclass="lineno"> 176</span> <spanclass="comment">// If the result needs at least 64 bits, then return a capped value.</span></div>
<divclass="line"><aname="l00177"></a><spanclass="lineno"> 177</span> <spanclass="comment">// If the result needs at most 63 bits, then return the product.</span></div>
<divclass="line"><aname="l00178"></a><spanclass="lineno"> 178</span> <spanclass="comment">// Otherwise, the result may use 63 or 64 bits: compute the product</span></div>
<divclass="line"><aname="l00179"></a><spanclass="lineno"> 179</span> <spanclass="comment">// as a uint64, and cap it if necessary.</span></div>
<divclass="line"><aname="l00183"></a><spanclass="lineno"> 183</span> <spanclass="comment">// Let MSB(x) denote the most significant bit of x. We have:</span></div>
<divclass="line"><aname="l00189"></a><spanclass="lineno"> 189</span> <spanclass="comment">// Catch a == 0 or b == 0 now, as MostSignificantBitPosition64(0) == 0.</span></div>
<divclass="line"><aname="l00190"></a><spanclass="lineno"> 190</span> <spanclass="comment">// TODO(user): avoid this by writing function Log2(a) with Log2(0) == -1.</span></div>
<divclass="line"><aname="l00194"></a><spanclass="lineno"> 194</span> <spanclass="comment">// The corner case is when msb_sum == 62, i.e. at least 63 bits will be</span></div>
<divclass="line"><aname="l00195"></a><spanclass="lineno"> 195</span> <spanclass="comment">// needed to store the product. The following product will never overflow</span></div>
<divclass="line"><aname="l00196"></a><spanclass="lineno"> 196</span> <spanclass="comment">// on uint64, since msb_sum == 62.</span></div>
<divclass="line"><aname="l00198"></a><spanclass="lineno"> 198</span> <spanclass="comment">// The overflow cases are captured by one of the following conditions:</span></div>
<divclass="line"><aname="l00201"></a><spanclass="lineno"> 201</span> <spanclass="comment">// These can be optimized as follows (and if the condition is false, it is</span></div>
<divclass="line"><aname="l00202"></a><spanclass="lineno"> 202</span> <spanclass="comment">// safe to compute x * y.</span></div>
<divclass="line"><aname="l00209"></a><spanclass="lineno"> 209</span> <spanclass="comment">// TODO(user): port this to other architectures.</span></div>
<divclass="line"><aname="l00211"></a><spanclass="lineno"> 211</span> <spanclass="comment">// cap = kint64max if x and y have the same sign, cap = kint64min</span></div>
<divclass="line"><aname="l00213"></a><spanclass="lineno"> 213</span> <spanclass="keyword">const</span><aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> cap = <aclass="code"href="namespaceoperations__research.html#a29f503f49388eeb29b06c6423d0aaeb6">CapWithSignOf</a>(x ^ y);</div>
<divclass="line"><aname="l00214"></a><spanclass="lineno"> 214</span> <aclass="code"href="integral__types_8h.html#a7cde0074dfd288f2d70c0e035dacb28a">int64</a> result = x;</div>
<divclass="line"><aname="l00215"></a><spanclass="lineno"> 215</span> <spanclass="comment">// Here, we use the fact that imul of two signed 64-integers returns a 128-bit</span></div>
<divclass="line"><aname="l00216"></a><spanclass="lineno"> 216</span> <spanclass="comment">// result -- we care about the lower 64 bits. More importantly, imul also sets</span></div>
<divclass="line"><aname="l00217"></a><spanclass="lineno"> 217</span> <spanclass="comment">// the carry flag if 64 bits were not enough.</span></div>
<divclass="line"><aname="l00218"></a><spanclass="lineno"> 218</span> <spanclass="comment">// We therefore use cmovc to return cap if the carry was set.</span></div>
<divclass="ttc"id="anamespaceoperations__research_html"><divclass="ttname"><ahref="namespaceoperations__research.html">operations_research</a></div><divclass="ttdoc">The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...</div><divclass="ttdef"><b>Definition:</b><ahref="dense__doubly__linked__list_8h_source.html#l00021">dense_doubly_linked_list.h:21</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_html_ae1bd0133149b70b678b123524541aaad"><divclass="ttname"><ahref="namespaceoperations__research.html#ae1bd0133149b70b678b123524541aaad">operations_research::SafeAddInto</a></div><divclass="ttdeci">bool SafeAddInto(IntegerType a, IntegerType *b)</div><divclass="ttdef"><b>Definition:</b><ahref="saturated__arithmetic_8h_source.html#l00087">saturated_arithmetic.h:87</a></div></div>