This is the mail archive of the
mailing list for the GCC project.
Re: Bug in divmodhi4(), plus poor inperformant code
- From: Paul Koning <paulkoning at comcast dot net>
- To: Stefan Kanthak <stefan dot kanthak at nexgo dot de>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 4 Dec 2018 19:32:46 -0500
- Subject: Re: Bug in divmodhi4(), plus poor inperformant code
- References: <08C1AE242031402FA1206D72CB9AD084@W340>
Yes, that's a rather nasty cut & paste error I made.
But if the 31 is changed to a 15, is the code correct? I would think so. For optimization I'd think that an assembly language version would make more sense, and a few targets do that.
> On Dec 4, 2018, at 5:51 PM, Stefan Kanthak <firstname.lastname@example.org> wrote:
> Hi @ll,
> libgcc's divmodhi4() function has an obvious bug; additionally
> it shows rather poor inperformant code: two of the three conditions
> tested in the first loop should clearly moved outside the loop!
> divmodsi4() shows this inperformant code too!
> Stefan Kanthak
> --- divmodhi4.c ---
> unsigned short
> __udivmodhi4(unsigned short num, unsigned short den, int modwanted)
> unsigned short bit = 1;
> unsigned short res = 0;
> #ifdef BUGFIX
> if (den > num)
> return modwanted ? num : 0;
> if (den == num)
> return modwanted ? 0 : 1;
> while ((signed short) den >= 0)
> #else // original, buggy and inperformant code
> while (den < num && bit && !(den & (1L<<31))) // unsigned shorts are 16 bit!
> den <<=1;
> bit <<=1;
> while (bit)
> if (num >= den)
> num -= den;
> res |= bit;
> bit >>=1;
> den >>=1;
> if (modwanted) return num;
> return res;