-/* Copyright (C) 2012-2019 Free Software Foundation, Inc.
+/* Copyright (C) 2012-2024 Free Software Foundation, Inc.
Contributed by Altera and Mentor Graphics, Inc.
This file is free software; you can redistribute it and/or modify it
/* 32-bit SI divide and modulo as used in gcn. */
-static USItype
-udivmodsi4 (USItype num, USItype den, word_type modwanted)
+union pack {
+ UDItype di;
+ struct {SItype quot, rem;} pair;
+};
+union upack {
+ UDItype di;
+ struct {USItype quot, rem;} pair;
+};
+
+UDItype
+__udivmodsi4 (USItype num, USItype den)
{
USItype bit = 1;
- USItype res = 0;
+ union upack res = {0};
while (den < num && bit && !(den & (1L<<31)))
{
if (num >= den)
{
num -= den;
- res |= bit;
+ res.pair.quot |= bit;
}
bit >>=1;
den >>=1;
}
- if (modwanted)
- return num;
- return res;
+ res.pair.rem = num;
+ return res.di;
}
-
-SItype
-__divsi3 (SItype a, SItype b)
+UDItype
+__divmodsi4 (SItype a, SItype b)
{
- word_type neg = 0;
- SItype res;
+ word_type nega = 0, negb = 0;
+ union pack res;
if (a < 0)
{
a = -a;
- neg = !neg;
+ nega = 1;
}
if (b < 0)
{
b = -b;
- neg = !neg;
+ negb = 1;
}
- res = udivmodsi4 (a, b, 0);
+ res.di = __udivmodsi4 (a, b);
- if (neg)
- res = -res;
+ if (nega)
+ res.pair.rem = -res.pair.rem;
+ if (nega ^ negb)
+ res.pair.quot = -res.pair.quot;
- return res;
+ return res.di;
}
SItype
-__modsi3 (SItype a, SItype b)
+__divsi3 (SItype a, SItype b)
{
- word_type neg = 0;
- SItype res;
-
- if (a < 0)
- {
- a = -a;
- neg = 1;
- }
-
- if (b < 0)
- b = -b;
-
- res = udivmodsi4 (a, b, 1);
-
- if (neg)
- res = -res;
+ union pack u;
+ u.di = __divmodsi4 (a, b);
+ return u.pair.quot;
+}
- return res;
+SItype
+__modsi3 (SItype a, SItype b)
+{
+ union pack u;
+ u.di = __divmodsi4 (a, b);
+ return u.pair.rem;
}
-SItype
-__udivsi3 (SItype a, SItype b)
+USItype
+__udivsi3 (USItype a, USItype b)
{
- return udivmodsi4 (a, b, 0);
+ union pack u;
+ u.di = __udivmodsi4 (a, b);
+ return u.pair.quot;
}
-SItype
-__umodsi3 (SItype a, SItype b)
+USItype
+__umodsi3 (USItype a, USItype b)
{
- return udivmodsi4 (a, b, 1);
+ union pack u;
+ u.di = __udivmodsi4 (a, b);
+ return u.pair.rem;
}