internal static DoubleArray ComputeDiscountPrice(DoubleArray price, DoubleArray discount)
        {
            if (price.Length != discount.Length)
            {
                throw new ArgumentException("Arrays need to be the same length");
            }

            int length  = price.Length;
            var builder = new DoubleArray.Builder().Reserve(length);
            ReadOnlySpan <double> prices    = price.Values;
            ReadOnlySpan <double> discounts = discount.Values;

            for (int i = 0; i < length; ++i)
            {
                builder.Append(prices[i] * (1 - discounts[i]));
            }

            return(builder.Build());
        }
Example #2
0
        internal static unsafe DoubleArray ComputeTotal(DoubleArray price, DoubleArray discount, DoubleArray tax)
        {
            if ((price.Length != discount.Length) || (price.Length != tax.Length))
            {
                throw new ArgumentException("Arrays need to be the same length");
            }

            int length = price.Length;
            int vectorizationLength = length - (length % 4);

            DoubleArray.Builder builder = new DoubleArray.Builder().Reserve(length);
            Span <double>       buffer  = stackalloc double[4];

            fixed(double *pExtendedPrice = price.Values)
            fixed(double *pDiscount = discount.Values)
            fixed(double *pTaxes    = discount.Values)
            fixed(double *pBuffer   = buffer)
            {
                Vector256 <double> ones = Vector256.Create(1.0);

                int i = 0;

                for (; i < vectorizationLength; i += 4)
                {
                    Vector256 <double> r = Avx.Multiply(
                        Avx.Multiply(
                            Avx.LoadVector256(pExtendedPrice + i),
                            Avx.Subtract(ones, Avx.LoadVector256(pDiscount + i))),
                        Avx.Add(ones, Avx.LoadVector256(pTaxes + i)));

                    Avx.Store(pBuffer, r);
                    builder.Append(buffer);
                }

                for (; i < length; i += 1)
                {
                    builder.Append(pExtendedPrice[i] * (1 - pDiscount[i]) * (1 + pTaxes[i]));
                }
            }

            return(builder.Build());
        }
Example #3
0
 public void NewBatch()
 {
     _builder = new DoubleArray.Builder();
 }