Пример #1
0
        public static double randi(double imax)
        {
            var exclusiveMax = PseudoBuiltins.ToInt(imax + 1);

            lock (threadSharedRandom)
                return(threadSharedRandom.Next(1, exclusiveMax));
        }
Пример #2
0
        public static int size(MValue value, double dim)
        {
            Contract.Requires(value != null);
            var shape = value.Shape;

            return(shape.GetDimensionSize(PseudoBuiltins.ToInt(dim) - 1));
        }
Пример #3
0
        internal static DateTime RowToDateTime(MFullArray <double> array, int rowIndex)
        {
            int    rowCount = array.Shape.RowCount;
            double seconds  = array[rowCount * 5 + rowIndex];

            return(new DateTime(
                       PseudoBuiltins.ToInt(array[rowCount * 0 + rowIndex]),
                       PseudoBuiltins.ToInt(array[rowCount * 1 + rowIndex]),
                       PseudoBuiltins.ToInt(array[rowCount * 2 + rowIndex]),
                       PseudoBuiltins.ToInt(array[rowCount * 3 + rowIndex]),
                       PseudoBuiltins.ToInt(array[rowCount * 4 + rowIndex]),
                       (int)seconds, (int)((seconds % 1) * 1000)));
        }
Пример #4
0
        public static MFullArray <double> randi(double imax, double rowCount, double columnCount)
        {
            var exclusiveMax = PseudoBuiltins.ToInt(imax + 1);
            var result       = MFullArray <double> .CreateWithShape(rowCount, columnCount);

            int count = result.Count;

            lock (threadSharedRandom)
            {
                for (int i = 0; i < count; ++i)
                {
                    result.BackingArray[i] = threadSharedRandom.Next(0, exclusiveMax);
                }
            }
            return(result);
        }
Пример #5
0
        public override void VisitRangeFor(RangeFor node)
        {
            // Declare loop labels, preserving parent ones
            var previousContinueTargetLabel = continueTargetLabel;
            var previousBreakTargetLabel    = breakTargetLabel;

            continueTargetLabel = cil.CreateLabel("for_continue");
            breakTargetLabel    = cil.CreateLabel("for_break");
            var conditionLabel = cil.CreateLabel("for_condition");

            var iteratorRepr = new MRepr(node.Iterator.StaticRepr.Type, MStructuralClass.Scalar);

            Contract.Assert(iteratorRepr.Class.IsNumeric && !iteratorRepr.IsComplex);
            Contract.Assert(node.Step == null || IsLiteral(node.Step));

            // Allocate the the temporaries we need
            var currentLocal = temporaryPool.Alloc(iteratorRepr.CliType);

            TemporaryLocalPool.AllocationScope?stepLocal = null, toLocal = null;

            try
            {
                // Setup the iterator variable, "current"
                EmitLoad(node.From);
                EmitConvert(node.From.StaticRepr, iteratorRepr);
                cil.Store(currentLocal.Location);

                // Save the increment to a local variable if it's not a literal or initonly
                if (node.Step != null && !IsLiteral(node.Step) && !node.Step.IsInitOnly)
                {
                    stepLocal = temporaryPool.Alloc(iteratorRepr.CliType);
                    EmitLoad(node.Step);
                    EmitConvert(node.Step.StaticRepr, iteratorRepr);
                    EmitCloneIfNeeded(iteratorRepr);
                    cil.Store(stepLocal.Value.Location);
                }

                // Save the "to" variable to a local variable if it's not a literal or initonly
                if (!IsLiteral(node.To) && !node.To.IsInitOnly)
                {
                    toLocal = temporaryPool.Alloc(iteratorRepr.CliType);
                    EmitLoad(node.To);
                    EmitConvert(node.To.StaticRepr, iteratorRepr);
                    EmitCloneIfNeeded(iteratorRepr);
                    cil.Store(toLocal.Value.Location);
                }

                // Test the loop condition
                {
                    // condition: if (current > to) goto break;
                    cil.MarkLabel(conditionLabel);
                    cil.Load(currentLocal.Location);

                    // IntOK hack: if the "to" is an integral float and our iterator is an integer,
                    // consider "to" as an integer to avoid a cast.
                    bool useIntegralFloatTo = iteratorRepr.Class == MClass.Int32 && IsLiteral(node.To) &&
                                              PseudoBuiltins.IsIntegralFloat(Convert.ToDouble(node.To.ConstantValue));
                    if (!useIntegralFloatTo)
                    {
                        EmitConvert(iteratorRepr, node.To.StaticRepr);                                            // Hack for IntOK
                    }
                    // Load the "to" loop bound variable (or our copy of it)
                    if (toLocal.HasValue)
                    {
                        cil.Load(toLocal.Value.Location);
                    }
                    else
                    {
                        if (useIntegralFloatTo)
                        {
                            cil.LoadInt32((int)Convert.ToDouble(node.To.ConstantValue));
                        }
                        else
                        {
                            EmitLoad(node.To);
                        }
                    }

                    bool forward = node.Step == null || Convert.ToDouble(node.Step.ConstantValue) > 0;
                    cil.Branch(forward ? Comparison.GreaterThan : Comparison.LessThan, breakTargetLabel);
                }

                // Setup the iterator variable (which can be modified inside the loop)
                cil.Load(currentLocal.Location);
                EmitConvert(iteratorRepr, node.Iterator.StaticRepr);
                cil.Store(GetLocation(node.Iterator));

                // body
                EmitStatements(node.Body);

                // Loop increment
                {
                    // continue: current += increment; goto condition;
                    cil.MarkLabel(continueTargetLabel);
                    cil.Load(currentLocal.Location);

                    // Load the "step" loop increment variable, our copy of it or the default value of 1
                    if (stepLocal.HasValue)
                    {
                        cil.Load(stepLocal.Value.Location);
                    }
                    else if (node.Step == null)
                    {
                        if (iteratorRepr.Class.IsInteger)
                        {
                            cil.Instruction(Opcode.Ldc_I4_1);
                            if (iteratorRepr.Class == MClass.Int64)
                            {
                                cil.Instruction(Opcode.Conv_I8);
                            }
                        }
                        else
                        {
                            cil.LoadFloat64(1.0);
                        }
                    }
                    else
                    {
                        EmitLoad(node.Step);
                        EmitConvert(node.Step.StaticRepr, iteratorRepr);
                    }

                    // Increment
                    cil.Instruction(Opcode.Add);
                    cil.Store(currentLocal.Location);
                }

                cil.Branch(conditionLabel);
            }
            finally
            {
                currentLocal.Dispose();
                if (stepLocal.HasValue)
                {
                    stepLocal.Value.Dispose();
                }
                if (toLocal.HasValue)
                {
                    toLocal.Value.Dispose();
                }
            }

            // break:
            cil.MarkLabel(breakTargetLabel);

            // Restore parent loop labels
            continueTargetLabel = previousContinueTargetLabel;
            breakTargetLabel    = previousBreakTargetLabel;
        }
Пример #6
0
 public static MFullArray <bool> @true(double rowCount, double columnCount)
 {
     return(PseudoBuiltins.Expand(true, rowCount, columnCount));
 }
Пример #7
0
 public static MFullArray <double> NaN(double rowCount, double columnCount)
 {
     return(PseudoBuiltins.Expand(double.NaN, rowCount, columnCount));
 }