1/* Copyright (C) 2012-2022 Free Software Foundation, Inc. 2 Contributed by Altera and Mentor Graphics, Inc. 3 4This file is free software; you can redistribute it and/or modify it 5under the terms of the GNU General Public License as published by the 6Free Software Foundation; either version 3, or (at your option) any 7later version. 8 9This file is distributed in the hope that it will be useful, but 10WITHOUT ANY WARRANTY; without even the implied warranty of 11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12General Public License for more details. 13 14Under Section 7 of GPL version 3, you are granted additional 15permissions described in the GCC Runtime Library Exception, version 163.1, as published by the Free Software Foundation. 17 18You should have received a copy of the GNU General Public License and 19a copy of the GCC Runtime Library Exception along with this program; 20see 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 27static USItype 28udivmodsi4 (USItype num, USItype den, word_type modwanted) 29{ 30 USItype bit = 1; 31 USItype res = 0; 32 33 while (den < num && bit && !(den & (1L<<31))) 34 { 35 den <<=1; 36 bit <<=1; 37 } 38 while (bit) 39 { 40 if (num >= den) 41 { 42 num -= den; 43 res |= bit; 44 } 45 bit >>=1; 46 den >>=1; 47 } 48 if (modwanted) 49 return num; 50 return res; 51} 52 53 54SItype 55__divsi3 (SItype a, SItype b) 56{ 57 word_type neg = 0; 58 SItype res; 59 60 if (a < 0) 61 { 62 a = -a; 63 neg = !neg; 64 } 65 66 if (b < 0) 67 { 68 b = -b; 69 neg = !neg; 70 } 71 72 res = udivmodsi4 (a, b, 0); 73 74 if (neg) 75 res = -res; 76 77 return res; 78} 79 80 81SItype 82__modsi3 (SItype a, SItype b) 83{ 84 word_type neg = 0; 85 SItype res; 86 87 if (a < 0) 88 { 89 a = -a; 90 neg = 1; 91 } 92 93 if (b < 0) 94 b = -b; 95 96 res = udivmodsi4 (a, b, 1); 97 98 if (neg) 99 res = -res; 100 101 return res; 102} 103 104 105USItype 106__udivsi3 (USItype a, USItype b) 107{ 108 return udivmodsi4 (a, b, 0); 109} 110 111 112USItype 113__umodsi3 (USItype a, USItype b) 114{ 115 return udivmodsi4 (a, b, 1); 116} 117 118