QueryMath.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Runtime;
  7. internal enum MathOperator
  8. {
  9. None,
  10. Plus,
  11. Minus,
  12. Div,
  13. Multiply,
  14. Mod,
  15. Negate
  16. }
  17. internal class MathOpcode : Opcode
  18. {
  19. MathOperator mathOp;
  20. internal MathOpcode(OpcodeID id, MathOperator op)
  21. : base(id)
  22. {
  23. this.mathOp = op;
  24. }
  25. internal override bool Equals(Opcode op)
  26. {
  27. if (base.Equals(op))
  28. {
  29. return (this.mathOp == ((MathOpcode) op).mathOp);
  30. }
  31. return false;
  32. }
  33. #if DEBUG_FILTER
  34. public override string ToString()
  35. {
  36. return string.Format("{0} {1}", base.ToString(), this.mathOp.ToString());
  37. }
  38. #endif
  39. }
  40. internal class PlusOpcode : MathOpcode
  41. {
  42. internal PlusOpcode()
  43. : base(OpcodeID.Plus, MathOperator.Plus)
  44. {
  45. }
  46. internal override Opcode Eval(ProcessingContext context)
  47. {
  48. StackFrame argX = context.TopArg;
  49. StackFrame argY = context.SecondArg;
  50. Fx.Assert(argX.Count == argY.Count, "");
  51. Value[] values = context.Values;
  52. for (int x = argX.basePtr, y = argY.basePtr; x <= argX.endPtr; ++x, ++y)
  53. {
  54. Fx.Assert(values[x].IsType(ValueDataType.Double), "");
  55. Fx.Assert(values[y].IsType(ValueDataType.Double), "");
  56. values[y].Add(values[x].Double);
  57. }
  58. context.PopFrame();
  59. return this.next;
  60. }
  61. }
  62. internal class MinusOpcode : MathOpcode
  63. {
  64. internal MinusOpcode()
  65. : base(OpcodeID.Minus, MathOperator.Minus)
  66. {
  67. }
  68. internal override Opcode Eval(ProcessingContext context)
  69. {
  70. StackFrame argX = context.TopArg;
  71. StackFrame argY = context.SecondArg;
  72. Fx.Assert(argX.Count == argY.Count, "");
  73. Value[] values = context.Values;
  74. for (int x = argX.basePtr, y = argY.basePtr; x <= argX.endPtr; ++x, ++y)
  75. {
  76. Fx.Assert(values[x].IsType(ValueDataType.Double), "");
  77. Fx.Assert(values[y].IsType(ValueDataType.Double), "");
  78. values[y].Double = values[x].Double - values[y].Double;
  79. }
  80. context.PopFrame();
  81. return this.next;
  82. }
  83. }
  84. internal class MultiplyOpcode : MathOpcode
  85. {
  86. internal MultiplyOpcode()
  87. : base(OpcodeID.Multiply, MathOperator.Multiply)
  88. {
  89. }
  90. internal override Opcode Eval(ProcessingContext context)
  91. {
  92. StackFrame argX = context.TopArg;
  93. StackFrame argY = context.SecondArg;
  94. Fx.Assert(argX.Count == argY.Count, "");
  95. Value[] values = context.Values;
  96. for (int x = argX.basePtr, y = argY.basePtr; x <= argX.endPtr; ++x, ++y)
  97. {
  98. Fx.Assert(values[x].IsType(ValueDataType.Double), "");
  99. Fx.Assert(values[y].IsType(ValueDataType.Double), "");
  100. values[y].Multiply(values[x].Double);
  101. }
  102. context.PopFrame();
  103. return this.next;
  104. }
  105. }
  106. internal class DivideOpcode : MathOpcode
  107. {
  108. internal DivideOpcode()
  109. : base(OpcodeID.Divide, MathOperator.Div)
  110. {
  111. }
  112. internal override Opcode Eval(ProcessingContext context)
  113. {
  114. StackFrame argX = context.TopArg;
  115. StackFrame argY = context.SecondArg;
  116. Fx.Assert(argX.Count == argY.Count, "");
  117. Value[] values = context.Values;
  118. for (int x = argX.basePtr, y = argY.basePtr; x <= argX.endPtr; ++x, ++y)
  119. {
  120. Fx.Assert(values[x].IsType(ValueDataType.Double), "");
  121. Fx.Assert(values[y].IsType(ValueDataType.Double), "");
  122. values[y].Double = values[x].Double / values[y].Double;
  123. }
  124. context.PopFrame();
  125. return this.next;
  126. }
  127. }
  128. internal class ModulusOpcode : MathOpcode
  129. {
  130. internal ModulusOpcode()
  131. : base(OpcodeID.Mod, MathOperator.Mod)
  132. {
  133. }
  134. internal override Opcode Eval(ProcessingContext context)
  135. {
  136. StackFrame argX = context.TopArg;
  137. StackFrame argY = context.SecondArg;
  138. Value[] values = context.Values;
  139. Fx.Assert(argX.Count == argY.Count, "");
  140. for (int x = argX.basePtr, y = argY.basePtr; x <= argX.endPtr; ++x, ++y)
  141. {
  142. Fx.Assert(values[x].IsType(ValueDataType.Double), "");
  143. Fx.Assert(values[y].IsType(ValueDataType.Double), "");
  144. values[y].Double = values[x].Double % values[y].Double;
  145. }
  146. context.PopFrame();
  147. return this.next;
  148. }
  149. }
  150. internal class NegateOpcode : MathOpcode
  151. {
  152. internal NegateOpcode()
  153. : base(OpcodeID.Negate, MathOperator.Negate)
  154. {
  155. }
  156. internal override Opcode Eval(ProcessingContext context)
  157. {
  158. StackFrame frame = context.TopArg;
  159. Value[] values = context.Values;
  160. for (int i = frame.basePtr; i <= frame.endPtr; ++i)
  161. {
  162. values[i].Negate();
  163. }
  164. return this.next;
  165. }
  166. }
  167. }