Commit 0f246257be
Changed files (1)
doc/langref.html.in
@@ -1244,8 +1244,9 @@ fn divide(a: i32, b: i32) i32 {
</p>
<p>
Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on
- integer overflow. Also available are operations such as {#syntax#}+%{#endsyntax#} and
- {#syntax#}-%{#endsyntax#} which are defined to have wrapping arithmetic on all targets.
+ integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
+ {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic
+ while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic.
</p>
<p>
Zig supports arbitrary bit-width integers, referenced by using
@@ -1395,6 +1396,24 @@ a +%= b{#endsyntax#}</pre></th>
<pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre>
</td>
</tr>
+ <tr>
+ <td><pre>{#syntax#}a +| b
+a +|= b{#endsyntax#}</pre></td>
+ <td>
+ <ul>
+ <li>{#link|Integers#}</li>
+ </ul>
+ </td>
+ <td>Saturating Addition.
+ <ul>
+ <li>Invokes {#link|Peer Type Resolution#} for the operands.</li>
+ <li>See also {#link|@addWithSaturation#}.</li>
+ </ul>
+ </td>
+ <td>
+ <pre>{#syntax#}@as(u32, std.math.maxInt(u32)) +| 1 == @as(u32, std.math.maxInt(u32)){#endsyntax#}</pre>
+ </td>
+ </tr>
<tr>
<th scope="row"><pre>{#syntax#}a - b
a -= b{#endsyntax#}</pre></th>
@@ -1434,6 +1453,24 @@ a -%= b{#endsyntax#}</pre></th>
<pre>{#syntax#}@as(u32, 0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre>
</td>
</tr>
+ <tr>
+ <td><pre>{#syntax#}a -| b
+a -|= b{#endsyntax#}</pre></td>
+ <td>
+ <ul>
+ <li>{#link|Integers#}</li>
+ </ul>
+ </td>
+ <td>Saturating Subtraction.
+ <ul>
+ <li>Invokes {#link|Peer Type Resolution#} for the operands.</li>
+ <li>See also {#link|@subWithSaturation#}.</li>
+ </ul>
+ </td>
+ <td>
+ <pre>{#syntax#}@as(u32, 0) -| 1 == 0{#endsyntax#}</pre>
+ </td>
+ </tr>
<tr>
<th scope="row"><pre>{#syntax#}-a{#endsyntax#}</pre></th>
<td>
@@ -1508,6 +1545,24 @@ a *%= b{#endsyntax#}</pre></th>
<pre>{#syntax#}@as(u8, 200) *% 2 == 144{#endsyntax#}</pre>
</td>
</tr>
+ <tr>
+ <td><pre>{#syntax#}a *| b
+a *|= b{#endsyntax#}</pre></td>
+ <td>
+ <ul>
+ <li>{#link|Integers#}</li>
+ </ul>
+ </td>
+ <td>Saturating Multiplication.
+ <ul>
+ <li>Invokes {#link|Peer Type Resolution#} for the operands.</li>
+ <li>See also {#link|@mulWithSaturation#}.</li>
+ </ul>
+ </td>
+ <td>
+ <pre>{#syntax#}@as(u8, 200) *| 2 == 255{#endsyntax#}</pre>
+ </td>
+ </tr>
<tr>
<th scope="row"><pre>{#syntax#}a / b
a /= b{#endsyntax#}</pre></th>
@@ -1577,6 +1632,24 @@ a <<= b{#endsyntax#}</pre></th>
<pre>{#syntax#}1 << 8 == 256{#endsyntax#}</pre>
</td>
</tr>
+ <tr>
+ <td><pre>{#syntax#}a <<| b
+a <<|= b{#endsyntax#}</pre></td>
+ <td>
+ <ul>
+ <li>{#link|Integers#}</li>
+ </ul>
+ </td>
+ <td>Saturating Bit Shift Left.
+ <ul>
+ <li>See also {#link|@shlExact#}.</li>
+ <li>See also {#link|@shlWithOverflow#}.</li>
+ </ul>
+ </td>
+ <td>
+ <pre>{#syntax#}@as(u8, 1) <<| 8 == 255{#endsyntax#}</pre>
+ </td>
+ </tr>
<tr>
<th scope="row"><pre>{#syntax#}a >> b
a >>= b{#endsyntax#}</pre></th>
@@ -1968,14 +2041,14 @@ const B = error{Two};
a!b
x{}
!x -x -%x ~x &x ?x
-* / % ** *% ||
-+ - ++ +% -%
-<< >>
+* / % ** *% *| ||
++ - ++ +% -% +| -|
+<< >> <<|
& ^ | orelse catch
== != < > <= >=
and
or
-= *= /= %= += -= <<= >>= &= ^= |={#endsyntax#}</pre>
+= *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}</pre>
{#header_close#}
{#header_close#}
{#header_open|Arrays#}
@@ -11839,6 +11912,7 @@ AssignOp
/ PLUSEQUAL
/ MINUSEQUAL
/ LARROW2EQUAL
+ / LARROW2PIPEEQUAL
/ RARROW2EQUAL
/ AMPERSANDEQUAL
/ CARETEQUAL
@@ -11873,6 +11947,8 @@ AdditionOp
/ PLUS2
/ PLUSPERCENT
/ MINUSPERCENT
+ / PLUSPIPE
+ / MINUSPIPE
MultiplyOp
<- PIPE2
@@ -11881,6 +11957,7 @@ MultiplyOp
/ PERCENT
/ ASTERISK2
/ ASTERISKPERCENT
+ / ASTERISKPIPE
PrefixOp
<- EXCLAMATIONMARK
@@ -12044,6 +12121,8 @@ ASTERISK2 <- '**' skip
ASTERISKEQUAL <- '*=' skip
ASTERISKPERCENT <- '*%' ![=] skip
ASTERISKPERCENTEQUAL <- '*%=' skip
+ASTERISKPIPE <- '*|' ![=] skip
+ASTERISKPIPEEQUAL <- '*|=' skip
CARET <- '^' ![=] skip
CARETEQUAL <- '^=' skip
COLON <- ':' skip
@@ -12060,6 +12139,8 @@ EXCLAMATIONMARK <- '!' ![=] skip
EXCLAMATIONMARKEQUAL <- '!=' skip
LARROW <- '<' ![<=] skip
LARROW2 <- '<<' ![=] skip
+LARROW2PIPE <- '<<|' ![=] skip
+LARROW2PIPEEQUAL <- '<<|=' ![=] skip
LARROW2EQUAL <- '<<=' skip
LARROWEQUAL <- '<=' skip
LBRACE <- '{' skip
@@ -12069,6 +12150,8 @@ MINUS <- '-' ![%=>] skip
MINUSEQUAL <- '-=' skip
MINUSPERCENT <- '-%' ![=] skip
MINUSPERCENTEQUAL <- '-%=' skip
+MINUSPIPE <- '-|' ![=] skip
+MINUSPIPEEQUAL <- '-|=' skip
MINUSRARROW <- '->' skip
PERCENT <- '%' ![=] skip
PERCENTEQUAL <- '%=' skip
@@ -12080,6 +12163,8 @@ PLUS2 <- '++' skip
PLUSEQUAL <- '+=' skip
PLUSPERCENT <- '+%' ![=] skip
PLUSPERCENTEQUAL <- '+%=' skip
+PLUSPIPE <- '+|' ![=] skip
+PLUSPIPEEQUAL <- '+|=' skip
LETTERC <- 'c' skip
QUESTIONMARK <- '?' skip
RARROW <- '>' ![>=] skip