Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MathHelpers.cs « CompilerHelpers « Runtime « Internal « src « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6947cc9f303fbfecf3a7013c1e58bea73c50d361 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime;
using System.Runtime.CompilerServices;

using Internal.Runtime;

namespace Internal.Runtime.CompilerHelpers
{
    /// <summary>
    /// Math helpers for generated code. The helpers marked with [RuntimeExport] and the type
    /// itself need to be public because they constitute a public contract with the .NET Native toolchain.
    /// </summary>
    [CLSCompliant(false)]
    public static class MathHelpers
    {
#if !BIT64
        //
        // 64-bit checked multiplication for 32-bit platforms
        //

        // Helper to multiply two 32-bit uints
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static ulong Mul32x32To64(uint a, uint b)
        {
            return a * (ulong)b;
        }

        // Helper to get high 32-bit of 64-bit int
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static uint Hi32Bits(long a)
        {
            return (uint)(a >> 32);
        }

        // Helper to get high 32-bit of 64-bit int
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static uint Hi32Bits(ulong a)
        {
            return (uint)(a >> 32);
        }

        [RuntimeExport("LMulOvf")]
        public static long LMulOvf(long i, long j)
        {
            long ret;

            // Remember the sign of the result
            int sign = (int)(Hi32Bits(i) ^ Hi32Bits(j));

            // Convert to unsigned multiplication
            if (i < 0) i = -i;
            if (j < 0) j = -j;

            // Get the upper 32 bits of the numbers
            uint val1High = Hi32Bits(i);
            uint val2High = Hi32Bits(j);

            ulong valMid;

            if (val1High == 0)
            {
                // Compute the 'middle' bits of the long multiplication
                valMid = Mul32x32To64(val2High, (uint)i);
            }
            else
            {
                if (val2High != 0)
                    goto ThrowExcep;
                // Compute the 'middle' bits of the long multiplication
                valMid = Mul32x32To64(val1High, (uint)j);
            }

            // See if any bits after bit 32 are set
            if (Hi32Bits(valMid) != 0)
                goto ThrowExcep;

            ret = (long)(Mul32x32To64((uint)i, (uint)j) + (valMid << 32));

            // check for overflow
            if (Hi32Bits(ret) < (uint)valMid)
                goto ThrowExcep;

            if (sign >= 0)
            {
                // have we spilled into the sign bit?
                if (ret < 0)
                    goto ThrowExcep;
            }
            else
            {
                ret = -ret;
                // have we spilled into the sign bit?
                if (ret > 0)
                    goto ThrowExcep;
            }
            return ret;

        ThrowExcep:
            return ThrowLngOvf();
        }

        [RuntimeExport("ULMulOvf")]
        public static ulong ULMulOvf(ulong i, ulong j)
        {
            ulong ret;

            // Get the upper 32 bits of the numbers
            uint val1High = Hi32Bits(i);
            uint val2High = Hi32Bits(j);

            ulong valMid;

            if (val1High == 0)
            {
                if (val2High == 0)
                    return Mul32x32To64((uint)i, (uint)j);
                // Compute the 'middle' bits of the long multiplication
                valMid = Mul32x32To64(val2High, (uint)i);
            }
            else
            {
                if (val2High != 0)
                    goto ThrowExcep;
                // Compute the 'middle' bits of the long multiplication
                valMid = Mul32x32To64(val1High, (uint)j);
            }

            // See if any bits after bit 32 are set
            if (Hi32Bits(valMid) != 0)
                goto ThrowExcep;

            ret = Mul32x32To64((uint)i, (uint)j) + (valMid << 32);

            // check for overflow
            if (Hi32Bits(ret) < (uint)valMid)
                goto ThrowExcep;
            return ret;

        ThrowExcep:
            return ThrowULngOvf();
        }
#endif // BIT64

        [RuntimeExport("Dbl2IntOvf")]
        public static int Dbl2IntOvf(double val)
        {
            const double two31 = 2147483648.0;

            // Note that this expression also works properly for val = NaN case
            if (val > -two31 - 1 && val < two31)
                return unchecked((int)val);

            return ThrowIntOvf();
        }

        [RuntimeExport("Dbl2UIntOvf")]
        public static uint Dbl2UIntOvf(double val)
        {
            // Note that this expression also works properly for val = NaN case
            if (val > -1.0 && val < 4294967296.0)
                return unchecked((uint)val);

            return ThrowUIntOvf();
        }

        [RuntimeExport("Dbl2LngOvf")]
        public static long Dbl2LngOvf(double val)
        {
            const double two63 = 2147483648.0 * 4294967296.0;

            // Note that this expression also works properly for val = NaN case
            // We need to compare with the very next double to two63. 0x402 is epsilon to get us there.
            if (val > -two63 - 0x402 && val < two63)
                return unchecked((long)val);

            return ThrowLngOvf();
        }

        [RuntimeExport("Dbl2ULngOvf")]
        public static ulong Dbl2ULngOvf(double val)
        {
            const double two64 = 2.0 * 2147483648.0 * 4294967296.0;

            // Note that this expression also works properly for val = NaN case
            if (val < two64)
                return unchecked((ulong)val);

            return ThrowULngOvf();
        }

        [RuntimeExport("Flt2IntOvf")]
        public static int Flt2IntOvf(float val)
        {
            const double two31 = 2147483648.0;

            // Note that this expression also works properly for val = NaN case
            if (val > -two31 - 1 && val < two31)
                return ((int)val);

            return ThrowIntOvf();
        }

        [RuntimeExport("Flt2LngOvf")]
        public static long Flt2LngOvf(float val)
        {
            const double two63 = 2147483648.0 * 4294967296.0;

            // Note that this expression also works properly for val = NaN case
            // We need to compare with the very next double to two63. 0x402 is epsilon to get us there.
            if (val > -two63 - 0x402 && val < two63)
                return ((long)val);

            return ThrowIntOvf();
        }

#if ARM
        private const string RuntimeLibrary = "[MRT]";

        [RuntimeImport(RuntimeLibrary, "RhpIDiv")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern int RhpIDiv(int i, int j);

        public static int IDiv(int i, int j)
        {
            if (j == 0)
                return ThrowIntDivByZero();
            else if (j == -1 && i == int.MinValue)
                return ThrowIntArithExc();
            else
                return RhpIDiv(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpUDiv")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern uint RhpUDiv(uint i, uint j);

        public static long UDiv(uint i, uint j)
        {
            if (j == 0)
                return ThrowUIntDivByZero();
            else
                return RhpUDiv(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpULDiv")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern ulong RhpULDiv(ulong i, ulong j);

        public static ulong ULDiv(ulong i, ulong j)
        {
            if (j == 0)
                return ThrowULngDivByZero();
            else
                return RhpULDiv(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpLDiv")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern long RhpLDiv(long i, long j);

        public static long LDiv(long i, long j)
        {
            if (j == 0)
                return ThrowLngDivByZero();
            else if (j == -1 && i == long.MinValue)
                return ThrowLngArithExc();
            else
                return RhpLDiv(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpIMod")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern int RhpIMod(int i, int j);

        public static int IMod(int i, int j)
        {
            if (j == 0)
                return ThrowIntDivByZero();
            else
                return RhpIMod(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpUMod")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern uint RhpUMod(uint i, uint j);

        public static long UMod(uint i, uint j)
        {
            if (j == 0)
                return ThrowUIntDivByZero();
            else
                return RhpUMod(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpULMod")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern ulong RhpULMod(ulong i, ulong j);

        public static ulong ULMod(ulong i, ulong j)
        {
            if (j == 0)
                return ThrowULngDivByZero();
            else
                return RhpULMod(i, j);
        }

        [RuntimeImport(RuntimeLibrary, "RhpLMod")]
        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern long RhpLMod(long i, long j);

        public static long LMod(long i, long j)
        {
            if (j == 0)
                return ThrowLngDivByZero();
            else
                return RhpLMod(i, j);
        }
#endif // ARM

        //
        // Matching return types of throw helpers enables tailcalling them. It improves performance 
        // of the hot path because of it does not need to raise full stackframe.
        //

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static int ThrowIntOvf()
        {
            throw new OverflowException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static uint ThrowUIntOvf()
        {
            throw new OverflowException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static long ThrowLngOvf()
        {
            throw new OverflowException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static ulong ThrowULngOvf()
        {
            throw new OverflowException();
        }

#if ARM
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static int ThrowIntDivByZero()
        {
            throw new DivideByZeroException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static uint ThrowUIntDivByZero()
        {
            throw new DivideByZeroException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static long ThrowLngDivByZero()
        {
            throw new DivideByZeroException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static ulong ThrowULngDivByZero()
        {
            throw new DivideByZeroException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static int ThrowIntArithExc()
        {
            throw new ArithmeticException();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static long ThrowLngArithExc()
        {
            throw new ArithmeticException();
        }
#endif // ARM
    }
}