llvm.org GIT mirror llvm / 5ec8983
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 Chris Lattner 11 years ago
1 changed file(s) with 40 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
20502050
Semantics:
20512051

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

20522052 operands.

2053

If an integer sum has unsigned overflow, the result returned is the

2054 mathematical result modulo 2n, where n is the bit width of
2055 the result.

2056

Because LLVM integers use a two's complement representation, this

2057 instruction is appropriate for both signed and unsigned integers.

20532058
Example:
20542059
  <result> = add i32 4, %var          ; yields {i32}:result = 4 + %var

                  
                
20552060
20752080
Semantics:
20762081

The value produced is the integer or floating point difference of

20772082 the two operands.

2083

If an integer difference has unsigned overflow, the result returned is the

2084 mathematical result modulo 2n, where n is the bit width of
2085 the result.

2086

Because LLVM integers use a two's complement representation, this

2087 instruction is appropriate for both signed and unsigned integers.

20782088
Example:
20792089

                  
                
20802090 <result> = sub i32 4, %var ; yields {i32}:result = 4 - %var
21002110
Semantics:
21012111

The value produced is the integer or floating point product of the

21022112 two operands.

2103

Because the operands are the same width, the result of an integer

2104 multiplication is the same whether the operands should be deemed unsigned or
2105 signed.

2113

If the result of an integer multiplication has unsigned overflow,

2114 the result returned is the mathematical result modulo
2115 2n, where n is the bit width of the result.

2116

Because LLVM integers use a two's complement representation, and the

2117 result is the same width as the operands, this instruction returns the
2118 correct result for both signed and unsigned integers. If a full product
2119 (e.g. i32xi32->i64) is needed, the operands
2120 should be sign-extended or zero-extended as appropriate to the
2121 width of the full product.

21062122
Example:
21072123
  <result> = mul i32 4, %var          ; yields {i32}:result = 4 * %var

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

21252141
Semantics:
2126

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

2127 instruction always performs an unsigned division operation, regardless of
2128 whether the arguments are unsigned or not.

2142

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

2143

Note that unsigned integer division and signed integer division are distinct

2144 operations; for signed integer division, use 'sdiv'.

2145

Division by zero leads to undefined behavior.

21292146
Example:
21302147
  <result> = udiv i32 4, %var          ; yields {i32}:result = 4 / %var

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

21482165
Semantics:
2149

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

2150 instruction always performs a signed division operation, regardless of whether
2151 the arguments are signed or not.

2166

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

2167

Note that signed integer division and unsigned integer division are distinct

2168 operations; for unsigned integer division, use 'udiv'.

2169

Division by zero leads to undefined behavior. Overflow also leads to

2170 undefined behavior; this is a rare case, but can occur, for example,
2171 by doing a 32-bit division of -2147483648 by -1.

21522172
Example:
21532173
  <result> = sdiv i32 4, %var          ; yields {i32}:result = 4 / %var

                  
                
21542174
21932213

This instruction returns the unsigned integer remainder of a division.

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

2216

Note that unsigned integer remainder and signed integer remainder are

2217 distinct operations; for signed integer remainder, use 'srem'.

2218

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

21962219
Example:
21972220
  <result> = urem i32 4, %var          ; yields {i32}:result = 4 % %var

                  
                
21982221
22242247 Math Forum. For a table of how this is implemented in various languages,
22252248 please see
22262249 Wikipedia: modulo operation.

2250

Note that signed integer remainder and unsigned integer remainder are

2251 distinct operations; for unsigned integer remainder, use 'urem'.

2252

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

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

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

                  
                
22292260