/// <summary> /// Gets the inner product (scalar product) between two vectors (a'*b). /// </summary> /// /// <param name="a">A vector.</param> /// <param name="b">A vector.</param> /// /// <returns>The inner product of the multiplication of the vectors.</returns> /// public static double Dot(this Sparse<double> a, Sparse<double> b) { double sum = 0; int i = 0, j = 0; while (i < a.Indices.Length && j < b.Indices.Length) { int posx = a.Indices[i]; int posy = b.Indices[j]; if (posx == posy) { sum += a.Values[i] * b.Values[j]; i++; j++; } else if (posx < posy) { i++; } else if (posx > posy) { j++; } } return sum; }
/// <summary> /// Gaussian Kernel function. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns>Dot product in feature (kernel) space.</returns> /// public double Function(Sparse <double> x, Sparse <double> y) { // Optimization in case x and y are // exactly the same object reference. if (x == y) { return(1.0); } double norm = Accord.Math.Distance.SquareEuclidean(x, y); return(Math.Exp(-gamma * norm)); }
/// <summary> /// Adds a sparse vector to a dense vector. /// </summary> public static double[] Add(this Sparse <double> a, double[] b, double[] result) { for (var j = 0; j < b.Length; j++) { result[j] = b[j]; } for (var j = 0; j < a.Indices.Length; j++) { result[a.Indices[j]] += a.Values[j]; } return(result); }
/// <summary> /// Computes the squared distance in feature space /// between two points given in input space. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// /// <returns>Squared distance between <c>x</c> and <c>y</c> in feature (kernel) space.</returns> /// public double Distance(Sparse <double> x, Sparse <double> y) { if (sigma == gamma) { Sigma = 1.0; // TODO: Remove if using VS 2015/C# 6 } if (x == y) { return(0.0); } double norm = Accord.Math.Distance.SquareEuclidean(x, y); return(2 - 2 * Math.Exp(-gamma * norm)); }
/// <summary> /// Computes the squared distance in feature space /// between two points given in input space. /// </summary> /// /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// /// <returns>Squared distance between <c>x</c> and <c>y</c> in feature (kernel) space.</returns> /// public double Distance(Sparse <double> x, Sparse <double> y) { if (x == y) { return(0.0); } double sumx = constant + x.Dot(x); double sumy = constant + y.Dot(y); double sum = constant + x.Dot(y); int d = degree; return(Math.Pow(sumx, d) + Math.Pow(sumy, d) - 2 * Math.Pow(sum, d)); }
public void learn_sparse_test() { Accord.Math.Random.Generator.Seed = 0; #region doc_learn_sparse // Example regression problem. Suppose we are trying // to model the following equation: f(x, y) = 2x + y Sparse <double>[] inputs = // (x, y) { Sparse.FromDense(new double[] { 0, 1 }), // 2*0 + 1 = 1 Sparse.FromDense(new double[] { 4, 3 }), // 2*4 + 3 = 11 Sparse.FromDense(new double[] { 8, -8 }), // 2*8 - 8 = 8 Sparse.FromDense(new double[] { 2, 2 }), // 2*2 + 2 = 6 Sparse.FromDense(new double[] { 6, 1 }), // 2*6 + 1 = 13 Sparse.FromDense(new double[] { 5, 4 }), // 2*5 + 4 = 14 Sparse.FromDense(new double[] { 9, 1 }), // 2*9 + 1 = 19 Sparse.FromDense(new double[] { 1, 6 }), // 2*1 + 6 = 8 }; double[] outputs = // f(x, y) { 1, 11, 8, 6, 13, 14, 19, 8 }; // Create the linear regression coordinate descent teacher var learn = new LinearRegressionCoordinateDescent <Linear, Sparse <double> >() { Complexity = 10000000, Epsilon = 1e-10 }; // Run the learning algorithm var svm = learn.Learn(inputs, outputs); // Compute the answer for one particular example double fxy = svm.Score(inputs[0]); // 1.000 // Check for correct answers double[] answers = svm.Score(inputs); #endregion Assert.AreEqual(1.0, fxy, 1e-5); for (int i = 0; i < outputs.Length; i++) { Assert.AreEqual(outputs[i], answers[i], 1e-2); } }
public void ParseTest() { double[] v; Sparse <double> actual; Sparse <double> expected; string s; v = new double[] { 1, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s); Assert.AreEqual(expected, actual); v = new double[] { 0, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s); Assert.AreEqual(expected, actual); v = new double[] { 1, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s, insertValueAtBeginning: 0); Assert.AreEqual(expected, actual); v = new double[] { 0, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s, insertValueAtBeginning: 0); Assert.AreEqual(expected, actual); v = new double[] { 1, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s, insertValueAtBeginning: 1); expected = Sparse.Parse("1:1 2:1 3:2 4:3 7:6"); Assert.AreEqual(expected, actual); v = new double[] { 0, 2, 3, 0, 0, 6 }; expected = Sparse.FromDense(v); s = expected.ToString(); actual = Sparse.Parse(s, insertValueAtBeginning: 42); expected = Sparse.Parse("1:42 3:2 4:3 7:6"); Assert.AreEqual(expected, actual); }
public void H() { const uint N = 10; var sparse = new Sparse <double>(124); for (uint i = 0; i < N; i++) { sparse.H[i] = -1; } for (uint i = 0; i < N; i++) { Assert.Equal(sparse.H[i], -1); } this.DisposeAndCheckDisposedState(sparse); }
static private void TestSparseGeneration(string Path) { var UniformMat = MatlabReader.Read <double> (Path, "Tri"); var NodeCount = MatlabReader.Read <double> (Path, "NodeCount"); var Count = NodeCount.ToArray(); var Delaunay = new int[UniformMat.RowCount, UniformMat.ColumnCount]; for (int i = 0; i < Delaunay.GetLength(0); i++) { for (int j = 0; j < Delaunay.GetLength(1); j++) { Delaunay [i, j] = (int)UniformMat [i, j] - 1; } } var myArray = new Sparse(Delaunay, (int)Count[0, 0]); }
public void ToStringTest() { double[] v; string actual; Sparse <double> d; v = new double[] { 1, 2, 3, 0, 0, 6 }; d = Sparse.FromDense(v); actual = d.ToString(); Assert.AreEqual("1:1 2:2 3:3 6:6", actual); v = new double[] { 0, 0, 2, 3, 0, 0, 6 }; d = Sparse.FromDense(v); actual = d.ToString(); Assert.AreEqual("3:2 4:3 7:6", actual); }
/// <summary> /// Computes the distance <c>d(x,y)</c> between points /// <paramref name="x"/> and <paramref name="y"/>. /// </summary> /// /// <param name="x">The first point <c>x</c>.</param> /// <param name="y">The second point <c>y</c>.</param> /// /// <returns> /// A double-precision value representing the distance <c>d(x,y)</c> /// between <paramref name="x"/> and <paramref name="y"/> according /// to the distance function implemented by this class. /// </returns> /// public static double Sparse(Sparse <double> x, Sparse <double> y) { double sum = 0; int i = 0, j = 0; while (i < x.Indices.Length && j < y.Indices.Length) { int posx = x.Indices[i]; int posy = y.Indices[j]; if (posx == posy) { double d = x.Values[i] - y.Values[j]; sum += d * d; i++; j++; } else if (posx < posy) { double d = x.Values[i]; sum += d * d; i++; } else if (posx > posy) { double d = y.Values[j]; sum += d * d; j++; } } for (; i < x.Values.Length; i++) { sum += x.Values[i] * x.Values[i]; } for (; j < y.Values.Length; j++) { sum += y.Values[j] * y.Values[j]; } return(sum); }
/// <summary> /// Applies the transformation to an input, producing an associated output. /// </summary> /// <param name="input">The input data to which /// the transformation should be applied.</param> /// <param name="result">The location to where to store the /// result of this transformation.</param> /// <returns>The output generated by applying this /// transformation to the given input.</returns> public Sparse <double> Transform(string[] input, Sparse <double> result) { // Detect all feature words foreach (string word in input) { int j; if (!stringToCode.TryGetValue(word, out j)) { continue; } if (result[j] < MaximumOccurance) { result[j]++; } } return(result); }
public void sparse_zero_vector_test() { // Create a linear-SVM learning method var teacher = new LinearNewtonMethod <Linear, Sparse <double> >() { Tolerance = 1e-10, Complexity = 1e+10, // learn a hard-margin model }; // Now suppose you have some points Sparse <double>[] inputs = Sparse.FromDense(new[] { new double[] { 1, 1, 2 }, new double[] { 0, 1, 6 }, new double[] { 1, 0, 8 }, new double[] { 0, 0, 0 }, }); int[] outputs = { 1, -1, 1, -1 }; // Learn the support vector machine var svm = teacher.Learn(inputs, outputs); // Compute the predicted points bool[] predicted = svm.Decide(inputs); // And the squared error loss using double error = new ZeroOneLoss(outputs).Loss(predicted); Assert.AreEqual(3, svm.NumberOfInputs); Assert.AreEqual(1, svm.NumberOfOutputs); Assert.AreEqual(2, svm.NumberOfClasses); Assert.AreEqual(1, svm.Weights.Length); Assert.AreEqual(1, svm.SupportVectors.Length); Assert.AreEqual(1.0, svm.Weights[0], 1e-6); Assert.AreEqual(2.0056922148257597, svm.SupportVectors[0][0], 1e-6); Assert.AreEqual(-0.0085361347231909836, svm.SupportVectors[0][1], 1e-6); Assert.AreEqual(0.0014225721169379331, svm.SupportVectors[0][2], 1e-6); Assert.AreEqual(0.0, error); }
/// <summary> /// Elementwise addition of a and b, storing in result. /// </summary> /// /// <param name="a">The first vector to add.</param> /// <param name="b">The second vector to add.</param> /// <param name="result">An array to store the result.</param> /// <returns>The same vector passed as result.</returns> /// public double[] Add(Sparse <double> a, double[] b, double[] result) { int i = 0; for (int j = 0; j < a.Indices.Length; j++) { if (a.Indices[j] == i) { result[i] += b[i] + a.Values[j]; } else { result[i] += b[i]; } i++; } return(result); }
/// <summary> /// Saves this model to disk using LibSVM's model format. /// </summary> /// /// <param name="stream">The stream where the file should be written.</param> /// public void Save(Stream stream) { StreamWriter writer = new StreamWriter(stream); writer.WriteLine("solver_type " + Solver.GetDescription().ToUpperInvariant()); writer.WriteLine("nr_class " + NumberOfClasses); writer.Write("label"); for (int i = 0; i < Labels.Length; i++) { writer.Write(" " + Labels[i]); } writer.WriteLine(); writer.WriteLine("nr_feature " + NumberOfInputs); writer.WriteLine("bias " + Bias.ToString("G17", System.Globalization.CultureInfo.InvariantCulture)); if (this.Vectors == null) { writer.WriteLine("w"); for (int i = 0; i < Weights.Length; i++) { writer.WriteLine(Weights[i].ToString("G17", System.Globalization.CultureInfo.InvariantCulture) + " "); } } else { writer.WriteLine("SV"); for (int i = 0; i < Vectors.Length; i++) { string alpha = Weights[i].ToString("G17", System.Globalization.CultureInfo.InvariantCulture); string values = Sparse.FromDense(Vectors[i]).ToString(); writer.WriteLine(alpha + " " + values); } } writer.Flush(); }
public void J() { const uint N = 10; var sparse = new Sparse <double>(124); for (uint i = 0; i < N; i++) { for (uint j = 0; j < N; j++) { sparse.J[i, j] = (i == j) ? 0 : -1; } } for (uint i = 0; i < N; i++) { for (uint j = 0; j < N; j++) { Assert.Equal(sparse.J[i, j], (i == j) ? 0 : -1); } } this.DisposeAndCheckDisposedState(sparse); }
public void learn_sparse_kernel() { #region doc_xor_sparse // As an example, we will try to learn a decision machine // that can replicate the "exclusive-or" logical function: Sparse <double>[] inputs = { Sparse.FromDense(new double[] { 0, 0 }), // the XOR function takes two booleans Sparse.FromDense(new double[] { 0, 1 }), // and computes their exclusive or: the Sparse.FromDense(new double[] { 1, 0 }), // output is true only if the two booleans Sparse.FromDense(new double[] { 1, 1 }) // are different }; int[] xor = // this is the output of the xor function { 0, // 0 xor 0 = 0 (inputs are equal) 1, // 0 xor 1 = 1 (inputs are different) 1, // 1 xor 0 = 1 (inputs are different) 0, // 1 xor 1 = 0 (inputs are equal) }; // Now, we can create the sequential minimal optimization teacher var learn = new SequentialMinimalOptimization <Gaussian, Sparse <double> >() { UseComplexityHeuristic = true, UseKernelEstimation = true }; // And then we can obtain a trained SVM by calling its Learn method var svm = learn.Learn(inputs, xor); // Finally, we can obtain the decisions predicted by the machine: bool[] prediction = svm.Decide(inputs); #endregion Assert.AreEqual(prediction, Classes.Decide(xor)); }
public static void SparseTest2 <Ta, Tb>(Ta sparseKernel, Tb denseKernel) where Ta : IDistance, IKernel, IKernel <Sparse <double> >, IDistance <Sparse <double> > where Tb : IDistance, IKernel { Sparse <double>[] sparse = { Sparse.Parse("109:1 389:1 429:1 488:1 538:1 566:1 598:2 659:1 741:1 1240:1 1302:1 1568:1 1962:1 2122:1 2362:1 2433:1 2474:2 2539:2 2638:1 2812:1 2871:1 2930:1 2960:1 3158:1 4468:1 5023:1 5520:1 6894:1 7076:1 7369:1 9062:2 9086:1 9422:1 10004:1 11187:2 13191:1 13384:1 14131:1 14196:1 14219:1 14608:1 15472:1 16018:2 17370:1 18603:1 18604:2 18605:2 18606:2"), Sparse.Parse("77:9 137:1 248:1 271:1 357:1 377:3 400:1 412:1 678:1 824:1 880:16 955:1 960:1 970:3 971:2 972:1 1007:2 1035:2 1166:1 1304:16 1354:1 1628:1 1686:2 1721:1 1877:1 2025:1 2219:1 2497:2 2539:4 2550:1 2874:1 2930:2 3378:1 3411:1 3572:17 3626:2 3688:1 3818:1 4039:1 4479:1 4526:2 4748:1 4822:1 4966:1 5479:1 5562:1 5583:2 5844:2 5848:1 6092:1 6096:1 6149:1 6268:1 6819:1 6894:18 7822:1 8139:1 8626:10 9824:17 10461:1 10609:2 10851:1 11463:1 11874:1 11875:2 12397:16 12412:1 13381:1 13384:1 13590:12 13755:2 14074:17 14166:1 14184:2 14517:1 14904:2 15400:1 15531:1 15579:17 15580:16 15936:16 16488:1 16579:1 16642:29 16793:2 17083:2 17458:1 20543:1 20544:2 21252:1 21358:2 22296:1 22479:3 23590:1 25024:17 25809:1 26235:1 26236:2 26237:1 26238:1 26239:1 26240:1 26241:2"), }; double[][] dense = { sparse[0].ToDense(30000), sparse[1].ToDense(30000), }; for (int i = 0; i < sparse.Length; i++) { for (int j = 0; j < sparse.Length; j++) { double expected = denseKernel.Function(dense[i], dense[j]); double actual = sparseKernel.Function(sparse[i], sparse[j]); Assert.AreEqual(expected, actual); } } for (int i = 0; i < sparse.Length; i++) { for (int j = 0; j < sparse.Length; j++) { double expected = denseKernel.Distance(dense[i], dense[j]); double actual = sparseKernel.Distance(sparse[i], sparse[j]); Assert.AreEqual(expected, actual); } } }
public void learn_linear_sparse() { #region doc_xor_sparse // As an example, we will try to learn a linear machine that can // replicate the "exclusive-or" logical function. However, since we // will be using a linear SVM, we will not be able to solve this // problem perfectly as the XOR is a non-linear classification problem: Sparse <double>[] inputs = { Sparse.FromDense(new double[] { 0, 0 }), // the XOR function takes two booleans Sparse.FromDense(new double[] { 0, 1 }), // and computes their exclusive or: the Sparse.FromDense(new double[] { 1, 0 }), // output is true only if the two booleans Sparse.FromDense(new double[] { 1, 1 }) // are different }; int[] xor = // this is the output of the xor function { 0, // 0 xor 0 = 0 (inputs are equal) 1, // 0 xor 1 = 1 (inputs are different) 1, // 1 xor 0 = 1 (inputs are different) 0, // 1 xor 1 = 0 (inputs are equal) }; // Now, we can create the sequential minimal optimization teacher var learn = new LinearNewtonMethod <Linear, Sparse <double> >() { UseComplexityHeuristic = true, UseKernelEstimation = false }; // And then we can obtain a trained SVM by calling its Learn method var svm = learn.Learn(inputs, xor); // Finally, we can obtain the decisions predicted by the machine: bool[] prediction = svm.Decide(inputs); #endregion Assert.AreEqual(prediction[0], false); Assert.AreEqual(prediction[1], false); Assert.AreEqual(prediction[2], false); Assert.AreEqual(prediction[3], false); int[] or = // this is the output of the xor function { 0, // 0 or 0 = 0 (inputs are equal) 1, // 0 or 1 = 1 (inputs are different) 1, // 1 or 0 = 1 (inputs are different) 1, // 1 or 1 = 1 (inputs are equal) }; learn = new LinearNewtonMethod <Linear, Sparse <double> >() { Complexity = 1e+8, UseKernelEstimation = false }; svm = learn.Learn(inputs, or); prediction = svm.Decide(inputs); Assert.AreEqual(0, inputs[0].Indices.Length); Assert.AreEqual(1, inputs[1].Indices.Length); Assert.AreEqual(1, inputs[2].Indices.Length); Assert.AreEqual(2, inputs[3].Indices.Length); Assert.AreEqual(prediction[0], false); Assert.AreEqual(prediction[1], true); Assert.AreEqual(prediction[2], true); Assert.AreEqual(prediction[3], true); }
public Decoder[] CreateRootDecoders() { var loads = new Decoder[] // 0b00000 { Instr(Mnemonic.lb, d, r1, Ls), Instr(Mnemonic.lh, d, r1, Ls), Instr(Mnemonic.lw, d, r1, Ls), Instr(Mnemonic.ld, d, r1, Ls), // 64I Instr(Mnemonic.lbu, d, r1, Ls), Instr(Mnemonic.lhu, d, r1, Ls), Instr(Mnemonic.lwu, d, r1, Ls), // 64I Nyi(""), }; var fploads = new Decoder[8] // 0b00001 { invalid, invalid, FpInstr32(Mnemonic.flw, Fd, Mem(PrimitiveType.Real32, 15, (20, 12))), FpInstr64(Mnemonic.fld, Fd, Mem(PrimitiveType.Real64, 15, (20, 12))), FpInstr128(Mnemonic.flq, Fd, Mem(PrimitiveType.Real64, 15, (20, 12))), invalid, invalid, invalid, }; var stores = new Decoder[] // 0b01000 { Instr(Mnemonic.sb, r2, r1, Ss), Instr(Mnemonic.sh, r2, r1, Ss), Instr(Mnemonic.sw, r2, r1, Ss), Instr(Mnemonic.sd, r2, r1, Ss), // I64 invalid, invalid, invalid, invalid, }; var fpstores = new Decoder[8] // 0b01001 { invalid, invalid, FpInstr32(Mnemonic.fsw, F2, Memc(PrimitiveType.Real32, 15, (25, 7), (7, 5))), FpInstr64(Mnemonic.fsd, F2, Memc(PrimitiveType.Real64, 15, (25, 7), (7, 5))), invalid, invalid, invalid, invalid, }; var opimm = new Decoder[] // 0b00100 { Instr(Mnemonic.addi, d, r1, i), new ShiftDecoder( Instr(Mnemonic.slli, d, r1, Z), invalid), Instr(Mnemonic.slti, d, r1, i), Instr(Mnemonic.sltiu, d, r1, i), Instr(Mnemonic.xori, d, r1, i), new ShiftDecoder( Instr(Mnemonic.srli, d, r1, Z), Instr(Mnemonic.srai, d, r1, Z)), Instr(Mnemonic.ori, d, r1, i), Instr(Mnemonic.andi, d, r1, i), }; var opimm32 = new Decoder[] // 0b00110 { Instr(Mnemonic.addiw, Rd, R1, I20s), Instr(Mnemonic.slliw, Rd, R1, Imm(20, 5)), Nyi(""), Nyi(""), Nyi(""), new ShiftDecoder( Instr(Mnemonic.srliw, d, r1, Z), Instr(Mnemonic.sraiw, d, r1, Z)), Nyi(""), Nyi(""), }; var op = Mask(30, 1, "op", // 0b01100 Mask(12, 3, "alu", Instr(Mnemonic.add, Rd, R1, R2), Instr(Mnemonic.sll, Rd, R1, R2), Instr(Mnemonic.slt, Rd, R1, R2), Instr(Mnemonic.sltu, Rd, R1, R2), Instr(Mnemonic.xor, Rd, R1, R2), Instr(Mnemonic.srl, Rd, R1, R2), Instr(Mnemonic.or, Rd, R1, R2), Instr(Mnemonic.and, Rd, R1, R2)), Mask(12, 3, "alu2", Instr(Mnemonic.sub, Rd, R1, R2), Nyi("op - 20 - 0b001"), Nyi("op - 20 - 0b010"), Nyi("op - 20 - 0b011"), Nyi("op - 20 - 0b100"), Instr(Mnemonic.sra, Rd, R1, R2), Nyi("op - 20 - 0b110"), Nyi("op - 20 - 0b111"))); var op32 = Mask(12, 3, " op-32", // 0b01110 Sparse(25, 7, " 000", Nyi(""), (0x00, Instr(Mnemonic.addw, d, r1, r2)), (0x01, Instr(Mnemonic.mulw, d, r1, r2)), (0x20, Instr(Mnemonic.subw, d, r1, r2))), Sparse(25, 7, " 000", Nyi(""), (0x00, Instr(Mnemonic.sllw, d, r1, r2))), Nyi(""), Nyi(""), Sparse(25, 7, " 100", Nyi(""), (0x01, Instr(Mnemonic.divw, d, r1, r2))), Sparse(25, 7, " 101", Nyi(""), (0x00, Instr(Mnemonic.srlw, d, r1, r2)), (0x01, Instr(Mnemonic.divuw, d, r1, r2)), (0x20, Instr(Mnemonic.sraw, d, r1, r2))), Sparse(25, 7, " 110", Nyi(""), (0x01, Instr(Mnemonic.remw, d, r1, r2))), Sparse(25, 7, " 111", Nyi(""), (0x01, Instr(Mnemonic.remuw, d, r1, r2)))); var opfp = new (uint, Decoder)[] // 0b10100
public void linear_regression_sparse_test() { #region doc_linreg_sparse // Declare some training data. This is exactly the same // data used in the MultipleLinearRegression documentation page // We will try to model a plane as an equation in the form // "ax + by + c = z". We have two input variables (x and y) // and we will be trying to find two parameters a and b and // an intercept term c. // Create a linear-SVM learning method var teacher = new LinearNewtonMethod <Linear, Sparse <double> >() { Tolerance = 1e-10, Complexity = 1e+10, // learn a hard-margin model }; // Now suppose you have some points Sparse <double>[] inputs = Sparse.FromDense(new[] { new double[] { 1, 1 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 0, 0 }, }); // located in the same Z (z = 1) double[] outputs = { 1, 1, 1, 1 }; // Learn the support vector machine var svm = teacher.Learn(inputs, outputs); // Convert the svm to logistic regression var regression = (MultipleLinearRegression)svm; // As result, we will be given the following: double a = regression.Weights[0]; // a = 0 double b = regression.Weights[1]; // b = 0 double c = regression.Intercept; // c = 1 // This is the plane described by the equation // ax + by + c = z => 0x + 0y + 1 = z => 1 = z. // We can compute the predicted points using double[] predicted = regression.Transform(inputs.ToDense()); // And the squared error loss using double error = new SquareLoss(outputs).Loss(predicted); #endregion Assert.AreEqual(2, regression.NumberOfInputs); Assert.AreEqual(1, regression.NumberOfOutputs); Assert.AreEqual(0.0, a, 1e-6); Assert.AreEqual(0.0, b, 1e-6); Assert.AreEqual(1.0, c, 1e-6); Assert.AreEqual(0.0, error, 1e-6); double[] expected = regression.Compute(inputs.ToDense()); double[] actual = regression.Transform(inputs.ToDense()); Assert.IsTrue(expected.IsEqual(actual, 1e-10)); double r = regression.CoefficientOfDetermination(inputs.ToDense(), outputs); Assert.AreEqual(1.0, r); }
Sparse <double> ITransform <string[], Sparse <double> > .Transform(string[] input) { return(Sparse.FromDense(Transform(input))); }
private Sparse <double> Transform(string[] x, out Sparse <double> sparse, double[] work) { IDictionary <string, int> codebook = bow.StringToCode; for (int j = 0; j < x.Length; j++) { int k; if (!codebook.TryGetValue(x[j], out k)) { continue; } work[k]++; } sparse = Sparse.FromDense(work); switch (tf) { case TermFrequency.Binary: for (int j = 0; j < sparse.Values.Length; j++) { sparse.Values[j] = sparse.Values[j] = 1; } break; case TermFrequency.Default: break; case TermFrequency.Log: for (int j = 0; j < sparse.Values.Length; j++) { sparse.Values[j] = 1 + Math.Log(sparse.Values[j]); } break; case TermFrequency.DoubleNormalization: double max = sparse.Values.Max(); for (int j = 0; j < sparse.Values.Length; j++) { sparse.Values[j] = 0.5 + 0.5 * (sparse.Values[j] / max); } break; default: throw new InvalidOperationException("Unknown TermFrequency: {0}".Format(tf)); } // Divide by the inverse document frequency for (int j = 0; j < sparse.Values.Length; j++) { double a = sparse.Values[j]; int k = sparse.Indices[j]; double v = a * inverseDocumentFrequency[k]; #if DEBUG if (Double.IsNaN(v) || Double.IsInfinity(v)) { throw new Exception(); } #endif sparse.Values[j] = v; } return(sparse); }
static MipsDisassembler() { var invalid = new InstrDecoder(Opcode.illegal); var cop1_s = Mask(0, 6, "FPU (single)", new InstrDecoder(Opcode.add_s, F4, F3, F2), new InstrDecoder(Opcode.sub_s, F4, F3, F2), new InstrDecoder(Opcode.mul_s, F4, F3, F2), new InstrDecoder(Opcode.div_s, F4, F3, F2), invalid, invalid, new InstrDecoder(Opcode.mov_s, F4, F3), new InstrDecoder(Opcode.neg_s, F4, F3), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, new InstrDecoder(Opcode.c_eq_s, c8, F3, F2), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, new InstrDecoder(Opcode.c_lt_s, c8, F3, F2), invalid, new InstrDecoder(Opcode.c_le_s, c8, F3, F2), invalid); var cop1_d = Mask(0, 6, "FPU (double)", // fn 00 new InstrDecoder(Opcode.add_d, F4, F3, F2), new InstrDecoder(Opcode.sub_d, F4, F3, F2), new InstrDecoder(Opcode.mul_d, F4, F3, F2), new InstrDecoder(Opcode.div_d, F4, F3, F2), invalid, invalid, new InstrDecoder(Opcode.mov_d, F4, F3), new InstrDecoder(Opcode.neg_d, F4, F3), invalid, new InstrDecoder(Opcode.trunc_l_d, F4, F3), invalid, invalid, invalid, invalid, invalid, invalid, // fn 10 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 20 new InstrDecoder(Opcode.cvt_s_d, F4, F3), invalid, invalid, invalid, new InstrDecoder(Opcode.cvt_w_d, F4, F3), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 30 invalid, invalid, new InstrDecoder(Opcode.c_eq_d, c8, F3, F2), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, new InstrDecoder(Opcode.c_lt_d, c8, F3, F2), invalid, new InstrDecoder(Opcode.c_le_d, c8, F3, F2), invalid); var cop1_w = Mask(0, 6, "FPU (word)", // fn 00 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 10 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 20 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 30 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, new InstrDecoder(Opcode.c_lt_d, c8, F3, F2), invalid, invalid, invalid); var cop1_l = Mask(0, 6, "FPU (dword)", // fn 00 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 10 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 20 new InstrDecoder(Opcode.cvt_s_l, F4, F3), new InstrDecoder(Opcode.cvt_d_l, F4, F3), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // fn 30 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid); var cop0_C0_decoder = Sparse(0, 6, "COP0 C0", invalid, (0x01, new InstrDecoder(Opcode.tlbr)), (0x02, new InstrDecoder(Opcode.tlbwi)), (0x06, new InstrDecoder(Opcode.tlbwr)), (0x08, new InstrDecoder(Opcode.tlbp)), (0x18, new InstrDecoder(Opcode.eret)), (0x20, new InstrDecoder(Opcode.wait))); var cop1 = Mask(21, 5, "COP1", new InstrDecoder(Opcode.mfc1, R2, F3), new A64Decoder(Opcode.dmfc1, R2, F3), new InstrDecoder(Opcode.cfc1, R2, f3), invalid, new InstrDecoder(Opcode.mtc1, R2, F3), new A64Decoder(Opcode.dmtc1, R2, F3), new InstrDecoder(Opcode.ctc1, R2, f3), invalid, Mask(16, 1, new InstrDecoder(InstrClass.ConditionalTransfer | InstrClass.Delay, Opcode.bc1f, c18, j), new InstrDecoder(InstrClass.ConditionalTransfer | InstrClass.Delay, Opcode.bc1t, c18, j)), invalid, invalid, invalid, invalid, invalid, invalid, invalid, cop1_s, cop1_d, invalid, invalid, cop1_w, cop1_l, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid); var special2 = Sparse(0, 6, "Special2", invalid, (0x2, new Version6Decoder( new InstrDecoder(Opcode.mul, R3, R1, R2), invalid) ) ); var condDecoders = Mask(16, 5, "CondDecoders", new InstrDecoder(DCT, Opcode.bltz, R1, j), new InstrDecoder(DCT, Opcode.bgez, R1, j), new InstrDecoder(DCT, Opcode.bltzl, R1, j), new InstrDecoder(DCT, Opcode.bgezl, R1, j), invalid, invalid, invalid, invalid, new InstrDecoder(CTD, Opcode.tgei, R1, I), new InstrDecoder(CTD, Opcode.tgeiu, R1, I), new InstrDecoder(CTD, Opcode.tlti, R1, I), new InstrDecoder(CTD, Opcode.tltiu, R1, I), new InstrDecoder(CTD, Opcode.teqi, R1, I), invalid, new InstrDecoder(CTD, Opcode.tnei, R1, I), invalid, new InstrDecoder(CTD, Opcode.bltzal, R1, j), new InstrDecoder(CTD, Opcode.bgezal, R1, j), new InstrDecoder(CTD, Opcode.bltzall, R1, j), new InstrDecoder(CTD, Opcode.bgezall, R1, j), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid); var bshfl = Mask(6, 5, invalid, invalid, new InstrDecoder(Opcode.wsbh, x("")), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // 10 new InstrDecoder(Opcode.seb, R3, R2), invalid, invalid, invalid, invalid, invalid, invalid, invalid, new InstrDecoder(Opcode.seh, R3, R2), invalid, invalid, invalid, invalid, invalid, invalid, invalid); var special3 = Mask(0, 6, "Special3", // 00 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // 10 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // 20 bshfl, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // 30 invalid, invalid, invalid, invalid, invalid, invalid, new Version6Decoder( invalid, new InstrDecoder(Opcode.ll, R2, ew)), new Version6Decoder( invalid, new A64Decoder(Opcode.lld, R2, el)), invalid, invalid, invalid, new InstrDecoder(Opcode.rdhwr, R2, H), invalid, invalid, invalid, invalid); var special = Mask(0, 6, "Special", Select((6, 5), n => n == 0, new InstrDecoder(InstrClass.Linear | InstrClass.Padding, Opcode.nop), new InstrDecoder(Opcode.sll, R3, R2, s)), Mask(16, 1, new InstrDecoder(Opcode.movf, R2, R1, C18), new InstrDecoder(Opcode.movt, R2, R1, C18)), new InstrDecoder(Opcode.srl, R3, R2, s), new InstrDecoder(Opcode.sra, R3, R2, s), new InstrDecoder(Opcode.sllv, R3, R2, R1), new InstrDecoder(Opcode.illegal), new InstrDecoder(Opcode.srlv, R3, R2, R1), new InstrDecoder(Opcode.srav, R3, R2, R1), new InstrDecoder(TD, Opcode.jr, R1), new InstrDecoder(CTD, Opcode.jalr, R3, R1), new InstrDecoder(Opcode.movz, R3, R1, R2), new InstrDecoder(Opcode.movn, R3, R1, R2), new InstrDecoder(Opcode.syscall, B), new InstrDecoder(Opcode.@break, B), new InstrDecoder(Opcode.illegal), new InstrDecoder(Opcode.sync, s), // 10 new InstrDecoder(Opcode.mfhi, R3), new InstrDecoder(Opcode.mthi, R1), new InstrDecoder(Opcode.mflo, R3), new InstrDecoder(Opcode.mtlo, R1), new A64Decoder(Opcode.dsllv, R3, R2, R1), new InstrDecoder(Opcode.illegal), new A64Decoder(Opcode.dsrlv, R3, R2, R1), new A64Decoder(Opcode.dsrav, R3, R2, R1), new InstrDecoder(Opcode.mult, R1, R2), new InstrDecoder(Opcode.multu, R1, R2), new InstrDecoder(Opcode.div, R1, R2), new InstrDecoder(Opcode.divu, R1, R2), new A64Decoder(Opcode.dmult, R1, R2), new A64Decoder(Opcode.dmultu, R1, R2), new A64Decoder(Opcode.ddiv, R1, R2), new A64Decoder(Opcode.ddivu, R1, R2), // 20 new InstrDecoder(Opcode.add, R3, R1, R2), new InstrDecoder(Opcode.addu, R3, R1, R2), new InstrDecoder(Opcode.sub, R3, R1, R2), new InstrDecoder(Opcode.subu, R3, R1, R2), new InstrDecoder(Opcode.and, R3, R1, R2), new InstrDecoder(Opcode.or, R3, R1, R2), new InstrDecoder(Opcode.xor, R3, R1, R2), new InstrDecoder(Opcode.nor, R3, R1, R2), new InstrDecoder(Opcode.illegal), new InstrDecoder(Opcode.illegal), new InstrDecoder(Opcode.slt, R3, R1, R2), new InstrDecoder(Opcode.sltu, R3, R1, R2), new A64Decoder(Opcode.dadd, R3, R1, R2), new A64Decoder(Opcode.daddu, R3, R1, R2), new A64Decoder(Opcode.dsub, R3, R1, R2), new A64Decoder(Opcode.dsubu, R3, R1, R2), // 30 new InstrDecoder(CTD, Opcode.tge, R1, R2, T), new InstrDecoder(CTD, Opcode.tgeu, R1, R2, T), new InstrDecoder(CTD, Opcode.tlt, R1, R2, T), new InstrDecoder(CTD, Opcode.tltu, R1, R2, T), new InstrDecoder(CTD, Opcode.teq, R1, R2, T), new InstrDecoder(Opcode.illegal), new InstrDecoder(CTD, Opcode.tne, R1, R2, T), new InstrDecoder(Opcode.illegal), new A64Decoder(Opcode.dsll, R3, R2, s), new InstrDecoder(Opcode.illegal), new A64Decoder(Opcode.dsrl, R3, R2, s), new A64Decoder(Opcode.dsra, R3, R2, s), new A64Decoder(Opcode.dsll32, R3, R2, s), new InstrDecoder(Opcode.illegal), new A64Decoder(Opcode.dsrl32, R3, R2, s), new A64Decoder(Opcode.dsra32, R3, R2, s)); rootDecoder = Mask(26, 6, special, condDecoders, new InstrDecoder(TD, Opcode.j, J), new InstrDecoder(CTD, Opcode.jal, J), new InstrDecoder(DCT, Opcode.beq, R1, R2, j), new InstrDecoder(DCT, Opcode.bne, R1, R2, j), new InstrDecoder(DCT, Opcode.blez, R1, j), new InstrDecoder(DCT, Opcode.bgtz, R1, j), new InstrDecoder(Opcode.addi, R2, R1, I), new InstrDecoder(Opcode.addiu, R2, R1, I), new InstrDecoder(Opcode.slti, R2, R1, I), new InstrDecoder(Opcode.sltiu, R2, R1, I), new InstrDecoder(Opcode.andi, R2, R1, U), new InstrDecoder(Opcode.ori, R2, R1, U), new InstrDecoder(Opcode.xori, R2, R1, U), new InstrDecoder(Opcode.lui, R2, i), // 10 Mask(21, 5, "Coprocessor", new InstrDecoder(Opcode.mfc0, R2, R3), new InstrDecoder(Opcode.dmfc0, R2, R3), invalid, invalid, new InstrDecoder(Opcode.mtc0, R2, R3), new InstrDecoder(Opcode.dmtc0, R2, R3), invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder, cop0_C0_decoder), // 11: COP1 encodings cop1, Mask(21, 5, "COP2", // 12: COP2 invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid), null, // COP1X new InstrDecoder(DCT, Opcode.beql, R1, R2, j), new InstrDecoder(DCT, Opcode.bnel, R1, R2, j), new InstrDecoder(DCT, Opcode.blezl, R1, j), new InstrDecoder(DCT, Opcode.bgtzl, R1, j), new A64Decoder(Opcode.daddi, R2, R1, I), new A64Decoder(Opcode.daddiu, R2, R1, I), new A64Decoder(Opcode.ldl, R2, El), new A64Decoder(Opcode.ldr, R2, El), special2, null, null, special3, // 20 new InstrDecoder(Opcode.lb, R2, EB), new InstrDecoder(Opcode.lh, R2, EH), new InstrDecoder(Opcode.lwl, R2, Ew), new InstrDecoder(Opcode.lw, R2, Ew), new InstrDecoder(Opcode.lbu, R2, Eb), new InstrDecoder(Opcode.lhu, R2, Eh), new InstrDecoder(Opcode.lwr, R2, Ew), new A64Decoder(Opcode.lwu, R2, Ew), new InstrDecoder(Opcode.sb, R2, Eb), new InstrDecoder(Opcode.sh, R2, Eh), new InstrDecoder(Opcode.swl, R2, Ew), new InstrDecoder(Opcode.sw, R2, Ew), new InstrDecoder(Opcode.sdl, R2, Ew), new InstrDecoder(Opcode.sdr, R2, Ew), new InstrDecoder(Opcode.swr, R2, Ew), null, // 30 new Version6Decoder( new InstrDecoder(Opcode.ll, R2, Ew), invalid), new InstrDecoder(Opcode.lwc1, F2, Ew), null, new InstrDecoder(Opcode.pref, R2, Ew), new Version6Decoder( new A64Decoder(Opcode.lld, R2, El), invalid), new InstrDecoder(Opcode.ldc1, F2, El), null, new A64Decoder(Opcode.ld, R2, El), new Version6Decoder( new InstrDecoder(Opcode.sc, R2, Ew), invalid), new InstrDecoder(Opcode.swc1, F2, Ew), null, null, new Version6Decoder( new A64Decoder(Opcode.scd, R2, El), invalid), new A64Decoder(Opcode.sdc1, F2, El), null, new A64Decoder(Opcode.sd, R2, El)); }
static WE32100Disassembler() { var invalid = Instr(Mnemonic.Invalid, InstrClass.Invalid); rootDecoder = Mask(0, 8, "WE32100", new Decoder[256] { // 0x00 Instr(Mnemonic.Invalid, InstrClass.Invalid | InstrClass.Zero | InstrClass.Padding), invalid, Instr(Mnemonic.spoprd, X("")), // 0x02 Coprocessor operation read double Instr(Mnemonic.spopd2, X("")), // 0x03 Coprocessor operation double, 2 - address Instr(Mnemonic.movaw, X("")), // 0x04 Move address(word) Instr(Mnemonic.spoprt, X("")), // 0x06 Coprocessor operation read triple Instr(Mnemonic.spopt2, X("")), // 0x07 Coprocessor operation triple, 2 - address Instr(Mnemonic.ret, X("")), // 0x08 Return from procedure invalid, invalid, invalid, invalid, invalid, invalid, invalid, invalid, // 0x10 Instr(Mnemonic.save, X("")), // 0x 10 Save registers invalid, invalid, Instr(Mnemonic.spopwd, X("")), // 0xl3 Coprocessor operation write double Instr(Mnemonic.ex, X("")), // TOP 0xl4 Extended opcode invalid, invalid, Instr(Mnemonic.spopwt, X("")), // 0xl7 Coprocessor operation write triple Instr(Mnemonic.restore, X("")), // 0xl8 Restore registers invalid, invalid, invalid, Instr(Mnemonic.swapwi, X("")), // 0x1C Swap word interlocked invalid, Instr(Mnemonic.swaphi, X("")), // 0x1E Swap halfword interlocked Instr(Mnemonic.swapbi, X("")), // 0x1F Swap byte interlocked // 0x20 Instr(Mnemonic.popw, X("")), // 0x20 Pop word invalid, Instr(Mnemonic.spoprs, X("")), // 0x22 Coprocessor operation read single Instr(Mnemonic.spops2, X("")), // 0x23 Coprocessor operation single, 2 - address Instr(Mnemonic.jmp, X("")), // 0x24 Jump invalid, invalid, invalid, Instr(Mnemonic.tstw, X("")), // 0x28 Test word invalid, Instr(Mnemonic.tsth, X("")), // 0x2A Test halfword Instr(Mnemonic.tstb, X("")), // 0x2B Test byte Instr(Mnemonic.call, X("")), // 0x2C Call procedure invalid, Instr(Mnemonic.bpt, X("")), // 0x2E Breakpoint trap invalid, // 0x30 new ExtDecoder(Sparse(0, 8, " 30", invalid, (0x09, Instr(Mnemonic.mverno, X(""))), // 0x3009 Move version number (0x19, Instr(Mnemonic.movblw, X(""))), // 0x3019 Move block of words (0x1F, Instr(Mnemonic.strend, X(""))), // 0x301F String end (0x35, Instr(Mnemonic.strcpy, X(""))))), // 0x3035 String copy invalid, Instr(Mnemonic.spop, X("")), // 0x32 Instr(Mnemonic.spopws, X("")), // 0x33 Instr(Mnemonic.jsb, X("")), // 0x34 invalid, Instr(Mnemonic.bsbh, X("")), // 0x36 Instr(Mnemonic.bsbb, X("")), // 0x37 Instr(Mnemonic.bitw, X("")), // 0x38 invalid, Instr(Mnemonic.bith, X("")), // 0x3A Instr(Mnemonic.bitb, X("")), // 0x3B Instr(Mnemonic.cmpw, X("")), // 0x3C invalid, Instr(Mnemonic.cmph, X("")), // 0x3E Instr(Mnemonic.cmpb, X("")), // 0x3F // 0x40 Instr(Mnemonic.rgeq, X("")), // 0x40 invalid, Instr(Mnemonic.bgeh, X("")), // 0x42 Instr(Mnemonic.bgeb, X("")), // 0x43 Instr(Mnemonic.rgtr, X("")), // 0x44 invalid, Instr(Mnemonic.bgh, X("")), // 0x46 Instr(Mnemonic.bgb, X("")), // 0x47 Instr(Mnemonic.rlss, X("")), // 0x48 invalid, Instr(Mnemonic.blh, X("")), // 0x4A Instr(Mnemonic.blb, X("")), // 0x4B Instr(Mnemonic.rleq, X("")), // 0x4C invalid, Instr(Mnemonic.bleh, X("")), // 0x4E Instr(Mnemonic.bleb, X("")), // 0x4F // 0x50 Instr(Mnemonic.rcc, X("")), // 0x50 * invalid, //Instr(Mnemonic.rgequ, X("")), // 0x50 * Instr(Mnemonic.bcch, X("")), // 0x52 * //Instr(Mnemonic.bgeuh, X("")), // 0x52 * Instr(Mnemonic.bccb, X("")), // 0x53 * //Instr(Mnemonic.bgeub, X("")), // 0x53 * Instr(Mnemonic.rgtru, X("")), // 0x54 invalid, Instr(Mnemonic.bguh, X("")), // 0x56 Instr(Mnemonic.bgub, X("")), // 0x57 Instr(Mnemonic.rcs, X("")), // 0x58 * //Instr(Mnemonic.rlssu, X("")), // 0x58 * invalid, Instr(Mnemonic.bcsh, X("")), // 0x5A * //Instr(Mnemonic.bluh, X("")), // 0x5A * Instr(Mnemonic.bcsb, X("")), // 0x5B * //Instr(Mnemonic.blub, X("")), // 0x5B * Instr(Mnemonic.rlequ, X("")), // 0x5C invalid, Instr(Mnemonic.bleuh, X("")), // 0x5E Instr(Mnemonic.bleub, X("")), // 0x5F //Instr(Mnemonic.Coprocessor, X("")), // operation //Instr(Mnemonic.Coprocessor, X("")), // operation write single //Instr(Mnemonic.Jump, X("")), // to subroutine //Instr(Mnemonic.Branch, X("")), // to subroutine, halfword displacement //Instr(Mnemonic.Branch, X("")), // to subroutine, byte displacement //Instr(Mnemonic.Bit, X("")), // test word //Instr(Mnemonic.Bit, X("")), // test halfword //Instr(Mnemonic.Bit, X("")), // test byte //Instr(Mnemonic.Compare, X("")), // word //Instr(Mnemonic.Compare, X("")), // halfword //Instr(Mnemonic.Compare, X("")), // byte //Instr(Mnemonic.Return, X("")), // on greater than or equal(signed) //Instr(Mnemonic.Branch), // on greater than or equal halfword(signed) //Instr(Mnemonic.Branch), // on greater than or equal byte(signed) //Instr(Mnemonic.Return), // on greater than(signed) //Instr(Mnemonic.Branch), // on greater than halfword(signed) //Instr(Mnemonic.Branch), // on greater than byte(signed) //Instr(Mnemonic.Return), // on less than(signed) //Instr(Mnemonic.Branch), // on less than halfword(signed) //Instr(Mnemonic.Branch), // on less than byte(signed) //Instr(Mnemonic.Return), // on less than or equal(signed) //Instr(Mnemonic.Branch), // on less than or equal halfword(signed) //Instr(Mnemonic.Branch), // on less than or equal byte(signed) //Instr(Mnemonic.Return), // on carry clear //Instr(Mnemonic.Return), // on greater than or equal(unsigned) //Instr(Mnemonic.Branch), // on carry clear halfword //Instr(Mnemonic.Branch), // on greater than or equal halfword(unsigned) //Instr(Mnemonic.Branch), // on carry clear byte //Instr(Mnemonic.Branch), // on greater than or equal byte(unsigned) //Instr(Mnemonic.Return), // on greater than(unsigned) //Instr(Mnemonic.Branch), // on greater than halfword(unsigned) //Instr(Mnemonic.Branch), // on greater than byte(unsigned) //Instr(Mnemonic.Return), // on carry set //Instr(Mnemonic.Return), // on less than(unsigned) //Instr(Mnemonic.Branch), // on carry set halfword //Instr(Mnemonic.Branch), // on less than halfword(unsigned) //Instr(Mnemonic.Branch), // on carry set byte //Instr(Mnemonic.Branch), // on less than byte(unsigned) //Instr(Mnemonic.Return), // on less than or equal(unsigned) //Instr(Mnemonic.Branch), // on less than or equal halfword(unsigned) //Instr(Mnemonic.Branch), // on less than or equal byte(unsigned) // 0x60 Instr(Mnemonic.rvc, X("")), // 0x60 Return on overflow clear invalid, Instr(Mnemonic.bvch, X("")), // 0x62 Branch on overflow clear, halfword displacement Instr(Mnemonic.bvcb, X("")), // 0x63 Branch on overflow clear, byte displacement Instr(Mnemonic.rnequ, X("")), // 0x64 Return on not equal(unsigned) invalid, Instr(Mnemonic.bneh, X("")), // 0x66 Branch on not equal halfword(duplicate) Instr(Mnemonic.bneb, X("")), // 0x67 Branch on not equal byte(duplicate) Instr(Mnemonic.rvs, X("")), // 0x68 Return on overflow set invalid, Instr(Mnemonic.bvsh, X("")), // 0x6A Branch on overflow set, halfword displacement Instr(Mnemonic.bvsb, X("")), // 0x6B Branch on overflow set, byte displacement Instr(Mnemonic.reqlu, X("")), // 0x6C Return on equal(unsigned) invalid, Instr(Mnemonic.beh, X("")), // 0x6E Branch on equal halfword(duplicate) Instr(Mnemonic.beb, X("")), // 0x6F Branch on equal byte(duplicate) // 0x70 Instr(Mnemonic.nop, X("")), // 0x70 No operation, 1 byte invalid, Instr(Mnemonic.nop3, X("")), // 0x72 No operation, 3 bytes Instr(Mnemonic.nop2, X("")), // 0x73 No operation, 2 bytes Instr(Mnemonic.rneq, X("")), // 0x74 Return on not equal(signed) invalid, Instr(Mnemonic.bneh, X("")), // 0x76 Branch on not equal halfword Instr(Mnemonic.bneb, X("")), // 0x77 Branch on not equal Instr(Mnemonic.rsb, X("")), // 0x78 Return from subroutine invalid, Instr(Mnemonic.brh, X("")), // 0x7A Branch with halfword(J 6 - bit) displacement Instr(Mnemonic.brh, X("")), // 0x7B Branch with byte(8 - bit) displacement Instr(Mnemonic.reql, X("")), // 0x7C Return on equal(signed) invalid, Instr(Mnemonic.beh, X("")), // 0x7E Branch on equal halfword Instr(Mnemonic.beb, X("")), // 0x7F Branch on equal byte // 0x80 Instr(Mnemonic.clrw, X("")), // 0x80 Clear word invalid, Instr(Mnemonic.clrh, X("")), // 0x82 Clear halfword Instr(Mnemonic.clrb, X("")), // 0x83 Clear byte Instr(Mnemonic.movw, X("")), // 0x84 Move word invalid, Instr(Mnemonic.movh, X("")), // 0x86 Move halfword Instr(Mnemonic.movb, Rb, Wb), // 0x87 Move byte Instr(Mnemonic.mcomw, X("")), // 0x88 Move complemented word invalid, Instr(Mnemonic.mcomh, X("")), // 0x8A Move complemented halfword Instr(Mnemonic.mcomb, X("")), // 0x8B Move complemented byte Instr(Mnemonic.mnegw, X("")), // 0x8C Move negated word invalid, Instr(Mnemonic.mnegh, X("")), // 0x8E Move negated halfword Instr(Mnemonic.mnegb, X("")), // 0x8F Move negated byte // 0x90 Instr(Mnemonic.incw, Ww), // 0x90 Increment word invalid, Instr(Mnemonic.inch, Wh), // 0x92 Increment halfword Instr(Mnemonic.incb, Wb), // 0x93 Increment byte Instr(Mnemonic.decw, Ww), // 0x94 Decrement word invalid, Instr(Mnemonic.dech, Wh), // 0x96 Decrement halfword Instr(Mnemonic.decb, Wb), // 0x97 Decrement byte invalid, invalid, invalid, invalid, Instr(Mnemonic.addw2, Rw, Ww), // 0x9C Add word invalid, Instr(Mnemonic.addh2, Rh, Wh), // 0x9E Add halfword Instr(Mnemonic.addb2, Rb, Wb), // 0x9F Add byte // 0xA0 Instr(Mnemonic.pushw, Rw), // 0xA0 // Push word invalid, invalid, invalid, Instr(Mnemonic.modw2, Rw, Ww), // 0xA4), // Modulo word invalid, Instr(Mnemonic.modh2, Rh, Wh), // 0xA6), // Modulo halfword Instr(Mnemonic.modb2, Rb, Wb), // 0xA7), // Modulo byte Instr(Mnemonic.mulw2, Rw, Ww), // 0xA8), // Multiply word invalid, Instr(Mnemonic.mulh2, Rh, Wh), // 0xAA), // Multiply halfword Instr(Mnemonic.mulb2, Rb, Wb), // 0xAB), // Multiply byte Instr(Mnemonic.divw2, Rw, Ww), // 0xAC), // Divide word invalid, Instr(Mnemonic.divh2, Rh, Wh), // 0xAE), // Divide halfword Instr(Mnemonic.divb2, Rb, Wb), // 0xAF), // Divide byte // 0xB0 Instr(Mnemonic.orw2, Rw, Ww), // 0xB0), // OR word invalid, Instr(Mnemonic.orh2, Rh, Wh), // 0xB2), // OR halfword Instr(Mnemonic.orb2, Rb, Wb), // 0xB3), // OR byte Instr(Mnemonic.xorw2, Rw, Ww), // 0xB4), // Exclusive OR word invalid, Instr(Mnemonic.xorh2, Rh, Wh), // 0xB6), // Exclusive OR halfword Instr(Mnemonic.xorb2, Rb, Wb), // 0xB7), // Exclusive OR byte Instr(Mnemonic.andw2, Rw, Ww), // 0xB8), // AND word invalid, Instr(Mnemonic.andh2, Rh, Wh), // 0xBA), // AND halfword Instr(Mnemonic.andb2, Rb, Wb), // 0xBB), // AND byte Instr(Mnemonic.subw2, Rw, Ww), // 0xBC), // Subtract word invalid, Instr(Mnemonic.subh2, Rh, Wh), // 0xBE), // Subtract halfword Instr(Mnemonic.subb2, Rb, Wb), // 0xBF), // Subtract byte // 0xC0 Instr(Mnemonic.alsw3, Rw, Rw, Ww), // 0xCO), // Arithmetic left shift word invalid, invalid, invalid, Instr(Mnemonic.arsw3, Rw, Rw, Ww), // 0xC4), // Arithmetic right shift word invalid, Instr(Mnemonic.arsh3, Rw, Rh, Wh), // 0xC6), // Arithmetic right shift halfword Instr(Mnemonic.arsb3, Rw, Rb, Wb), // 0xC7), // Arithmetic right shift byte Instr(Mnemonic.insfw, X("")), // 0xC8), // Insert field word invalid, Instr(Mnemonic.insfh, X("")), // 0xCA), // Insert field halfword Instr(Mnemonic.insfb, X("")), // 0xCB), // Insert field byte Instr(Mnemonic.extfw, X("")), // 0xCC), // Extract field word invalid, Instr(Mnemonic.extfh, X("")), // 0xCE), // Extract field halfword Instr(Mnemonic.extfb, X("")), // 0xCF), // Extract field byte // 0xD0 Instr(Mnemonic.llsw3, X("")), // 0xD0 // Logical left shift word invalid, Instr(Mnemonic.llsh3, X("")), // 0xD2), // Logical left shift halfword Instr(Mnemonic.llsb3, X("")), // 0xD3), // Logical left shift byte Instr(Mnemonic.lrsw3, X("")), // 0xD4), // Logical right shift word invalid, invalid, invalid, Instr(Mnemonic.rotw, X("")), // 0xD8), // Rotate word invalid, invalid, invalid, Instr(Mnemonic.addw3, Rw, Rw, Ww), // 0xDC Add word, 3 - address invalid, Instr(Mnemonic.addh3, Rh, Rh, Wh), // 0xDE Add halfword, 3 - address Instr(Mnemonic.addb3, Rb, Rb, Wb), // 0xDF Add byte, 3 - address // 0xE0 Instr(Mnemonic.pushaw, X("")), // 0xEO Push address word invalid, invalid, invalid, Instr(Mnemonic.modw3, X("")), // 0xE4 Modulo word, 3 - address invalid, Instr(Mnemonic.modh3, X("")), // 0xE6 Modulo halfword, 3 - address Instr(Mnemonic.modb3, X("")), // 0xE? Modulo byte, 3 - address Instr(Mnemonic.mulw3, X("")), // 0xE8 Multiply word, 3 - address invalid, Instr(Mnemonic.mulh3, X("")), // 0xEA Multiply halfword, 3 - address Instr(Mnemonic.mulb3, X("")), // 0xEB Multiply byte, 3 - address Instr(Mnemonic.divw3, X("")), // 0xEC Divide word, 3 - address invalid, Instr(Mnemonic.divh3, X("")), // 0xEE Divide halfword, 3 - address Instr(Mnemonic.divb3, X("")), // 0xEF Divide byte, 3 - address // 0xF0 Instr(Mnemonic.orw3, X("")), // 0xF0 OR word, 3 - address invalid, Instr(Mnemonic.orh3, X("")), // 0xF2 OR halfword, 3 - address Instr(Mnemonic.orb3, X("")), // 0xF3 OR byte, 3 - address Instr(Mnemonic.xorw3, X("")), // 0xF4 Exclusive OR word, 3 - address invalid, Instr(Mnemonic.xorh3, X("")), // 0xF6 Exclusive OR halfword, 3 - address Instr(Mnemonic.xorb3, X("")), // 0xF? Exclusive OR byte, 3 - address Instr(Mnemonic.andw3, X("")), // 0xF8 AND word, 3 - address invalid, Instr(Mnemonic.andh3, X("")), // 0xFA AND halfword, 3 - address Instr(Mnemonic.andb3, X("")), // 0xFB AND byte, 3 - address Instr(Mnemonic.subw3, X("")), // 0xFC Subtract word, 3 - address invalid, Instr(Mnemonic.subh3, X("")), // 0x FE Subtract halfword, 3 - address Instr(Mnemonic.subb3, X("")) }); // 0xFF Subtract byte, 3 - address
/// <summary> /// Applies the transformation to an input, producing an associated output. /// </summary> /// <param name="input">The input data to which /// the transformation should be applied.</param> /// <param name="result">The location to where to store the /// result of this transformation.</param> /// <returns>The output generated by applying this /// transformation to the given input.</returns> public Sparse <double> Transform(string[] input, out Sparse <double> result) { return(Transform(input, out result, new double[NumberOfWords])); }
static OpenRISCDisassembler() { var invalid = Instr(Mnemonic.Invalid, InstrClass.Invalid); rootDecoder = Mask(26, 6, "OpenRISC", // 00 Instr(Mnemonic.l_j, TD, Pc26), Instr(Mnemonic.l_jal, TDC, Pc26), Instr64( Instr(Mnemonic.l_adrp, RD, Page(19)), Instr(Mnemonic.l_adrp, RD, Page(21))), Instr(Mnemonic.l_bnf, TD | InstrClass.Conditional, Pc26), Instr(Mnemonic.l_bf, TD | InstrClass.Conditional, Pc26), Instr(Mnemonic.l_nop, InstrClass.Linear | InstrClass.Padding), Mask(16, 1, " 0x06", Instr(Mnemonic.l_movhi, RD, Iu16), Instr(Mnemonic.l_macrc, RD)), invalid, Sparse(23, 3, " 0x08", invalid, (0b000, Instr(Mnemonic.l_sys, Iu16)), (0b010, Instr(Mnemonic.l_trap, Iu16)), (0b100, Instr(Mnemonic.l_msync)), (0b101, Instr(Mnemonic.l_psync)), (0b110, Instr(Mnemonic.l_csync))), Instr(Mnemonic.l_rfe, TD), Nyi("0x0A"), invalid, invalid, invalid, invalid, invalid, // 10 invalid, Instr(Mnemonic.l_jr, TD, RB), Instr(Mnemonic.l_jalr, TDC, RB), Instr(Mnemonic.l_maci, RA, Is16), invalid, invalid, invalid, invalid, invalid, invalid, Instr(Mnemonic.l_lf, RD, Mo(16, PrimitiveType.Real32)), Instr(Mnemonic.l_lwa, RD, Mo(16, PrimitiveType.Word32)), invalid, // 0x1C - custom invalid, // 0x1D - custom invalid, // 0x1E - custom invalid, // 0x1F - custom // 20 Instr64( invalid, Instr(Mnemonic.l_ld, RD, Mo(16, PrimitiveType.Word64))), Instr(Mnemonic.l_lwz, RD, Mo(16, PrimitiveType.Word32)), Instr(Mnemonic.l_lws, RD, Mo(16, PrimitiveType.Int32)), Instr(Mnemonic.l_lbz, RD, Mo(16, PrimitiveType.Byte)), Instr(Mnemonic.l_lbs, RD, Mo(16, PrimitiveType.SByte)), Instr(Mnemonic.l_lhz, RD, Mo(16, PrimitiveType.Word16)), Instr(Mnemonic.l_lhs, RD, Mo(16, PrimitiveType.Int16)), Instr(Mnemonic.l_addi, RD, RA, Is16), Instr(Mnemonic.l_addic, RD, RA, Is16), Instr(Mnemonic.l_andi, RD, RA, Iu16), Instr(Mnemonic.l_ori, RD, RA, Iu16), Instr(Mnemonic.l_xori, RD, RA, Iu16), Instr(Mnemonic.l_muli, RD, RA, Is16), Instr(Mnemonic.l_mfspr, RD, RA, Spr(Bf((0, 16)))), Mask(6, 2, " 0x2E", Instr(Mnemonic.l_slli, RD, RA, Iu6), Instr(Mnemonic.l_srli, RD, RA, Iu6), Instr(Mnemonic.l_srai, RD, RA, Iu6), Instr(Mnemonic.l_rori, RD, RA, Iu6)), Sparse(21, 5, " 0x2F", invalid, (0x0, Instr(Mnemonic.l_sfeqi, RA, Isu16)), (0x1, Instr(Mnemonic.l_sfnei, RA, Isu16)), (0x2, Instr(Mnemonic.l_sfgtui, RA, Isu16)), (0x3, Instr(Mnemonic.l_sfgeui, RA, Isu16)), (0x4, Instr(Mnemonic.l_sfltui, RA, Isu16)), (0x5, Instr(Mnemonic.l_sfleui, RA, Isu16)), (0xA, Instr(Mnemonic.l_sfgtsi, RA, Is16)), (0xB, Instr(Mnemonic.l_sfgesi, RA, Is16)), (0xC, Instr(Mnemonic.l_sfltsi, RA, Is16)), (0xD, Instr(Mnemonic.l_sflesi, RA, Is16))), // 30 Instr(Mnemonic.l_mtspr, RA, RB, Spr(Bf((21, 5), (0, 11)))), Nyi("0x31"), Nyi("0x32"), Nyi("0x33"), Instr64( invalid, Instr(Mnemonic.l_sd, Mo(16, Bf((21, 5), (0, 11)), PrimitiveType.Word32), RB)), Instr(Mnemonic.l_sw, Mo(16, Bf((21, 5), (0, 11)), PrimitiveType.Word32), RB), Instr(Mnemonic.l_sb, Mo(16, Bf((21, 5), (0, 11)), PrimitiveType.Byte), RB), Instr(Mnemonic.l_sh, Mo(16, Bf((21, 5), (0, 11)), PrimitiveType.Word16), RB), Mask(8, 2, " 0x38", Mask(0, 4, " 0x38-0", Instr(Mnemonic.l_add, RD, RA, RB), Instr(Mnemonic.l_addc, RD, RA, RB), Instr(Mnemonic.l_sub, RD, RA, RB), Instr(Mnemonic.l_and, RD, RA, RB), Instr(Mnemonic.l_or, RD, RA, RB), Instr(Mnemonic.l_xor, RD, RA, RB), invalid, invalid, Mask(6, 2, " 0x30-0-8", Instr(Mnemonic.l_sll, RD, RA, RB), Instr(Mnemonic.l_srl, RD, RA, RB), Instr(Mnemonic.l_sra, RD, RA, RB), Instr(Mnemonic.l_ror, RD, RA, RB)), invalid, invalid, invalid, Nyi(" 0x38-0-C"), Nyi(" 0x38-0-D"), Instr(Mnemonic.l_cmov, RD, RA, RB), Instr(Mnemonic.l_ff1, RD, RA)), Sparse(0, 4, " 0x38-1", invalid, (0xF, Instr(Mnemonic.l_fl1, RD, RA))), Nyi(" 0x38-2"), Sparse(0, 4, " 0x38-3", invalid, (0x6, Instr(Mnemonic.l_mul, RD, RA, RB)), (0x7, Instr(Mnemonic.l_muld, RA, RB)), (0x9, Instr(Mnemonic.l_div, RD, RA, RB)), (0xA, Instr(Mnemonic.l_divu, RD, RA, RB)), (0xB, Instr(Mnemonic.l_mulu, RD, RA, RB)), (0xC, Instr(Mnemonic.l_muldu, RA, RB)))), Sparse(21, 5, " 0x39", Nyi("0x39"), (0x0, Instr(Mnemonic.l_sfeq, RA, RB)), (0x1, Instr(Mnemonic.l_sfne, RA, RB)), (0x2, Instr(Mnemonic.l_sfgtu, RA, RB)), (0x3, Instr(Mnemonic.l_sfgeu, RA, RB)), (0x4, Instr(Mnemonic.l_sfltu, RA, RB)), (0x5, Instr(Mnemonic.l_sfleu, RA, RB)), (0xA, Instr(Mnemonic.l_sfgts, RA, RB)), (0xB, Instr(Mnemonic.l_sfges, RA, RB)), (0xC, Instr(Mnemonic.l_sflts, RA, RB)), (0xD, Instr(Mnemonic.l_sfles, RA, RB))), Nyi("0x3A"), Nyi("0x3B"), Nyi("0x3C"), Nyi("0x3D"), Nyi("0x3E"), Nyi("0x3F")); }
static XCore200Disassembler() { var invalid = Instr(Mnemonic.Invalid, InstrClass.Invalid); bool is_0x1F(uint u) => u == 0x1F; bool is_lt27(uint u) => u < 27; var longDecoder = Mask(11, 5, " long", Nyi("00"), Nyi("01"), Nyi("02"), Nyi("03"), Nyi("04"), Nyi("05"), Nyi("06"), Nyi("07"), Nyi("08"), Nyi("09"), Nyi("0A"), Nyi("0B"), Nyi("0C"), Nyi("0D"), Nyi("0E"), Nyi("0F"), // 10 Nyi("10"), Nyi("11"), Nyi("12"), Nyi("13"), Nyi("14"), Nyi("15"), Nyi("16"), Nyi("17"), Nyi("18"), Nyi("19"), Nyi("1A"), Nyi("1B"), Nyi("1C"), Nyi("1D"), Nyi("1E"), Sparse(11, 5, " long 1F", Nyi(""), (2, Instr(Mnemonic.ashr, l3r)))); var decode_0F_0 = Nyi("0F_0"); var decode_0F_1 = Nyi("0F_1"); rootDecoder = Mask(11, 5, "XCore", Select((6, 5), is_lt27, Instr(Mnemonic.stwi, r2us), Mask(4, 2, " 00", Instr(Mnemonic.byterev, r2), Select((6, 5), is_0x1F, Nyi("00 01 11111"), Instr(Mnemonic.getst, r2)), Select((6, 5), is_0x1F, Sparse(0, 4, Instr(Mnemonic.edu, r1), (0xC, Instr(Mnemonic.waiteu)), (0xD, Instr(Mnemonic.clre)), (0xE, Instr(Mnemonic.ssync)), (0xF, Instr(Mnemonic.freet))), Instr(Mnemonic.getst, r2)), Select((6, 5), is_0x1F, Sparse(0, 4, Instr(Mnemonic.eeu, r1), (0xD, Instr(Mnemonic.dcall)), (0xF, Instr(Mnemonic.setkep))), Instr(Mnemonic.getst, r2)))), Select((6, 5), is_lt27, " 01", Instr(Mnemonic.ldwi, r2us), Mask(4, 2, " 01", Instr(Mnemonic.clz, r2), Select((6, 5), is_0x1F, " 01 01", Nyi("01 01 0x1F"), Instr(Mnemonic.outt, r2r)), Select((6, 5), is_0x1F, " 01 10", Sparse(0, 4, " 01 10 0x1F", Instr(Mnemonic.waitet, r1), (0xC, Instr(Mnemonic.ldspc)), (0xD, Instr(Mnemonic.stspc)), (0xE, Instr(Mnemonic.stssr)), (0xF, Instr(Mnemonic.ldssr))), Instr(Mnemonic.clz, r2)), Select((6, 5), is_0x1F, " 01 11", Sparse(0, 4, " 01 11 0x1F", Instr(Mnemonic.waitef, r1), (0xC, Instr(Mnemonic.stsed)), (0xD, Instr(Mnemonic.stet)), (0xE, Instr(Mnemonic.geted)), (0xF, Instr(Mnemonic.getet))), Instr(Mnemonic.outt, r2r)))), Select((6, 5), is_lt27, " 02", Instr(Mnemonic.add, r3), Mask(4, 1, " 02 >=27", Select((6, 5), is_0x1F, " 02 0", Sparse(0, 4, " 02 0 0x1F", Instr(Mnemonic.freer, r1), (0xC, Instr(Mnemonic.getps)), (0xD, invalid), (0xE, Instr(Mnemonic.getid)), (0xF, Instr(Mnemonic.getkep))), Instr(Mnemonic.bitrev, r2)), Select((6, 5), is_0x1F, " 02 1", Sparse(0, 4, " 02 1 0x1F", Instr(Mnemonic.mjoin, r1), (0xC, Instr(Mnemonic.getksp)), (0xD, Instr(Mnemonic.ldsed)), (0xE, Instr(Mnemonic.ldset)), (0xF, Instr(Mnemonic.nop))), Instr(Mnemonic.setd, r2r)))), Select((6, 5), is_lt27, " 03", Instr(Mnemonic.sub, r3), Mask(4, 1, " 03 >= 27", Select((6, 5), is_0x1F, " 03 0", Sparse(0, 4, " 03 0 0x1F", Instr(Mnemonic.tstart, r1), (0xC, Nyi("03 0 0x1F 0xC")), (0xD, Nyi("03 0 0x1F 0xD")), (0xE, Nyi("03 0 0x1F 0xE")), (0xF, Nyi("03 0 0x1F 0xF"))), Nyi(" 03 0 !0x1F")), Select((6, 5), is_0x1F, " 03 0", Sparse(0, 4, " 03 1 0x1F", Instr(Mnemonic.msync, r1), (0xC, Nyi("03 1 0x1F 0xC")), (0xD, Nyi("03 1 0x1F 0xD")), (0xE, Nyi("03 1 0x1F 0xE")), (0xF, Nyi("03 1 0x1F 0xF"))), Nyi(" 03 0 !0x1F")))), Mask(4, 1, " 04", Select((5, 6), u => u == 0x3F, " 04 - 0 0x3F", Instr(Mnemonic.bla, InstrClass.Transfer | InstrClass.Call, r1), Nyi("04 - 0")), Select((5, 6), u => u == 0x3F, " 04 - 1 0x3F", Instr(Mnemonic.bau, InstrClass.Transfer, r1), Instr(Mnemonic.eet, r2))), Mask(4, 1, " 05", Select((6, 5), u => u == 0x1F, Instr(Mnemonic.bru, r1), Instr(Mnemonic.andnot, r2)), Instr(Mnemonic.eef, r2)), Instr(Mnemonic.eq, r3), Instr(Mnemonic.and, r3), Select((6, 5), is_0x1F, Instr(Mnemonic.setv, r1), Instr(Mnemonic.or, r3)), Select((10, 1), u => u == 0, " 0A", Instr(Mnemonic.outct, r2), Instr(Mnemonic.outcti, rus)), Select((10, 1), u => u == 0, " 0A", Instr(Mnemonic.stwdp, ru6), Instr(Mnemonic.stwsp, ru6)), Select((10, 1), u => u == 0, " 0B", Instr(Mnemonic.ldwdp, ru6), Instr(Mnemonic.ldwsp, ru6)), Select((10, 1), u => u == 0, " 0C", Instr(Mnemonic.ldawdp, ru6), Instr(Mnemonic.ldawsp, ru6)), Select((10, 1), u => u == 0, " 0D", Instr(Mnemonic.ldc, ru6), Instr(Mnemonic.ldwcp, ru6)), Select((10, 1), u => u == 0, " 0E", Sparse(6, 4, " 0E_0", Instr(Mnemonic.brft, InstrClass.ConditionalTransfer, ru6_p_pc), (0xC, Instr(Mnemonic.brfu, InstrClass.Transfer, u6_p_pc)), (0xD, Instr(Mnemonic.blat, InstrClass.Transfer | InstrClass.Call, u6)), (0xE, Instr(Mnemonic.extdp, u6)), (0xF, Instr(Mnemonic.kcalli, InstrClass.Transfer | InstrClass.Call, u6))), Sparse(6, 4, " 0E_1", Instr(Mnemonic.brbt, InstrClass.ConditionalTransfer, ru6_m_pc), (0xC, Instr(Mnemonic.brbu, InstrClass.Transfer, u6_m_pc)), (0xD, Instr(Mnemonic.entsp, u6)), (0xE, Instr(Mnemonic.extsp, u6)), (0xF, Instr(Mnemonic.retsp, u6)))), Select((10, 1), u => u == 0, " 0F", Sparse(6, 4, " 0F_0", Instr(Mnemonic.brff, InstrClass.ConditionalTransfer, ru6_p_pc), (0xC, Instr(Mnemonic.clrsr, u6)), (0xD, Instr(Mnemonic.setsr, u6)), (0xE, Instr(Mnemonic.kentsp, u6)), (0xF, Nyi("0F_0 F"))), Sparse(6, 4, " 0F_0", Instr(Mnemonic.brbf, InstrClass.ConditionalTransfer, ru6_m_pc), (0xC, Instr(Mnemonic.getsr, u6)), (0xD, Instr(Mnemonic.ldawcp, u6)), (0xE, Instr(Mnemonic.dualentsp, u6)), (0xF, Nyi("0F_0 F")))), // 10 Instr(Mnemonic.ld16s, r3), Instr(Mnemonic.ld8u, r3), Select((6, 5), is_lt27, " 12", Instr(Mnemonic.addi, r2us), Mask(4, 1, " 12 >=27", Instr(Mnemonic.neg, r2), Instr(Mnemonic.endin, r2))), Select((6, 5), is_lt27, " 13", Instr(Mnemonic.subi, r2us), Nyi(" 13 >=27")), Select((6, 5), is_lt27, Instr(Mnemonic.shli, r2us_bitp), Mask(4, 1, " 14 >= 27", Instr(Mnemonic.mkmsk, r2), Instr(Mnemonic.mkmski, rus_bitp))), Select((6, 5), is_lt27, Instr(Mnemonic.shri, r2us_bitp), Mask(4, 1, " 15 >= 27", Instr(Mnemonic.@out, r2), Instr(Mnemonic.outshr, r2r))), Select((6, 5), is_lt27, Instr(Mnemonic.eqi, r2us), Mask(4, 1, " 16 >= 27", Instr(Mnemonic.@in, r2), Instr(Mnemonic.inshr, r2))), Mask(4, 1, " 17", Instr(Mnemonic.peek, r2), Instr(Mnemonic.testct, r2)), Select((6, 5), is_lt27, Instr(Mnemonic.lss, r3), Mask(4, 1, " 18 >= 27", Instr(Mnemonic.setpsc, r2r), Instr(Mnemonic.testwct, r2))), Select((6, 5), is_lt27, Instr(Mnemonic.lsu, r3), Mask(4, 1, " 19 >= 27", Instr(Mnemonic.chkct, r2), Instr(Mnemonic.chkcti, rus))), Mask(10, 1, " 1A", Instr(Mnemonic.blrf, InstrClass.Transfer | InstrClass.Call, u10_p_pc), Instr(Mnemonic.blrb, InstrClass.Transfer | InstrClass.Call, u10_m_pc)), Mask(10, 1, " 1B", Instr(Mnemonic.ldapb, PcRel11_2), Instr(Mnemonic.ldapf, PcRel11_2)), Mask(10, 1, " 1C", Instr(Mnemonic.blacp, InstrClass.Transfer | InstrClass.Call, u10), Instr(Mnemonic.ldwcp, u10)), Mask(10, 1, " 1D", Instr(Mnemonic.setci, ru6), Nyi(" 1D 1")), Nyi("1E"), new W32Decoder(longDecoder)); }
/// <summary> /// The kernel function. /// </summary> /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns> /// Dot product in feature (kernel) space. /// </returns> public double Function(Sparse <double> x, Sparse <double> y) { return(x.Dot(y) + constant); }
static H8Disassembler() { var invalid = Instr(Mnemonic.Invalid, InstrClass.Invalid); var mov_b_aa8_ld = Instr(Mnemonic.mov, b, aa8, r8); var mov_b_aa8_st = Instr(Mnemonic.mov, b, r8, aa8); var add_b_imm = Instr(Mnemonic.add, b, I8, rb8); var and_b_imm = Instr(Mnemonic.and, b, I8, rb8); var addx_b_imm = Instr(Mnemonic.addx, b, I8, rb8); var cmp_b_imm = Instr(Mnemonic.cmp, b, I8, rb8); var mov_b_imm = Instr(Mnemonic.mov, b, I8, rb8); var or_b_imm = Instr(Mnemonic.or, b, I8, rb8); var subx_b_imm = Instr(Mnemonic.subx, b, I8, rb8); var xor_b_imm = Instr(Mnemonic.xor, b, I8, rb8); rootDecoder = Mask(8, 8, "H8", new Decoder <H8Disassembler, Mnemonic, H8Instruction>[256] { Select((0, 8), u => u == 0, " 00", Instr(Mnemonic.nop, InstrClass.Linear | InstrClass.Padding | InstrClass.Zero), invalid), Sparse(4, 4, " 01", invalid, (0x0, Nyi("mov")), (0x4, Nyi("ldc/stc")), (0x8, Nyi("sleep")), (0xC, Select((0, 4), u => u == 0, " 01 C0", Nyi("01 C0"), invalid)), (0xD, Select((0, 4), u => u == 0, " 01 D0", Nyi("01 D0"), invalid)), (0xF, Nyi("Table 2-6"))), Select((4, 4), u => u == 0, Instr(Mnemonic.stc, b, ccr, rl), invalid), Select((4, 4), u => u == 0, Instr(Mnemonic.ldc, b, rl, ccr), invalid), Instr(Mnemonic.orc, I8, ccr), Instr(Mnemonic.xorc, I8, ccr), Instr(Mnemonic.andc, I8, ccr), Instr(Mnemonic.ldc, b, I8, ccr), Instr(Mnemonic.add, b, rh, rl), Instr(Mnemonic.add, w, rh, rl), Nyi("2-5"), Sparse(4, 4, " 0B", invalid, (0, Instr(Mnemonic.adds, Imm32(1), rgpl)), (8, Instr(Mnemonic.adds, Imm32(2), rgpl)), (9, Instr(Mnemonic.adds, Imm32(3), rgpl))), Instr(Mnemonic.mov, b, rh, rl), Instr(Mnemonic.mov, w, rh, rl), Nyi("addx"), Nyi("2-5"), // 10 Sparse(4, 4, " 10", invalid, (0x0, Instr(Mnemonic.shll, b, rl)), (0x1, Instr(Mnemonic.shll, w, rl)), (0x3, Instr(Mnemonic.shll, l, rl)), (0x8, Instr(Mnemonic.shal, b, rl)), (0x9, Instr(Mnemonic.shal, w, rl)), (0xB, Instr(Mnemonic.shal, l, rl))), Sparse(4, 4, " 11", invalid, (0x0, Instr(Mnemonic.shlr, b, rl)), (0x1, Instr(Mnemonic.shlr, w, rl)), (0x3, Instr(Mnemonic.shlr, l, rl)), (0x8, Instr(Mnemonic.shar, b, rl)), (0x9, Instr(Mnemonic.shar, w, rl)), (0xB, Instr(Mnemonic.shar, l, rl))), Sparse(4, 4, " 12", invalid, (0x0, Instr(Mnemonic.rotxl, b, rl)), (0x1, Instr(Mnemonic.rotxl, w, rl)), (0x3, Instr(Mnemonic.rotxl, l, rl)), (0x8, Instr(Mnemonic.rotl, b, rl)), (0x9, Instr(Mnemonic.rotl, w, rl)), (0xB, Instr(Mnemonic.rotl, l, rl))), Sparse(4, 4, " 13", invalid, (0x0, Instr(Mnemonic.rotxr, b, rl)), (0x1, Instr(Mnemonic.rotxr, w, rl)), (0x3, Instr(Mnemonic.rotxr, l, rl)), (0x8, Instr(Mnemonic.rotr, b, rl)), (0x9, Instr(Mnemonic.rotr, w, rl)), (0xB, Instr(Mnemonic.rotr, l, rl))), Instr(Mnemonic.or, b, rh, rl), Instr(Mnemonic.xor, b, rh, rl), Instr(Mnemonic.and, b, rh, rl), Mask(4, 4, " 17", Instr(Mnemonic.not, b, rl), Instr(Mnemonic.not, w, rl), invalid, Instr(Mnemonic.not, l, rl), invalid, Instr(Mnemonic.extu, w, rl), invalid, Instr(Mnemonic.extu, l, rl), Nyi("neg"), Nyi("neg"), invalid, Nyi("neg"), invalid, Instr(Mnemonic.exts, w, rl), invalid, Instr(Mnemonic.exts, l, rl)), Instr(Mnemonic.sub, b, rh, rl), Instr(Mnemonic.sub, w, rh, rl), Sparse(4, 4, " 1A", invalid, (0, Instr(Mnemonic.dec, b, rl)), (0x8, Instr(Mnemonic.sub, l, rh, rl)), (0x9, Instr(Mnemonic.sub, l, rh, rl)), (0xA, Instr(Mnemonic.sub, l, rh, rl)), (0xB, Instr(Mnemonic.sub, l, rh, rl)), (0xC, Instr(Mnemonic.sub, l, rh, rl)), (0xD, Instr(Mnemonic.sub, l, rh, rl)), (0xE, Instr(Mnemonic.sub, l, rh, rl)), (0xF, Instr(Mnemonic.sub, l, rh, rl))), Sparse(4, 4, " 1B", invalid, (0, Instr(Mnemonic.subs, Imm32(1), rgpl)), (8, Instr(Mnemonic.subs, Imm32(2), rgpl)), (9, Instr(Mnemonic.subs, Imm32(3), rgpl))), Instr(Mnemonic.cmp, b, rh, rl), Instr(Mnemonic.cmp, w, rh, rl), Instr(Mnemonic.subx, b, rh, rl), Sparse(4, 4, " 1F", invalid, (0, Nyi("das")), (0x8, Instr(Mnemonic.cmp, l, rh3, rl3)), (0x9, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xA, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xB, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xC, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xD, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xE, Instr(Mnemonic.cmp, l, rh3, rl3)), (0xF, Instr(Mnemonic.cmp, l, rh3, rl3))), // 20 mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, mov_b_aa8_ld, // 30 mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, mov_b_aa8_st, // 40 Instr(Mnemonic.bra, InstrClass.Transfer, disp8), Instr(Mnemonic.brn, InstrClass.Linear | InstrClass.Padding, disp8), Instr(Mnemonic.bhi, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bls, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bcc, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bcs, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bne, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.beq, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bvc, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bvs, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bpl, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bmi, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bge, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.blt, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.bgt, InstrClass.ConditionalTransfer, disp8), Instr(Mnemonic.ble, InstrClass.ConditionalTransfer, disp8), // 50 Instr(Mnemonic.mulxu, b, rh, rl), Nyi("divxu"), Instr(Mnemonic.mulxu, w, rh, rl), Nyi("divxu"), Select((0, 8), u => u == 0x70, " 5E", Instr(Mnemonic.rts, InstrClass.Transfer), Nyi("rts")), Nyi("bsr"), Nyi("rte"), Nyi("trapa"), Nyi("2-5"), Instr(Mnemonic.jmp, InstrClass.Transfer, Mind), Instr(Mnemonic.jmp, InstrClass.Transfer, aa24_16), Instr(Mnemonic.jmp, InstrClass.Transfer, Def_aa8), Nyi("bsr"), Instr(Mnemonic.jsr, InstrClass.Transfer | InstrClass.Call, Mind), Instr(Mnemonic.jsr, InstrClass.Transfer | InstrClass.Call, aa24_16), Instr(Mnemonic.jsr, InstrClass.Transfer | InstrClass.Call, Def_aa8), // 60 Nyi("bset"), Nyi("bnot"), Nyi("bclr"), Nyi("btst"), Instr(Mnemonic.or, w, rh, rl), Instr(Mnemonic.xor, w, rh, rl), Instr(Mnemonic.and, w, rh, rl), Mask(7, 1, " 67", Instr(Mnemonic.bst, Bit, rbl), Instr(Mnemonic.bist, Bit, rbl)), Mask(7, 1, " 68", Instr(Mnemonic.mov, b, Mind, rl), Instr(Mnemonic.mov, b, rl, Mind)), Mask(7, 1, " 69", Instr(Mnemonic.mov, w, Mind, rl), Instr(Mnemonic.mov, w, rl, Mind)), Sparse(4, 4, " 6A", invalid, (0x0, Instr(Mnemonic.mov, b, aa16, rl)), (0x2, Instr(Mnemonic.mov, b, aa24, rl)), (0x8, Instr(Mnemonic.mov, b, rl, aa16)), (0xA, Instr(Mnemonic.mov, b, rl, aa24))), Sparse(4, 4, " 6B", invalid, (0x0, Instr(Mnemonic.mov, w, aa16, rl)), (0x2, Instr(Mnemonic.mov, w, aa24, rl)), (0x8, Instr(Mnemonic.mov, w, rl, aa16)), (0xA, Instr(Mnemonic.mov, w, rl, aa24))), Nyi("mov"), Mask(7, 1, " 6D", Instr(Mnemonic.mov, w, PostEh, rl), Instr(Mnemonic.mov, w, rl, PreEh)), Mask(7, 1, " 6E", Instr(Mnemonic.mov, b, Md16, rl), Instr(Mnemonic.mov, b, rl, Md16)), Mask(7, 1, " 6F", Instr(Mnemonic.mov, w, Md16, rl), Instr(Mnemonic.mov, w, rl, Md16)), // 70 Instr(Mnemonic.bset, Bit, rbl), Instr(Mnemonic.bnot, Bit, rbl), Instr(Mnemonic.bclr, Bit, rbl), Instr(Mnemonic.btst, Bit, rbl), Nyi("bor/bior"), Nyi("bxor/bixor"), Nyi("band/biand"), Mask(7, 1, " 67", Instr(Mnemonic.bld, Bit, rbl), Instr(Mnemonic.bild, Bit, rbl)), Nyi("mov"), Sparse(4, 4, " 79", Nyi("79"), (0, Instr(Mnemonic.mov, w, I16, rl)), (7, invalid)), Nyi("2-5"), Nyi("eepmov"), Nyi("2-6"), Nyi("2-6"), Nyi("2-6"), Nyi("2-6"), // 80 add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, add_b_imm, // 90 addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, addx_b_imm, // A0 cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, cmp_b_imm, // B0 subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, subx_b_imm, // C0 or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, or_b_imm, // D0 xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, xor_b_imm, // E0 and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, and_b_imm, // F0 mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm, mov_b_imm });;
/// <summary> /// The kernel function. /// </summary> /// <param name="x">Vector <c>x</c> in input space.</param> /// <param name="y">Vector <c>y</c> in input space.</param> /// <returns> /// Dot product in feature (kernel) space. /// </returns> public double Function(double[] y, Sparse <double> x) { return(x.Dot(y) + constant); }