Assembly mod algorithm on processor with no division operator

前端 未结 8 1890
挽巷
挽巷 2020-12-09 10:57

I need to implement a simple macro that finds the modulo of two numbers on a processor that doesn\'t have a division operator (think ARM). I could use division by repeated s

相关标签:
8条回答
  • 2020-12-09 11:46

    Seems like subtracting (or adding if a is negative) by b until you hit or cross 0 would be an easy implementation albeit almost certainly not the most efficient.

    0 讨论(0)
  • 2020-12-09 11:51

    This doesn't answer your question directly but is an interesting case nonetheless. If the number is being modulo'd by a power of two the operation can be performed as

    x % 2^n = x & (2^n - 1)
    

    Which uses a single AND operation, which usually is a one or two cycle operation.

    More information At Wikipedia

    0 讨论(0)
提交回复
热议问题