質問

Stackoverflowに質問を投稿するのはこれが初めてですので、質問/コードをフォーマットする際に行った可能性のあるエラーを見落としてみてください。しかし、私に同じことを指摘してください。

2つの128ビット(4つのフロート変数を含む)数字を追加するために、いくつかの単純な内在性ルーチンを作成しようとしていました。ネット上でいくつかのコードを見つけて、システムで実行しようとしていました。コードは次のとおりです。

 //this is a sample Intrinsics program to add two vectors.

    #include <iostream>  
    #include <iomanip>      
    #include <xmmintrin.h>  
    #include <stdio.h>

    using namespace std;

    struct vector4 {  
        float x, y, z, w;    };   

    //functions to operate on them.  
    vector4 set_vector(float x, float y, float z, float w = 0) {     
        vector4 temp;  
        temp.x = x;   
        temp.y = y;   
        temp.z = z;  
        temp.w = w;  
        return temp;  
    }    


    void print_vector(const vector4& v) {   
        cout << " This is the contents of vector: " << endl;  
        cout << " > vector.x = " << v.x << endl;  
        cout << " vector.y = " << v.y << endl;  
        cout << " vector.z = " << v.z << endl;  
        cout << " vector.w = " << v.w << endl;  
    }

    vector4 sse_vector4_add(const vector4&a, const vector4& b) {  
        vector4 result;  

        asm volatile (  
          "movl $a, %eax" //move operands into registers.  
          "\n\tmovl $b, %ebx"  
          "\n\tmovups  (%eax), xmm0"  //move register contents into SSE registers.  
          "\n\tmovups (%ebx), xmm1"  
          "\n\taddps xmm0, xmm1" //add the elements. addps operates on single-precision vectors.    
          "\n\t movups xmm0, result" //move result into vector4 type data.  
        );
        return result;  
    }

    int main() {     
        vector4 a, b, result;  
        a = set_vector(1.1, 2.1, 3.2, 4.5);   
        b = set_vector(2.2, 4.2, 5.6);    
        result = sse_vector4_add(a, b);    
        print_vector(a);  
        print_vector(b);    
        print_vector(result);
        return 0;
    }

私が使用するG ++パラメーターは次のとおりです。

g++ -Wall -pedantic -g -march=i386 -msse intrinsics_SSE_example.C -o h

私が得るエラーは次のとおりです。

intrinsics_SSE_example.C: Assembler messages:  
intrinsics_SSE_example.C:45: Error: too many memory references for movups  
intrinsics_SSE_example.C:46: Error: too many memory references for movups  
intrinsics_SSE_example.C:47: Error: too many memory references for addps  
intrinsics_SSE_example.C:48: Error: too many memory references for movups  

私はこれらのエラーをデバッグしようとするのに多くの時間を費やし、それらをグーグルで検索しました。私は内因性の完全なnoobであるため、いくつかの重要なことを見落としていたかもしれません。

どんな助けも感謝しています、
ありがとう、
スリラム。

役に立ちましたか?

解決

本質的ではなく、ASMブロックを使用しています。

これらのxmmxはレジスタであるため、 %:

      "\n\tmovups  (%eax), %xmm0"
      // etc.

また、ASMにはいくつかのエラーがあります。

  1. 変更しないでください ebx 登録。
  2. $a などはアセンブラーのグローバルシンボルと見なされますが、そうではありません。
  3. addps %xmm0, %xmm1 結果をXMM1に保存します。 AT&T構文では、目的地が右側にあることを忘れないでください。

修正されたASMブロックは似ています

    asm volatile (  
      "movl %1, %%eax"
      "\n\tmovl %2, %%ecx"  
      "\n\tmovups  (%%eax), %%xmm0"
      "\n\tmovups (%%ecx), %%xmm1"  
      "\n\taddps %%xmm0, %%xmm1"
      "\n\tmovups %%xmm0, %0"
      : "=m"(result)
      : "r"(&a), "r"(&b)
      : "eax", "ecx");

基本的に、%0はのアドレスに置き換えられます result, 、%1と%2はに置き換えられます &a&b. 。見る http://www.ibiblio.org/gferg/ldp/gcc-inline-assembly-howto.html 詳細な説明。 "eax", "ecx" これらの2つのレジスタがそれらの%nの置換として使用されるのを防ぎます。

しかし、最初の2 movl'sは不要です...

    asm volatile(  
      "\n\tmovups (%1), %%xmm0"
      "\n\tmovups (%2), %%xmm1"  
      "\n\taddps %%xmm1, %%xmm0"
      "\n\tmovups %%xmm0, %0"
      : "=m"(result)
      : "r"(&a), "r"(&b));

本質的に言及したので、なぜ使用してみませんか __builtin_ia32_addps?

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