1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc. 2 Contributed by Mentor Graphics, Inc. 3 4 This file is free software; you can redistribute it and/or modify it 5 under the terms of the GNU General Public License as published by the 6 Free Software Foundation; either version 3, or (at your option) any 7 later version. 8 9 This file is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 Under Section 7 of GPL version 3, you are granted additional 15 permissions described in the GCC Runtime Library Exception, version 16 3.1, as published by the Free Software Foundation. 17 18 You should have received a copy of the GNU General Public License and 19 a copy of the GCC Runtime Library Exception along with this program; 20 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 21 <http://www.gnu.org/licenses/>. */ 22 23 #include "lib2-gcn.h" 24 25 /* 64-bit SI divide and modulo as used in gcn. */ 26 27 union pack { 28 UTItype ti; 29 struct {DItype quot, rem;} pair; 30 }; 31 union upack { 32 UTItype ti; 33 struct {UDItype quot, rem;} pair; 34 }; 35 36 UTItype 37 __udivmoddi4 (UDItype num, UDItype den) 38 { 39 UDItype bit = 1; 40 union upack res = {0}; 41 42 while (den < num && bit && !(den & (1L<<63))) 43 { 44 den <<=1; 45 bit <<=1; 46 } 47 while (bit) 48 { 49 if (num >= den) 50 { 51 num -= den; 52 res.pair.quot |= bit; 53 } 54 bit >>=1; 55 den >>=1; 56 } 57 res.pair.rem = num; 58 return res.ti; 59 } 60 61 UTItype 62 __divmoddi4 (DItype a, DItype b) 63 { 64 word_type nega = 0, negb = 0; 65 union pack res; 66 67 if (a < 0) 68 { 69 a = -a; 70 nega = 1; 71 } 72 73 if (b < 0) 74 { 75 b = -b; 76 negb = 1; 77 } 78 79 res.ti = __udivmoddi4 (a, b); 80 81 if (nega) 82 res.pair.rem = -res.pair.rem; 83 if (nega ^ negb) 84 res.pair.quot = -res.pair.quot; 85 86 return res.ti; 87 } 88 89 90 DItype 91 __divdi3 (DItype a, DItype b) 92 { 93 union pack u; 94 u.ti = __divmoddi4 (a, b); 95 return u.pair.quot; 96 } 97 98 DItype 99 __moddi3 (DItype a, DItype b) 100 { 101 union pack u; 102 u.ti = __divmoddi4 (a, b); 103 return u.pair.rem; 104 } 105 106 107 UDItype 108 __udivdi3 (UDItype a, UDItype b) 109 { 110 union pack u; 111 u.ti = __udivmoddi4 (a, b); 112 return u.pair.quot; 113 } 114 115 UDItype 116 __umoddi3 (UDItype a, UDItype b) 117 { 118 union pack u; 119 u.ti = __udivmoddi4 (a, b); 120 return u.pair.rem; 121 } 122 123