| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | // |
| | | 6 | | // This code is copied almost verbatim from the same-named file in CoreRT with mechanical changes to make it build outsi |
| | | 7 | | // |
| | | 8 | | |
| | | 9 | | namespace System |
| | | 10 | | { |
| | | 11 | | internal static class DecimalDecCalc |
| | | 12 | | { |
| | | 13 | | private static uint D32DivMod1E9(uint hi32, ref uint lo32) |
| | 1 | 14 | | { |
| | 1 | 15 | | ulong n = (ulong)hi32 << 32 | lo32; |
| | 1 | 16 | | lo32 = (uint)(n / 1000000000); |
| | 1 | 17 | | return (uint)(n % 1000000000); |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | // Performs the equivalent of: |
| | | 21 | | // |
| | | 22 | | // uint modulo = value % 1e9; |
| | | 23 | | // value = value / 1e9; |
| | | 24 | | // return modulo; |
| | | 25 | | // |
| | | 26 | | internal static uint DecDivMod1E9(ref MutableDecimal value) |
| | 1 | 27 | | { |
| | 1 | 28 | | return D32DivMod1E9(D32DivMod1E9(D32DivMod1E9(0, ref value.High), ref value.Mid), ref value.Low); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static void DecAddInt32(ref MutableDecimal value, uint i) |
| | 1 | 32 | | { |
| | 1 | 33 | | if (D32AddCarry(ref value.Low, i)) |
| | 1 | 34 | | { |
| | 1 | 35 | | if (D32AddCarry(ref value.Mid, 1)) |
| | 1 | 36 | | { |
| | 1 | 37 | | D32AddCarry(ref value.High, 1); |
| | 1 | 38 | | } |
| | 1 | 39 | | } |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | private static bool D32AddCarry(ref uint value, uint i) |
| | 1 | 43 | | { |
| | 1 | 44 | | uint v = value; |
| | 1 | 45 | | uint sum = v + i; |
| | 1 | 46 | | value = sum; |
| | 1 | 47 | | return (sum < v) || (sum < i); |
| | 1 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal static void DecMul10(ref MutableDecimal value) |
| | 1 | 51 | | { |
| | 1 | 52 | | MutableDecimal d = value; |
| | 1 | 53 | | DecShiftLeft(ref value); |
| | 1 | 54 | | DecShiftLeft(ref value); |
| | 1 | 55 | | DecAdd(ref value, d); |
| | 1 | 56 | | DecShiftLeft(ref value); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static void DecShiftLeft(ref MutableDecimal value) |
| | 1 | 60 | | { |
| | 1 | 61 | | uint c0 = (value.Low & 0x80000000) != 0 ? 1u : 0u; |
| | 1 | 62 | | uint c1 = (value.Mid & 0x80000000) != 0 ? 1u : 0u; |
| | 1 | 63 | | value.Low = value.Low << 1; |
| | 1 | 64 | | value.Mid = (value.Mid << 1) | c0; |
| | 1 | 65 | | value.High = (value.High << 1) | c1; |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | private static void DecAdd(ref MutableDecimal value, MutableDecimal d) |
| | 1 | 69 | | { |
| | 1 | 70 | | if (D32AddCarry(ref value.Low, d.Low)) |
| | 1 | 71 | | { |
| | 1 | 72 | | if (D32AddCarry(ref value.Mid, 1)) |
| | 1 | 73 | | { |
| | 1 | 74 | | D32AddCarry(ref value.High, 1); |
| | 1 | 75 | | } |
| | 1 | 76 | | } |
| | | 77 | | |
| | 1 | 78 | | if (D32AddCarry(ref value.Mid, d.Mid)) |
| | 1 | 79 | | { |
| | 1 | 80 | | D32AddCarry(ref value.High, 1); |
| | 1 | 81 | | } |
| | | 82 | | |
| | 1 | 83 | | D32AddCarry(ref value.High, d.High); |
| | 1 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |