質問

どうやら、x86(およびおそらく他の多くの命令セット)は、商と残りの除算操作の両方を別々のレジスタに置いたようです。

これで、おそらくこのようなコードを最適化するコンパイラを信頼することができます。

( x / 6 )
( x % 6 )

そして、彼らはおそらくそうします。それでも、何かをしてください 言語 (またはライブラリですが、主に言語を探しています)サポートは、dividとModuloの両方の結果を同時に提供しますか?もしそうなら、彼らは何ですか、そして構文はどのように見えますか?

役に立ちましたか?

解決

Cがあります divldiv. 。これらが商と残りのために個別の指示を生成するかどうかは、特定の標準ライブラリの実装とコンパイラ、最適化設定に依存します。 C99から始めて、あなたも持っています lldiv より大きな数の場合。

他のヒント

Pythonはそうします。

>>> divmod(9, 4)
(2, 1)

Pythonが非常に高レベルの言語であるため、奇妙です。

Rubyもそうです:

11.divmod(3) #=> [3, 2]

* 編集 *

これらのオペレーターの目的は、おそらく可能な限り効率的に作業を行うことではなく、正確性/移植性の理由で機能が存在する可能性が高いことに注意する必要があります。

興味のある方のために、私は信じています これがコードです 整数divmodのPython実装の:

static enum divmod_result
i_divmod(register long x, register long y,
     long *p_xdivy, long *p_xmody)
{
long xdivy, xmody;

if (y == 0) {
    PyErr_SetString(PyExc_ZeroDivisionError,
                    "integer division or modulo by zero");
    return DIVMOD_ERROR;
}
/* (-sys.maxint-1)/-1 is the only overflow case. */
if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
    return DIVMOD_OVERFLOW;
xdivy = x / y;
/* xdiv*y can overflow on platforms where x/y gives floor(x/y)
 * for x and y with differing signs. (This is unusual
 * behaviour, and C99 prohibits it, but it's allowed by C89;
 * for an example of overflow, take x = LONG_MIN, y = 5 or x =
 * LONG_MAX, y = -5.)  However, x - xdivy*y is always
 * representable as a long, since it lies strictly between
 * -abs(y) and abs(y).  We add casts to avoid intermediate
 * overflow.
 */
xmody = (long)(x - (unsigned long)xdivy * y);
/* If the signs of x and y differ, and the remainder is non-0,
 * C89 doesn't define whether xdivy is now the floor or the
 * ceiling of the infinitely precise quotient.  We want the floor,
 * and we have it iff the remainder's sign matches y's.
 */
if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
    xmody += y;
    --xdivy;
    assert(xmody && ((y ^ xmody) >= 0));
}
*p_xdivy = xdivy;
*p_xmody = xmody;
return DIVMOD_OK;
}

C#/。ネットでは、あなたは持っています Math.DivRem: http://msdn.microsoft.com/en-us/library/system.math.divrem.aspx

しかし、によれば このスレッド これはそれほど最適化ではありません。

一般的なLISPは次のとおりです。 http://www.lispworks.com/documentation/hyperspec/body/f_floorc.htm

ストリンガーベルが述べたように DivRem どれの 最適化されていません .net 3.5まで。

.NET 4.0 ngenを使用します.

私が得た結果 Math.DivRem (デバッグ;リリース= 〜1100m)

11863
11820
11881
11859
11854

結果を得ました MyDivRem (デバッグ;リリース= 〜1100m)

29177
29214
29472
29277
29196

x86を対象としたプロジェクト。


Math.DivRem 使用例

int mod1;
int div1 = Math.DivRem(4, 2, out mod1);

メソッドシグネチャ

DivRem(Int32, Int32, Int32&) : Int32
DivRem(Int64, Int64, Int64&) : Int64

.NET 4.0コード

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static int DivRem(int a, int b, out int result)
{
    result = a % b;
    return (a / b);
}

.NET 4.0 IL

.custom instance void System.Runtime.TargetedPatchingOptOutAttribute::.ctor(string) = { string('Performance critical to inline across NGen image boundaries') }
.maxstack 8
L_0000: ldarg.2 
L_0001: ldarg.0 
L_0002: ldarg.1 
L_0003: rem 
L_0004: stind.i4 
L_0005: ldarg.0 
L_0006: ldarg.1 
L_0007: div 
L_0008: ret 

MSDNリファレンス

.NETフレームワークにはあります Math.DivRem:

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

それでも、 DivRem このようなものの周りのラッパーです:

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

(ジッターがそのようなことを単一の指示に最適化できるかどうかはわかりません。)

FWIW、Haskellには両方があります divModquotRem 後者は機械命令に直接対応しています(によると 積分演算子quot vs. div) その間 divMod ではないかもしれない。

    int result,rest;
    _asm
    {
        xor edx, edx // pone edx a cero; edx = 0
        mov eax, result// eax = 2AF0
        mov ecx, radix // ecx = 4
        div ecx
        mov val, eax
        mov rest, edx
    }

これにより、結果が残りを返します

        int result,rest;
    _asm
    {
        xor edx, edx // pone edx a cero; edx = 0
        mov eax, result// eax = 2AF0
        mov ecx, radix // ecx = 4
        div ecx
        mov val, eax
        mov rest, edx
    }

Javaでクラス BigDecimal 操作があります divideAndRemainder 結果と部門の残りの部分で2つの要素の配列を返します。

BigDecimal bDecimal = ...
BigDecimal[] result = bDecimal.divideAndRemainder(new BigDecimal(60));

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/math/bigdecimal.html#divideandremainder(java.math.bigdecimal)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top