Beispiel #1
0
        public Number(string number, NumberKind numberKind)
        {
            if (number == null)
            {
                throw new ArgumentNullException(nameof(number));
            }

            this.Kind = numberKind;

            if (numberKind.IsInteger())
            {
                this.State = CheckIntegerString(number, numberKind);
                if (!this.HasErrors)
                {
                    this.Value = LLVM.ConstIntOfString(LLVM.Int128Type(), number, 10);
                }
            }
            else
            {
                this.State = CheckFloatString(number);
                if (!this.HasErrors)
                {
                    this.APFloat = LLVMExt.APFloatZero(GetAPFloatSemantics(this.Kind));
                    this.State  |= (NumberState)LLVMExt.APFloatFromString(this.APFloat, number, RoundingMode);
                }
            }
        }
Beispiel #2
0
        public Number(ulong number, NumberKind numberKind)
        {
            this.Kind = numberKind;

            if (numberKind.IsInteger())
            {
                var overflows = CheckForOverflow(number, numberKind);
                if (overflows == 1)
                {
                    this.State = NumberState.Overflow;
                }
                else if (overflows == -1)
                {
                    this.State = NumberState.Underflow;
                }

                if (!HasErrors)
                {
                    this.Value = LLVM.ConstInt(GetLLVMType(), number, new LLVMBool(0));
                }
            }
            else
            {
                this.APFloat = LLVMExt.APFloatZero(GetAPFloatSemantics(this.Kind));
                this.State  |= (NumberState)LLVMExt.APFloatFromString(this.APFloat, number.ToString(CultureInfo.InvariantCulture), RoundingMode);
            }
        }
Beispiel #3
0
 public NumberSemantics(
     NumberKind DefaultKind             = NumberKind.Int32,
     FloatSemantics LongDoubleSemantics = FloatSemantics.X87DoubleExtended,
     FloatSemantics QuadSemantics       = FloatSemantics.IEEEQuad)
 {
     if (!DefaultKind.IsInteger())
     {
         throw new ArgumentException("DefaultKind must be an integer", nameof(DefaultKind));
     }
     this.DefaultKind         = DefaultKind;
     this.LongDoubleSemantics = LongDoubleSemantics;
     this.QuadSemantics       = QuadSemantics;
 }
Beispiel #4
0
        public Number(double number, NumberKind numberKind)
        {
            this.Kind = numberKind;

            if (numberKind.IsInteger())
            {
                var overflows = CheckForOverflow(number, numberKind);
                if (overflows == 1)
                {
                    this.State = NumberState.Overflow;
                }
                else if (overflows == -1)
                {
                    this.State = NumberState.Underflow;
                }

                if (!HasErrors)
                {
                    this.Value = LLVM.ConstReal(LLVM.DoubleType(), number);
                    if (numberKind.IsSigned())
                    {
                        this.Value = LLVM.ConstFPToSI(this.Value, GetLLVMType());
                    }
                    else
                    {
                        this.Value = LLVM.ConstFPToUI(this.Value, GetLLVMType());
                    }
                }
            }
            else
            {
                int state;
                this.APFloat = LLVMExt.APFloatFromDouble(number, GetAPFloatSemantics(this.Kind), out state);
                this.State  |= (NumberState)state;
            }
        }
Beispiel #5
0
        static NumberKind OperationKind(NumberKind k1, NumberKind k2, bool unsignedWins = false)
        {
            if (k1.IsInteger())
            {
                if (k2.IsInteger())
                {
                    var sameSignedness = (k1.IsSigned() && k2.IsSigned()) || (k1.IsUnsigned() && k2.IsUnsigned());
                    if (sameSignedness)
                    {
                        var minKSize = Semantics.DefaultKind.BitSize(Semantics);
                        var k1Size   = k1.BitSize(Semantics);
                        var k2Size   = k2.BitSize(Semantics);

                        if (k1Size < minKSize && k2Size < minKSize)
                        {
                            if (k1.IsSigned())
                            {
                                return(Semantics.DefaultKind.Signed());
                            }
                            else
                            {
                                return(Semantics.DefaultKind.Unsigned());
                            }
                        }
                        else
                        {
                            var kSize = Math.Max(k1Size, k2Size);
                            if (kSize == k1Size)
                            {
                                return(k1);
                            }
                            else
                            {
                                return(k2);
                            }
                        }
                    }
                    else
                    {
                        if (unsignedWins)
                        {
                            k1 = k1.Unsigned();
                            k2 = k2.Unsigned();
                        }
                        else
                        {
                            k1 = k1.Signed();
                            k2 = k2.Signed();
                        }

                        var minKSize = Semantics.DefaultKind.BitSize(Semantics);
                        var k1Size   = k1.BitSize(Semantics);
                        var k2Size   = k2.BitSize(Semantics);

                        if (k1Size < minKSize && k2Size < minKSize)
                        {
                            if (k1.IsSigned())
                            {
                                return(Semantics.DefaultKind.Signed());
                            }
                            else
                            {
                                return(Semantics.DefaultKind.Unsigned());
                            }
                        }
                        else
                        {
                            var kSize = Math.Max(k1Size, k2Size);
                            if (kSize == k1Size)
                            {
                                return(k1);
                            }
                            else
                            {
                                return(k2);
                            }
                        }
                    }
                }
                else
                {
                    return(k2);
                }
            }
            else
            {
                if (k2.IsInteger())
                {
                    return(k1);
                }
                else
                {
                    var k1Size = k1.BitSize(Semantics);
                    var k2Size = k2.BitSize(Semantics);

                    if (k1Size == k2Size)
                    {
                        if (k1 == k2)
                        {
                            return(k1);
                        }
                        else
                        {
                            return(GetBestFloatKind(k1, k2));
                        }
                    }
                    else
                    {
                        var biggest = Math.Max(k1Size, k2Size);
                        return(biggest == k1Size ? k1 : k2);
                    }
                }
            }
        }