Divide and Get Remainder at the same time?

后端 未结 9 1175
不知归路
不知归路 2020-12-15 02:37

Apparently, x86 (and probably a lot of other instruction sets) put both the quotient and the remainder of a divide operation in separate registers.

Now, we can proba

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 03:03

    The .NET framework has Math.DivRem:

    int mod, div = Math.DivRem(11, 3, out mod);
    // mod = 2, div = 3
    

    Although, DivRem is just a wrapper around something like this:

    int div = x / y;
    int mod = x % y;
    

    (I have no idea whether or not the jitter can/does optimise that sort of thing into a single instruction.)

提交回复
热议问题