0

固定小数点数用に既存のライブラリを改造しようとしています。現在、ライブラリは 32​​ ビットの符号付き整数で動作する名前空間関数です。私はこれを好転させ、整数をラップする固定小数点クラスを作成したいと考えていますが、パフォーマンスはユースケースの問題であるため、クラスに関連するパフォーマンスのペナルティをこのような細かいものに支払いたくありません。

将来のクラスには非常に単純なデータ要件があり、リソースがないため、クラスを「値指向」にして、非変更操​​作を活用し、妥当な場合はインスタンスを値で渡すことが可能であると考えました。これは、実装された場合、階層の一部ではなく単純なクラスになります。

生の整数を使用する場合と比較して、実際のパフォーマンスの低下が発生しないように、整数ラッパー クラスを作成できるかどうか疑問に思っています。私はこれが事実であるとほぼ確信していますが、コンパイルプロセスについて十分に知っているわけではありません。

stl イテレータは単純なポインタ操作にコンパイルされ、整数操作でのみ同様のことをしたいと言っていることを私は知っています。

いずれにせよ、ライブラリはプロジェクトの一部として c++11 に更新されるので、少なくとも constexpr と右辺値参照などの他の新機能を使用して、このクラスのパフォーマンスを純粋な整数のパフォーマンスに近づけることができることを願っています。オペレーション。

さらに、2 つの実装間のパフォーマンスの違いをベンチマークするための推奨事項をいただければ幸いです。

4

2 に答える 2

3

この質問で面白いのは、それがコンパイラに依存しているということです。Clang/LLVM の使用:

#include <iostream>
using namespace std;

inline int foo(int a) { return a << 1; }

struct Bar
{
    int a;

    Bar(int x) : a(x) {}

    Bar baz() { return a << 1; }
};

void out(int x) __attribute__ ((noinline));
void out(int x) { cout << x; }

void out(Bar x) __attribute__ ((noinline));
void out(Bar x) { cout << x.a; }

void f1(int x) __attribute ((noinline));
void f1(int x) { out(foo(x)); }

void f2(Bar b) __attribute ((noinline));
void f2(Bar b) { out(b.baz()); }

int main(int argc, char** argv)
{
    f1(argc);
    f2(argc);
}

次の IRを提供します。

define void @_Z3outi(i32 %x) uwtable noinline {
  %1 = tail call %"class.std::basic_ostream"*
                 @_ZNSolsEi(%"class.std::basic_ostream"* @_ZSt4cout, i32 %x)
  ret void
}

define void @_Z3out3Bar(i32 %x.coerce) uwtable noinline {
  %1 = tail call %"class.std::basic_ostream"*
                 @_ZNSolsEi(%"class.std::basic_ostream"* @_ZSt4cout, i32 %x.coerce)
  ret void
}

define void @_Z2f1i(i32 %x) uwtable noinline {
  %1 = shl i32 %x, 1
  tail call void @_Z3outi(i32 %1)
  ret void
}

define void @_Z2f23Bar(i32 %b.coerce) uwtable noinline {
  %1 = shl i32 %b.coerce, 1
  tail call void @_Z3out3Bar(i32 %1)
  ret void
}

当然のことながら、生成されたアセンブリはまったく同じです。

    .globl  _Z2f1i
    .align  16, 0x90
    .type   _Z2f1i,@function
_Z2f1i:                                 # @_Z2f1i
.Ltmp6:
    .cfi_startproc
# BB#0:
    addl    %edi, %edi
    jmp _Z3outi                 # TAILCALL
.Ltmp7:
    .size   _Z2f1i, .Ltmp7-_Z2f1i
.Ltmp8:
    .cfi_endproc
.Leh_func_end2:


    .globl  _Z2f23Bar
    .align  16, 0x90
    .type   _Z2f23Bar,@function
_Z2f23Bar:                              # @_Z2f23Bar
.Ltmp9:
    .cfi_startproc
# BB#0:
    addl    %edi, %edi
    jmp _Z3out3Bar              # TAILCALL
.Ltmp10:
    .size   _Z2f23Bar, .Ltmp10-_Z2f23Bar
.Ltmp11:
    .cfi_endproc
.Leh_func_end3:

通常、クラスのメソッドがインライン化されている限り、thisパラメーターと参照は簡単に省略できます。gccがこれを台無しにする方法がよくわかりません。

于 2012-03-12T09:17:48.517 に答える
1

値セマンティクスを使用して固定小数点演算を実装すると、パフォーマンスが低下します。理由は...

#include <iostream>
using namespace std;

inline int foo(int a) { return a << 1; }

struct Bar
{
    int a;

    Bar(int x) : a(x) {}

    Bar baz() { return a << 1; }
};

void out(int x) __attribute__ ((noinline));
void out(int x) { cout << x; }

void out(Bar x) __attribute__ ((noinline));
void out(Bar x) { cout << x.a; }

void f1(int x) __attribute ((noinline));
void f1(int x) { out(foo(x)); }

void f2(Bar b) __attribute ((noinline));
void f2(Bar b) { out(b.baz()); }

int main(int argc, char** argv)
{
    f1(argc);
    f2(argc);
}

それでは、f1 と f2 の逆アセンブルを見てみましょう...

00000000004006e0 <f1(int)>:
  4006e0:   01 ff                   add    edi,edi
  4006e2:   e9 d9 ff ff ff          jmp    4006c0 <out(int)>
  4006e7:   66 0f 1f 84 00 00 00    nop    WORD PTR [rax+rax*1+0x0]
  4006ee:   00 00 

00000000004006f0 <f2(Bar)>:
  4006f0:   48 83 ec 08             sub    rsp,0x8
  4006f4:   01 ff                   add    edi,edi
  4006f6:   e8 d5 ff ff ff          call   4006d0 <out(Bar)>
  4006fb:   48 83 c4 08             add    rsp,0x8
  4006ff:   c3                      ret    

ご覧のとおり、f2 にはスタック ポインターをいじる余分な機能があり、ret が省略されるのを防いでいます。

(これは -O3 の g++ 4.6.1 です)

于 2012-03-12T06:04:39.553 に答える