Reputation: 3361
I'm trying to use llvm::IRBuilder Create Add/Sub/Mul/Div operations. But there're many apis in https://llvm.org/doxygen/classllvm_1_1IRBuilder.html#a4ef70bab263e38c5e0b8c1bf95a5d814:
Value * CreateAdd (Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false);
Value * CreateNSWAdd (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateNUWAdd (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateSub (Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false);
Value * CreateNSWSub (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateNUWSub (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateMul (Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false);
Value * CreateNSWMul (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateNUWMul (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateUDiv (Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false);
Value * CreateExactUDiv (Value *LHS, Value *RHS, const Twine &Name="");
Value * CreateSDiv (Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false);
Value * CreateExactSDiv (Value *LHS, Value *RHS, const Twine &Name="");
Which one should I use when I want to generate Add/Sub/Mul/Div operations?
Upvotes: 0
Views: 1897
Reputation:
using System;
namespace ConsoleApp1
{
class Program
{
public static int Addition(int value1, int value2)
{
return value1 + value2;
}
public static int Subtraction(int value1, int value2)
{
return value1 - value2;
}
public static int Multiplication(int value1, int value2)
{
return value1 * value2;
}
public static double Division(double value1, double value2)
{
return value1 / value2;
}
static void Main(string[] args)
{
while (true)
{
Console.Write("\nEnter Value 1 : ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Value 2 : ");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please Enter action to be performed :");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
int action = Convert.ToInt32(Console.ReadLine());
string _operation = "";
double result = 0;
switch (action)
{
case 1:
{
result = Addition(a, b);
_operation = "+";
break;
}
case 2:
{
result = Subtraction(a, b);
_operation = "-";
break;
}
case 3:
{
result = Multiplication(a, b);
_operation = "*";
break;
}
case 4:
{
result = Division(a, b);
_operation = "/";
break;
}
}
Console.WriteLine("Result A {0} B = {1}", _operation, result);
char theKey = Console.ReadKey().KeyChar;
if (theKey == 'y' || theKey == 'Y')
{
Console.Clear();
continue;
}
else
{
break;
}
}
}
}
}
Upvotes: -1
Reputation: 1671
NSW (No Signed Wrap)
and NUS(No Unsigned Wrap)
Signed integer overflow occurs when the result of the operation would fall outside the representable range for the signed integer type. For say and Add operation, use CreateAdd
if you want the result of the operation to wrap around the MAX_INT (i.e $(2)^(32)$ - 1 for unsigned i32) for that integer type in case it exceeds that max value.
By wrapping around we mean if its value exceeds MAX_INT(overflow) then it is set to MIN_INT. CreateNSWAdd
will allow wrapping for unsigned integers but generate a poison value (undefined behaviour) for signed integers. CreateNUSAdd
will generate an addition operation allowing wrapping for signed integers but not for their unsigned mates.
Upvotes: 3