forked from dotnet/macios
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrimitives.tt
More file actions
441 lines (396 loc) · 15.4 KB
/
Primitives.tt
File metadata and controls
441 lines (396 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// !!! WARNING - GENERATED CODE - DO NOT EDIT !!!
//
// Generated by Primitives.tt, a T4 template.
//
// Primitives.cs: basic types with 32 or 64 bit sizes:
//
// - nint
// - nuint
// - nfloat
//
// The primitive n* types are optimized by the Mono JIT to perform
// at native int/long/float/double speeds. When NINT_JIT_OPTIMIZED
// is defined, all operators will throw NotImplementedException to
// ensure the JIT works as expected. Otherwise operations are
// carried out slowly using managed code.
//
// If ARCH_32 is defined, the underlying types for n* types will be
// 32 bit (int, uint, float). If not defined, the underlying types
// will be 64 bit (long, ulong, double).
//
// Authors:
// Aaron Bockover <[email protected]>
//
// Copyright 2013 Xamarin, Inc. All rights reserved.
//
#if NET
#define OBJCRUNTIME_nfloat
#endif
<#@ template language="C#v3.5" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using ObjCRuntime;
<#
foreach (var type in new [] {
new { NSName = "nint", CilName32 = "Int32", CilName64 = "Int64", IsIntegerType = true },
new { NSName = "nuint", CilName32 = "UInt32", CilName64 = "UInt64", IsIntegerType = true },
new { NSName = "nfloat", CilName32 = "Single", CilName64 = "Double", IsIntegerType = false }
}) {
var unops = new List<string> { "+" };
if (type.NSName != "nuint") {
unops.Add ("-");
}
if (type.IsIntegerType) {
unops.Add ("~");
}
var binops = new List<string> { "+", "-", "*", "/", "%" };
if (type.IsIntegerType) {
binops.Add ("&");
binops.Add ("|");
binops.Add ("^");
}
#>
#if OBJCRUNTIME_<#= type.NSName #>
namespace ObjCRuntime
#else
namespace System
#endif
{
[Serializable]
[DebuggerDisplay ("{v,nq}")]
public unsafe struct <#= type.NSName #> : IFormattable, IConvertible, IComparable, IComparable<<#= type.NSName #>>, IEquatable <<#= type.NSName #>>
{
internal <#= type.NSName #> (<#= type.NSName #> v) { this.v = v.v; }
public <#= type.NSName #> (<#= type.CilName32 #> v) { this.v = v; }
#if ARCH_32
public static readonly int Size = 4;
public static readonly <#= type.NSName #> MaxValue = <#= type.CilName32 #>.MaxValue;
public static readonly <#= type.NSName #> MinValue = <#= type.CilName32 #>.MinValue;
<# if (type.NSName == "nfloat") { #>
public static readonly nfloat Epsilon = (nfloat)<#= type.CilName32 #>.Epsilon;
public static readonly nfloat NaN = (nfloat)<#= type.CilName32 #>.NaN;
public static readonly nfloat NegativeInfinity = (nfloat)<#= type.CilName32 #>.NegativeInfinity;
public static readonly nfloat PositiveInfinity = (nfloat)<#= type.CilName32 #>.PositiveInfinity;
<# } #>
[DebuggerBrowsable (DebuggerBrowsableState.Never)]
internal <#= type.CilName32 #> v;
public <#= type.NSName #> (<#= type.CilName64 #> v) { this.v = (<#= type.CilName32 #>)v; }
#else
public static readonly int Size = 8;
public static readonly <#= type.NSName #> MaxValue = (<#= type.NSName #>) <#= type.CilName64 #>.MaxValue; // 64-bit only codepath
public static readonly <#= type.NSName #> MinValue = (<#= type.NSName #>) <#= type.CilName64 #>.MinValue; // 64-bit only codepath
<# if (type.NSName == "nfloat") { #>
public static readonly nfloat Epsilon = (nfloat)<#= type.CilName64 #>.Epsilon;
public static readonly nfloat NaN = (nfloat)<#= type.CilName64 #>.NaN;
public static readonly nfloat NegativeInfinity = (nfloat)<#= type.CilName64 #>.NegativeInfinity;
public static readonly nfloat PositiveInfinity = (nfloat)<#= type.CilName64 #>.PositiveInfinity;
<# } #>
[DebuggerBrowsable (DebuggerBrowsableState.Never)]
internal <#= type.CilName64 #> v;
public <#= type.NSName #> (<#= type.CilName64 #> v) { this.v = v; }
#endif
<#
Action<string, string, string> Conversion = (conversionKind, fromType, toType) => {
// Console.Error.WriteLine ("{0}\t{1}\t{2}", conversionKind, fromType, toType);
WriteLine ("\t\tpublic static {0} operator {1} ({2} v)\n\t\t{{", conversionKind, toType, fromType);
Func<int, string> Cast = arch => {
switch (toType) {
case "nint":
return arch == 32 ? "int" : "long";
case "nuint":
return arch == 32 ? "uint" : "ulong";
case "nfloat":
return arch == 32 ? "float" : "double";
default:
return toType;
}
};
WriteLine ("#if NINT_JIT_OPTIMIZED");
WriteLine ("\t\t\tthrow new NotImplementedException ();");
WriteLine ("#elif ARCH_32");
foreach (var arch in new [] { 32, 64 }) {
Write ("\t\t\treturn ");
var closeParen = false;
switch (toType) {
case "nint":
case "nuint":
case "nfloat":
closeParen = true;
Write ("new {0} (", toType);
break;
}
if (fromType == "IntPtr") {
Write ("*(({0} *)&v)", Cast (arch));
} else if (toType == "IntPtr") {
Write ("*((IntPtr *)&v.v)");
} else {
Write ("({0})", Cast (arch));
switch (fromType) {
case "nint":
case "nuint":
case "nfloat":
Write ("v.v");
break;
default:
Write ("v");
break;
}
}
if (closeParen)
Write (")");
WriteLine (";");
if (arch == 32)
WriteLine ("#else");
}
WriteLine ("#endif");
WriteLine ("\t\t}\n");
};
Action<string, string> Exp = (from, to) => Conversion ("explicit", from, to);
Action<string, string> Imp = (from, to) => Conversion ("implicit", from, to);
switch (type.NSName) {
case "nint":
Exp ("nuint", "nint");
Exp ("nint", "nuint");
Exp ("nfloat", "nint");
Imp ("nint", "nfloat");
Exp ("IntPtr", "nint");
Exp ("nint", "IntPtr");
Imp ("sbyte", "nint");
Exp ("nint", "sbyte");
Imp ("byte", "nint");
Exp ("nint", "byte");
Imp ("char", "nint");
Exp ("nint", "char");
Imp ("short", "nint");
Exp ("nint", "short");
Exp ("ushort", "nint");
Exp ("nint", "ushort");
Imp ("int", "nint");
Exp ("nint", "int");
Exp ("uint", "nint");
Exp ("nint", "uint");
Exp ("long", "nint");
Imp ("nint", "long");
Exp ("ulong", "nint");
Exp ("nint", "ulong");
Exp ("float", "nint");
Imp ("nint", "float");
Exp ("double", "nint");
Imp ("nint", "double");
Exp ("decimal", "nint");
Imp ("nint", "decimal");
break;
case "nuint":
Exp ("nfloat", "nuint");
Imp ("nuint", "nfloat");
Exp ("IntPtr", "nuint");
Exp ("nuint", "IntPtr");
Exp ("sbyte", "nuint");
Exp ("nuint", "sbyte");
Imp ("byte", "nuint");
Exp ("nuint", "byte");
Imp ("char", "nuint");
Exp ("nuint", "char");
Exp ("short", "nuint");
Exp ("nuint", "short");
Imp ("ushort", "nuint");
Exp ("nuint", "ushort");
Exp ("int", "nuint");
Exp ("nuint", "int");
Imp ("uint", "nuint");
Exp ("nuint", "uint");
Exp ("long", "nuint");
Exp ("nuint", "long");
Exp ("ulong", "nuint");
Imp ("nuint", "ulong");
Exp ("float", "nuint");
Imp ("nuint", "float");
Exp ("double", "nuint");
Imp ("nuint", "double");
Exp ("decimal", "nuint");
Imp ("nuint", "decimal");
break;
case "nfloat":
Exp ("IntPtr", "nfloat");
Exp ("nfloat", "IntPtr");
Imp ("sbyte", "nfloat");
Exp ("nfloat", "sbyte");
Imp ("byte", "nfloat");
Exp ("nfloat", "byte");
Imp ("char", "nfloat");
Exp ("nfloat", "char");
Imp ("short", "nfloat");
Exp ("nfloat", "short");
Imp ("ushort", "nfloat");
Exp ("nfloat", "ushort");
Imp ("int", "nfloat");
Exp ("nfloat", "int");
Imp ("uint", "nfloat");
Exp ("nfloat", "uint");
Imp ("long", "nfloat");
Exp ("nfloat", "long");
Imp ("ulong", "nfloat");
Exp ("nfloat", "ulong");
Imp ("float", "nfloat");
Exp ("nfloat", "float");
Exp ("double", "nfloat");
Imp ("nfloat", "double");
Exp ("decimal", "nfloat");
Exp ("nfloat", "decimal");
break;
}
#>
#if NINT_JIT_OPTIMIZED
<# foreach (var op in unops) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> v) { throw new NotImplementedException (); }
<# } #>
#else
<# foreach (var op in unops) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> v) { return new <#= type.NSName #> (<#= op #>v.v); }
<# } #>
#endif
#if NINT_JIT_OPTIMIZED
<# foreach (var op in new [] { '+', '-' }) { #>
public static <#= type.NSName #> operator <#= op #><#= op #> (<#= type.NSName #> v) { throw new NotImplementedException (); }
<# } #>
#else
<# foreach (var op in new [] { '+', '-' }) { #>
public static <#= type.NSName #> operator <#= op #><#= op #> (<#= type.NSName #> v) { return new <#= type.NSName #> (v.v <#= op #> 1); }
<# } #>
#endif
#if NINT_JIT_OPTIMIZED
<# foreach (var op in binops) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> l, <#= type.NSName #> r) { throw new NotImplementedException (); }
<# } #>
<# if (type.IsIntegerType) { #>
<# foreach (var op in new [] { "<<", ">>" }) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> l, int r) { throw new NotImplementedException (); }
<# } } #>
#else
<# foreach (var op in binops) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> l, <#= type.NSName #> r) { return new <#= type.NSName #> (l.v <#= op #> r.v); }
<# } #>
<# if (type.IsIntegerType) { #>
<# foreach (var op in new [] { "<<", ">>" }) { #>
public static <#= type.NSName #> operator <#= op #> (<#= type.NSName #> l, int r) { return new <#= type.NSName #> (l.v <#= op #> r); }
<# } } #>
#endif
#if NINT_JIT_OPTIMIZED
<# foreach (var op in new [] { "==", "!=", "<", ">", "<=", ">=" }) { #>
public static bool operator <#= op.PadRight (2) #> (<#= type.NSName #> l, <#= type.NSName #> r) { throw new NotImplementedException (); }
<# } #>
#else
<# foreach (var op in new [] { "==", "!=", "<", ">", "<=", ">=" }) { #>
public static bool operator <#= op.PadRight (2) #> (<#= type.NSName #> l, <#= type.NSName #> r) { return l.v <#= op #> r.v; }
<# } #>
#endif
public int CompareTo (<#= type.NSName #> value) { return v.CompareTo (value.v); }
public int CompareTo (object value)
{
if (value is <#= type.NSName #>)
return v.CompareTo (((<#= type.NSName #>) value).v);
return v.CompareTo (value);
}
public bool Equals (<#= type.NSName #> obj) { return v.Equals (obj.v); }
public override bool Equals (object obj)
{
if (obj is <#= type.NSName #>)
return v.Equals (((<#= type.NSName #>) obj).v);
return v.Equals (obj);
}
public override int GetHashCode () { return v.GetHashCode (); }
#if ARCH_32
<# foreach (var cilName in new [] { type.CilName32, type.CilName64 }) { #>
<# if (type.NSName == "nfloat") { #>
public static bool IsNaN (nfloat f) { return <#= cilName #>.IsNaN ((<#= cilName #>)f); }
public static bool IsInfinity (nfloat f) { return <#= cilName #>.IsInfinity ((<#= cilName #>)f); }
public static bool IsPositiveInfinity (nfloat f) { return <#= cilName #>.IsPositiveInfinity ((<#= cilName #>)f); }
public static bool IsNegativeInfinity (nfloat f) { return <#= cilName #>.IsNegativeInfinity ((<#= cilName #>)f); }
<# } #>
public static <#= type.NSName #> Parse (string s, IFormatProvider provider) { return (<#= type.NSName #>)<#= cilName #>.Parse (s, provider); }
public static <#= type.NSName #> Parse (string s, NumberStyles style) { return (<#= type.NSName #>)<#= cilName #>.Parse (s, style); }
public static <#= type.NSName #> Parse (string s) { return (<#= type.NSName #>)<#= cilName #>.Parse (s); }
public static <#= type.NSName #> Parse (string s, NumberStyles style, IFormatProvider provider) {
return (<#= type.NSName #>)<#= cilName #>.Parse (s, style, provider);
}
public static bool TryParse (string s, out <#= type.NSName #> result)
{
<#= cilName #> v;
var r = <#= cilName #>.TryParse (s, out v);
result = (<#= type.NSName #>)v;
return r;
}
public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out <#= type.NSName #> result)
{
<#= cilName #> v;
var r = <#= cilName #>.TryParse (s, style, provider, out v);
result = (<#= type.NSName #>)v;
return r;
}
<# if (cilName == type.CilName32) { #>
#else
<# } } #>
#endif
public override string ToString () { return v.ToString (); }
public string ToString (IFormatProvider provider) { return v.ToString (provider); }
public string ToString (string format) { return v.ToString (format); }
public string ToString (string format, IFormatProvider provider) { return v.ToString (format, provider); }
public TypeCode GetTypeCode () { return v.GetTypeCode (); }
bool IConvertible.ToBoolean (IFormatProvider provider) { return ((IConvertible)v).ToBoolean (provider); }
byte IConvertible.ToByte (IFormatProvider provider) { return ((IConvertible)v).ToByte (provider); }
char IConvertible.ToChar (IFormatProvider provider) { return ((IConvertible)v).ToChar (provider); }
DateTime IConvertible.ToDateTime (IFormatProvider provider) { return ((IConvertible)v).ToDateTime (provider); }
decimal IConvertible.ToDecimal (IFormatProvider provider) { return ((IConvertible)v).ToDecimal (provider); }
double IConvertible.ToDouble (IFormatProvider provider) { return ((IConvertible)v).ToDouble (provider); }
short IConvertible.ToInt16 (IFormatProvider provider) { return ((IConvertible)v).ToInt16 (provider); }
int IConvertible.ToInt32 (IFormatProvider provider) { return ((IConvertible)v).ToInt32 (provider); }
long IConvertible.ToInt64 (IFormatProvider provider) { return ((IConvertible)v).ToInt64 (provider); }
sbyte IConvertible.ToSByte (IFormatProvider provider) { return ((IConvertible)v).ToSByte (provider); }
float IConvertible.ToSingle (IFormatProvider provider) { return ((IConvertible)v).ToSingle (provider); }
ushort IConvertible.ToUInt16 (IFormatProvider provider) { return ((IConvertible)v).ToUInt16 (provider); }
uint IConvertible.ToUInt32 (IFormatProvider provider) { return ((IConvertible)v).ToUInt32 (provider); }
ulong IConvertible.ToUInt64 (IFormatProvider provider) { return ((IConvertible)v).ToUInt64 (provider); }
object IConvertible.ToType (Type targetType, IFormatProvider provider) {
return ((IConvertible)v).ToType (targetType, provider);
}
public static void CopyArray (IntPtr source, <#= type.NSName #> [] destination, int startIndex, int length)
{
if (source == IntPtr.Zero)
throw new ArgumentNullException ("source");
if (destination is null)
throw new ArgumentNullException ("destination");
if (destination.Rank != 1)
throw new ArgumentException ("destination", "array is multi-dimensional");
if (startIndex < 0)
throw new ArgumentException ("startIndex", "must be >= 0");
if (length < 0)
throw new ArgumentException ("length", "must be >= 0");
if (startIndex + length > destination.Length)
throw new ArgumentException ("length", "startIndex + length > destination.Length");
for (int i = 0; i < length; i++)
destination [i + startIndex] = (<#= type.NSName #>)Marshal.ReadIntPtr (source, i * <#= type.NSName #>.Size);
}
public static void CopyArray (<#= type.NSName #> [] source, int startIndex, IntPtr destination, int length)
{
if (source is null)
throw new ArgumentNullException ("source");
if (destination == IntPtr.Zero)
throw new ArgumentNullException ("destination");
if (source.Rank != 1)
throw new ArgumentException ("source", "array is multi-dimensional");
if (startIndex < 0)
throw new ArgumentException ("startIndex", "must be >= 0");
if (length < 0)
throw new ArgumentException ("length", "must be >= 0");
if (startIndex + length > source.Length)
throw new ArgumentException ("length", "startIndex + length > source.Length");
for (int i = 0; i < length; i++)
Marshal.WriteIntPtr (destination, i * <#= type.NSName #>.Size, (IntPtr)source [i + startIndex]);
}
}
}
<# } #>