| ... | @@ -1244,8 +1244,9 @@ fn divide(a: i32, b: i32) i32 { | ... | @@ -1244,8 +1244,9 @@ fn divide(a: i32, b: i32) i32 { |
| 1244 | </p> | 1244 | </p> |
| 1245 | <p> | 1245 | <p> |
| 1246 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on | 1246 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on |
| 1247 | integer overflow. Also available are operations such as {#syntax#}+%{#endsyntax#} and | 1247 | integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets. |
| 1248 | {#syntax#}-%{#endsyntax#} which are defined to have wrapping arithmetic on all targets. | 1248 | {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic |
| | 1249 | while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic. |
| 1249 | </p> | 1250 | </p> |
| 1250 | <p> | 1251 | <p> |
| 1251 | Zig supports arbitrary bit-width integers, referenced by using | 1252 | Zig supports arbitrary bit-width integers, referenced by using |
| ... | @@ -1395,6 +1396,24 @@ a +%= b{#endsyntax#}</pre></th> | ... | @@ -1395,6 +1396,24 @@ a +%= b{#endsyntax#}</pre></th> |
| 1395 | <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre> | 1396 | <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre> |
| 1396 | </td> | 1397 | </td> |
| 1397 | </tr> | 1398 | </tr> |
| | 1399 | <tr> |
| | 1400 | <td><pre>{#syntax#}a +| b |
| | 1401 | a +|= b{#endsyntax#}</pre></td> |
| | 1402 | <td> |
| | 1403 | <ul> |
| | 1404 | <li>{#link|Integers#}</li> |
| | 1405 | </ul> |
| | 1406 | </td> |
| | 1407 | <td>Saturating Addition. |
| | 1408 | <ul> |
| | 1409 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| | 1410 | <li>See also {#link|@addWithSaturation#}.</li> |
| | 1411 | </ul> |
| | 1412 | </td> |
| | 1413 | <td> |
| | 1414 | <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +| 1 == @as(u32, std.math.maxInt(u32)){#endsyntax#}</pre> |
| | 1415 | </td> |
| | 1416 | </tr> |
| 1398 | <tr> | 1417 | <tr> |
| 1399 | <th scope="row"><pre>{#syntax#}a - b | 1418 | <th scope="row"><pre>{#syntax#}a - b |
| 1400 | a -= b{#endsyntax#}</pre></th> | 1419 | a -= b{#endsyntax#}</pre></th> |
| ... | @@ -1434,6 +1453,24 @@ a -%= b{#endsyntax#}</pre></th> | ... | @@ -1434,6 +1453,24 @@ a -%= b{#endsyntax#}</pre></th> |
| 1434 | <pre>{#syntax#}@as(u32, 0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre> | 1453 | <pre>{#syntax#}@as(u32, 0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre> |
| 1435 | </td> | 1454 | </td> |
| 1436 | </tr> | 1455 | </tr> |
| | 1456 | <tr> |
| | 1457 | <td><pre>{#syntax#}a -| b |
| | 1458 | a -|= b{#endsyntax#}</pre></td> |
| | 1459 | <td> |
| | 1460 | <ul> |
| | 1461 | <li>{#link|Integers#}</li> |
| | 1462 | </ul> |
| | 1463 | </td> |
| | 1464 | <td>Saturating Subtraction. |
| | 1465 | <ul> |
| | 1466 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| | 1467 | <li>See also {#link|@subWithSaturation#}.</li> |
| | 1468 | </ul> |
| | 1469 | </td> |
| | 1470 | <td> |
| | 1471 | <pre>{#syntax#}@as(u32, 0) -| 1 == 0{#endsyntax#}</pre> |
| | 1472 | </td> |
| | 1473 | </tr> |
| 1437 | <tr> | 1474 | <tr> |
| 1438 | <th scope="row"><pre>{#syntax#}-a{#endsyntax#}</pre></th> | 1475 | <th scope="row"><pre>{#syntax#}-a{#endsyntax#}</pre></th> |
| 1439 | <td> | 1476 | <td> |
| ... | @@ -1508,6 +1545,24 @@ a *%= b{#endsyntax#}</pre></th> | ... | @@ -1508,6 +1545,24 @@ a *%= b{#endsyntax#}</pre></th> |
| 1508 | <pre>{#syntax#}@as(u8, 200) *% 2 == 144{#endsyntax#}</pre> | 1545 | <pre>{#syntax#}@as(u8, 200) *% 2 == 144{#endsyntax#}</pre> |
| 1509 | </td> | 1546 | </td> |
| 1510 | </tr> | 1547 | </tr> |
| | 1548 | <tr> |
| | 1549 | <td><pre>{#syntax#}a *| b |
| | 1550 | a *|= b{#endsyntax#}</pre></td> |
| | 1551 | <td> |
| | 1552 | <ul> |
| | 1553 | <li>{#link|Integers#}</li> |
| | 1554 | </ul> |
| | 1555 | </td> |
| | 1556 | <td>Saturating Multiplication. |
| | 1557 | <ul> |
| | 1558 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| | 1559 | <li>See also {#link|@mulWithSaturation#}.</li> |
| | 1560 | </ul> |
| | 1561 | </td> |
| | 1562 | <td> |
| | 1563 | <pre>{#syntax#}@as(u8, 200) *| 2 == 255{#endsyntax#}</pre> |
| | 1564 | </td> |
| | 1565 | </tr> |
| 1511 | <tr> | 1566 | <tr> |
| 1512 | <th scope="row"><pre>{#syntax#}a / b | 1567 | <th scope="row"><pre>{#syntax#}a / b |
| 1513 | a /= b{#endsyntax#}</pre></th> | 1568 | a /= b{#endsyntax#}</pre></th> |
| ... | @@ -1577,6 +1632,24 @@ a <<= b{#endsyntax#}</pre></th> | ... | @@ -1577,6 +1632,24 @@ a <<= b{#endsyntax#}</pre></th> |
| 1577 | <pre>{#syntax#}1 << 8 == 256{#endsyntax#}</pre> | 1632 | <pre>{#syntax#}1 << 8 == 256{#endsyntax#}</pre> |
| 1578 | </td> | 1633 | </td> |
| 1579 | </tr> | 1634 | </tr> |
| | 1635 | <tr> |
| | 1636 | <td><pre>{#syntax#}a <<| b |
| | 1637 | a <<|= b{#endsyntax#}</pre></td> |
| | 1638 | <td> |
| | 1639 | <ul> |
| | 1640 | <li>{#link|Integers#}</li> |
| | 1641 | </ul> |
| | 1642 | </td> |
| | 1643 | <td>Saturating Bit Shift Left. |
| | 1644 | <ul> |
| | 1645 | <li>See also {#link|@shlExact#}.</li> |
| | 1646 | <li>See also {#link|@shlWithOverflow#}.</li> |
| | 1647 | </ul> |
| | 1648 | </td> |
| | 1649 | <td> |
| | 1650 | <pre>{#syntax#}@as(u8, 1) <<| 8 == 255{#endsyntax#}</pre> |
| | 1651 | </td> |
| | 1652 | </tr> |
| 1580 | <tr> | 1653 | <tr> |
| 1581 | <th scope="row"><pre>{#syntax#}a >> b | 1654 | <th scope="row"><pre>{#syntax#}a >> b |
| 1582 | a >>= b{#endsyntax#}</pre></th> | 1655 | a >>= b{#endsyntax#}</pre></th> |
| ... | @@ -1968,14 +2041,14 @@ const B = error{Two}; | ... | @@ -1968,14 +2041,14 @@ const B = error{Two}; |
| 1968 | a!b | 2041 | a!b |
| 1969 | x{} | 2042 | x{} |
| 1970 | !x -x -%x ~x &x ?x | 2043 | !x -x -%x ~x &x ?x |
| 1971 | * / % ** *% || | 2044 | * / % ** *% *| || |
| 1972 | + - ++ +% -% | 2045 | + - ++ +% -% +| -| |
| 1973 | << >> | 2046 | << >> <<| |
| 1974 | & ^ | orelse catch | 2047 | & ^ | orelse catch |
| 1975 | == != < > <= >= | 2048 | == != < > <= >= |
| 1976 | and | 2049 | and |
| 1977 | or | 2050 | or |
| 1978 | = *= /= %= += -= <<= >>= &= ^= |={#endsyntax#}</pre> | 2051 | = *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}</pre> |
| 1979 | {#header_close#} | 2052 | {#header_close#} |
| 1980 | {#header_close#} | 2053 | {#header_close#} |
| 1981 | {#header_open|Arrays#} | 2054 | {#header_open|Arrays#} |
| ... | @@ -11839,6 +11912,7 @@ AssignOp | ... | @@ -11839,6 +11912,7 @@ AssignOp |
| 11839 | / PLUSEQUAL | 11912 | / PLUSEQUAL |
| 11840 | / MINUSEQUAL | 11913 | / MINUSEQUAL |
| 11841 | / LARROW2EQUAL | 11914 | / LARROW2EQUAL |
| | 11915 | / LARROW2PIPEEQUAL |
| 11842 | / RARROW2EQUAL | 11916 | / RARROW2EQUAL |
| 11843 | / AMPERSANDEQUAL | 11917 | / AMPERSANDEQUAL |
| 11844 | / CARETEQUAL | 11918 | / CARETEQUAL |
| ... | @@ -11873,6 +11947,8 @@ AdditionOp | ... | @@ -11873,6 +11947,8 @@ AdditionOp |
| 11873 | / PLUS2 | 11947 | / PLUS2 |
| 11874 | / PLUSPERCENT | 11948 | / PLUSPERCENT |
| 11875 | / MINUSPERCENT | 11949 | / MINUSPERCENT |
| | 11950 | / PLUSPIPE |
| | 11951 | / MINUSPIPE |
| 11876 | | 11952 | |
| 11877 | MultiplyOp | 11953 | MultiplyOp |
| 11878 | &lt;- PIPE2 | 11954 | &lt;- PIPE2 |
| ... | @@ -11881,6 +11957,7 @@ MultiplyOp | ... | @@ -11881,6 +11957,7 @@ MultiplyOp |
| 11881 | / PERCENT | 11957 | / PERCENT |
| 11882 | / ASTERISK2 | 11958 | / ASTERISK2 |
| 11883 | / ASTERISKPERCENT | 11959 | / ASTERISKPERCENT |
| | 11960 | / ASTERISKPIPE |
| 11884 | | 11961 | |
| 11885 | PrefixOp | 11962 | PrefixOp |
| 11886 | &lt;- EXCLAMATIONMARK | 11963 | &lt;- EXCLAMATIONMARK |
| ... | @@ -12044,6 +12121,8 @@ ASTERISK2 &lt;- '**' skip | ... | @@ -12044,6 +12121,8 @@ ASTERISK2 &lt;- '**' skip |
| 12044 | ASTERISKEQUAL &lt;- '*=' skip | 12121 | ASTERISKEQUAL &lt;- '*=' skip |
| 12045 | ASTERISKPERCENT &lt;- '*%' ![=] skip | 12122 | ASTERISKPERCENT &lt;- '*%' ![=] skip |
| 12046 | ASTERISKPERCENTEQUAL &lt;- '*%=' skip | 12123 | ASTERISKPERCENTEQUAL &lt;- '*%=' skip |
| | 12124 | ASTERISKPIPE &lt;- '*|' ![=] skip |
| | 12125 | ASTERISKPIPEEQUAL &lt;- '*|=' skip |
| 12047 | CARET &lt;- '^' ![=] skip | 12126 | CARET &lt;- '^' ![=] skip |
| 12048 | CARETEQUAL &lt;- '^=' skip | 12127 | CARETEQUAL &lt;- '^=' skip |
| 12049 | COLON &lt;- ':' skip | 12128 | COLON &lt;- ':' skip |
| ... | @@ -12060,6 +12139,8 @@ EXCLAMATIONMARK &lt;- '!' ![=] skip | ... | @@ -12060,6 +12139,8 @@ EXCLAMATIONMARK &lt;- '!' ![=] skip |
| 12060 | EXCLAMATIONMARKEQUAL &lt;- '!=' skip | 12139 | EXCLAMATIONMARKEQUAL &lt;- '!=' skip |
| 12061 | LARROW &lt;- '&lt;' ![&lt;=] skip | 12140 | LARROW &lt;- '&lt;' ![&lt;=] skip |
| 12062 | LARROW2 &lt;- '&lt;&lt;' ![=] skip | 12141 | LARROW2 &lt;- '&lt;&lt;' ![=] skip |
| | 12142 | LARROW2PIPE &lt;- '&lt;&lt;|' ![=] skip |
| | 12143 | LARROW2PIPEEQUAL &lt;- '&lt;&lt;|=' ![=] skip |
| 12063 | LARROW2EQUAL &lt;- '&lt;&lt;=' skip | 12144 | LARROW2EQUAL &lt;- '&lt;&lt;=' skip |
| 12064 | LARROWEQUAL &lt;- '&lt;=' skip | 12145 | LARROWEQUAL &lt;- '&lt;=' skip |
| 12065 | LBRACE &lt;- '{' skip | 12146 | LBRACE &lt;- '{' skip |
| ... | @@ -12069,6 +12150,8 @@ MINUS &lt;- '-' ![%=&gt;] skip | ... | @@ -12069,6 +12150,8 @@ MINUS &lt;- '-' ![%=&gt;] skip |
| 12069 | MINUSEQUAL &lt;- '-=' skip | 12150 | MINUSEQUAL &lt;- '-=' skip |
| 12070 | MINUSPERCENT &lt;- '-%' ![=] skip | 12151 | MINUSPERCENT &lt;- '-%' ![=] skip |
| 12071 | MINUSPERCENTEQUAL &lt;- '-%=' skip | 12152 | MINUSPERCENTEQUAL &lt;- '-%=' skip |
| | 12153 | MINUSPIPE &lt;- '-|' ![=] skip |
| | 12154 | MINUSPIPEEQUAL &lt;- '-|=' skip |
| 12072 | MINUSRARROW &lt;- '-&gt;' skip | 12155 | MINUSRARROW &lt;- '-&gt;' skip |
| 12073 | PERCENT &lt;- '%' ![=] skip | 12156 | PERCENT &lt;- '%' ![=] skip |
| 12074 | PERCENTEQUAL &lt;- '%=' skip | 12157 | PERCENTEQUAL &lt;- '%=' skip |
| ... | @@ -12080,6 +12163,8 @@ PLUS2 &lt;- '++' skip | ... | @@ -12080,6 +12163,8 @@ PLUS2 &lt;- '++' skip |
| 12080 | PLUSEQUAL &lt;- '+=' skip | 12163 | PLUSEQUAL &lt;- '+=' skip |
| 12081 | PLUSPERCENT &lt;- '+%' ![=] skip | 12164 | PLUSPERCENT &lt;- '+%' ![=] skip |
| 12082 | PLUSPERCENTEQUAL &lt;- '+%=' skip | 12165 | PLUSPERCENTEQUAL &lt;- '+%=' skip |
| | 12166 | PLUSPIPE &lt;- '+|' ![=] skip |
| | 12167 | PLUSPIPEEQUAL &lt;- '+|=' skip |
| 12083 | LETTERC &lt;- 'c' skip | 12168 | LETTERC &lt;- 'c' skip |
| 12084 | QUESTIONMARK &lt;- '?' skip | 12169 | QUESTIONMARK &lt;- '?' skip |
| 12085 | RARROW &lt;- '&gt;' ![&gt;=] skip | 12170 | RARROW &lt;- '&gt;' ![&gt;=] skip |