A faster signed remainder when divisor is power of 2

Bug #1817452 reported by Dan Lewis
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
New
Undecided
Unassigned

Bug Description

The following C code:

int Rem8(int n)
    {
    return n % 8 ;
    }

is recognized as a special case (divisor is power of 2) and compiled by gcc version 8.2 with -O3 into:

Rem8: rsbs r3, r0, #0
        and r3, r3, #7
        and r0, r0, #7
        it pl
        rsbpl r0, r3, #0
        bx lr

However, I believe there's a simpler and faster solution (see below). Computing the remainder for other powers of 2 is simply a matter of changing the constant 7 to 2^k-1 and the constant 3 to k.

Rem8: LSRS R1,R0,31
  ANDS R0,R0,7
  IT NE
  SUBNE R0,R0,R1,LSL 3
  BX LR

Dan Lewis (danielwlewis)
description: updated
description: updated
description: updated
description: updated
description: updated
description: updated
Dan Lewis (danielwlewis)
description: updated
description: updated
description: updated
description: updated
Dan Lewis (danielwlewis)
description: updated
Dan Lewis (danielwlewis)
description: updated
Dan Lewis (danielwlewis)
description: updated
Dan Lewis (danielwlewis)
description: updated
Dan Lewis (danielwlewis)
description: updated
description: updated
Dan Lewis (danielwlewis)
description: updated
Dan Lewis (danielwlewis)
description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.