4 // Authors: Marek Safar (marek.safar@gmail.com)
6 // Copyright 2014 Xamarin Inc
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Diagnostics;
31 using Microsoft.Scripting.Runtime;
32 using Microsoft.Scripting.Utils;
34 namespace Microsoft.Scripting.Interpreter {
35 internal abstract class ModInstruction : ArithmeticInstruction {
36 private static Instruction _Int16, _Int32, _Int64, _UInt16, _UInt32, _UInt64, _Single, _Double;
38 private ModInstruction() {
41 internal sealed class ModInt32 : ModInstruction {
42 protected override object Calculate (object l, object r)
44 return ScriptingRuntimeHelpers.Int32ToObject(unchecked((Int32)l % (Int32)r));
48 internal sealed class ModInt16 : ModInstruction {
49 protected override object Calculate (object l, object r)
51 return (Int16)unchecked((Int16)l % (Int16)r);
55 internal sealed class ModInt64 : ModInstruction {
56 protected override object Calculate (object l, object r)
58 return (Int64)unchecked((Int64)l % (Int64)r);
62 internal sealed class ModUInt16 : ModInstruction {
63 protected override object Calculate (object l, object r)
65 return (UInt16)unchecked((UInt16)l % (UInt16)r);
69 internal sealed class ModUInt32 : ModInstruction {
70 protected override object Calculate (object l, object r)
72 return (UInt32)unchecked((UInt32)l % (UInt32)r);
76 internal sealed class ModUInt64 : ModInstruction {
77 protected override object Calculate (object l, object r)
79 return (UInt64)unchecked((UInt64)l % (UInt64)r);
83 internal sealed class ModSingle : ModInstruction {
84 protected override object Calculate (object l, object r)
86 return (Single)((Single)l % (Single)r);
90 internal sealed class ModDouble : ModInstruction {
91 protected override object Calculate (object l, object r)
93 return (Double)l % (Double)r;
97 public static Instruction Create(Type type) {
98 Debug.Assert(!type.IsEnum());
99 switch (type.GetTypeCode()) {
100 case TypeCode.Int16: return _Int16 ?? (_Int16 = new ModInt16());
101 case TypeCode.Int32: return _Int32 ?? (_Int32 = new ModInt32());
102 case TypeCode.Int64: return _Int64 ?? (_Int64 = new ModInt64());
103 case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new ModUInt16());
104 case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new ModUInt32());
105 case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new ModUInt64());
106 case TypeCode.Single: return _Single ?? (_Single = new ModSingle());
107 case TypeCode.Double: return _Double ?? (_Double = new ModDouble());
110 throw Assert.Unreachable;
114 public override string ToString() {