From 5ec898399f63e75b35197ffa259339a1159c3c93 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 28 Jan 2008 00:36:27 +0000 Subject: [PATCH] make handling of overflow and undefined results much more clear. Patch by Eli Friedman, thanks! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46428 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LangRef.html | 49 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/docs/LangRef.html b/docs/LangRef.html index 5aa5157251e..a2ee339bc0b 100644 --- a/docs/LangRef.html +++ b/docs/LangRef.html @@ -2051,6 +2051,11 @@ Both arguments must have identical types.

Semantics:

The value produced is the integer or floating point sum of the two operands.

+

If an integer sum has unsigned overflow, the result returned is the +mathematical result modulo 2n, where n is the bit width of +the result.

+

Because LLVM integers use a two's complement representation, this +instruction is appropriate for both signed and unsigned integers.

Example:
  <result> = add i32 4, %var          ; yields {i32}:result = 4 + %var
 
@@ -2076,6 +2081,11 @@ Both arguments must have identical types.

Semantics:

The value produced is the integer or floating point difference of the two operands.

+

If an integer difference has unsigned overflow, the result returned is the +mathematical result modulo 2n, where n is the bit width of +the result.

+

Because LLVM integers use a two's complement representation, this +instruction is appropriate for both signed and unsigned integers.

Example:
   <result> = sub i32 4, %var          ; yields {i32}:result = 4 - %var
@@ -2101,9 +2111,15 @@ Both arguments must have identical types.

Semantics:

The value produced is the integer or floating point product of the two operands.

-

Because the operands are the same width, the result of an integer -multiplication is the same whether the operands should be deemed unsigned or -signed.

+

If the result of an integer multiplication has unsigned overflow, +the result returned is the mathematical result modulo +2n, where n is the bit width of the result.

+

Because LLVM integers use a two's complement representation, and the +result is the same width as the operands, this instruction returns the +correct result for both signed and unsigned integers. If a full product +(e.g. i32xi32->i64) is needed, the operands +should be sign-extended or zero-extended as appropriate to the +width of the full product.

Example:
  <result> = mul i32 4, %var          ; yields {i32}:result = 4 * %var
 
@@ -2124,9 +2140,10 @@ operands.

types. This instruction can also take vector versions of the values in which case the elements must be integers.

Semantics:
-

The value produced is the unsigned integer quotient of the two operands. This -instruction always performs an unsigned division operation, regardless of -whether the arguments are unsigned or not.

+

The value produced is the unsigned integer quotient of the two operands.

+

Note that unsigned integer division and signed integer division are distinct +operations; for signed integer division, use 'sdiv'.

+

Division by zero leads to undefined behavior.

Example:
  <result> = udiv i32 4, %var          ; yields {i32}:result = 4 / %var
 
@@ -2147,9 +2164,12 @@ operands.

types. This instruction can also take vector versions of the values in which case the elements must be integers.

Semantics:
-

The value produced is the signed integer quotient of the two operands. This -instruction always performs a signed division operation, regardless of whether -the arguments are signed or not.

+

The value produced is the signed integer quotient of the two operands.

+

Note that signed integer division and unsigned integer division are distinct +operations; for unsigned integer division, use 'udiv'.

+

Division by zero leads to undefined behavior. Overflow also leads to +undefined behavior; this is a rare case, but can occur, for example, +by doing a 32-bit division of -2147483648 by -1.

Example:
  <result> = sdiv i32 4, %var          ; yields {i32}:result = 4 / %var
 
@@ -2194,6 +2214,9 @@ of the values in which case the elements must be integers.

This instruction returns the unsigned integer remainder of a division. This instruction always performs an unsigned division to get the remainder, regardless of whether the arguments are unsigned or not.

+

Note that unsigned integer remainder and signed integer remainder are +distinct operations; for signed integer remainder, use 'srem'.

+

Taking the remainder of a division by zero leads to undefined behavior.

Example:
  <result> = urem i32 4, %var          ; yields {i32}:result = 4 % %var
 
@@ -2225,6 +2248,14 @@ a value. For more information about the difference, see . For a table of how this is implemented in various languages, please see Wikipedia: modulo operation.

+

Note that signed integer remainder and unsigned integer remainder are +distinct operations; for unsigned integer remainder, use 'urem'.

+

Taking the remainder of a division by zero leads to undefined behavior. +Overflow also leads to undefined behavior; this is a rare case, but can occur, +for example, by taking the remainder of a 32-bit division of -2147483648 by -1. +(The remainder doesn't actually overflow, but this rule lets srem be +implemented using instructions that return both the result of the division +and the remainder.)

Example:
  <result> = srem i32 4, %var          ; yields {i32}:result = 4 % %var