1 /* Copyright (C) 2012-2024 Free Software Foundation, Inc. 2 Contributed by Altera and 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 /* 32-bit SI divide and modulo as used in gcn. */ 26 27 union pack { 28 UDItype di; 29 struct {SItype quot, rem;} pair; 30 }; 31 union upack { 32 UDItype di; 33 struct {USItype quot, rem;} pair; 34 }; 35 36 UDItype 37 __udivmodsi4 (USItype num, USItype den) 38 { 39 USItype bit = 1; 40 union upack res = {0}; 41 42 while (den < num && bit && !(den & (1L<<31))) 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.di; 59 } 60 61 UDItype 62 __divmodsi4 (SItype a, SItype 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.di = __udivmodsi4 (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.di; 87 } 88 89 90 SItype 91 __divsi3 (SItype a, SItype b) 92 { 93 union pack u; 94 u.di = __divmodsi4 (a, b); 95 return u.pair.quot; 96 } 97 98 SItype 99 __modsi3 (SItype a, SItype b) 100 { 101 union pack u; 102 u.di = __divmodsi4 (a, b); 103 return u.pair.rem; 104 } 105 106 107 USItype 108 __udivsi3 (USItype a, USItype b) 109 { 110 union pack u; 111 u.di = __udivmodsi4 (a, b); 112 return u.pair.quot; 113 } 114 115 116 USItype 117 __umodsi3 (USItype a, USItype b) 118 { 119 union pack u; 120 u.di = __udivmodsi4 (a, b); 121 return u.pair.rem; 122 } 123 124