mrDarker
2025-07-24 27b45f7dd911640b4c8fefe6e060fc35a1f98e6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SISMath.cpp: implementation of the CSISMath class.
//
//////////////////////////////////////////////////////////////////////
 
#include "stdafx.h"
#include "SISMath.h"
 
 
#include <emmintrin.h>
#include <tmmintrin.h>
#include <omp.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
#define SIS_SIMD
 
#define MOMP_MAX_THREAD_SIZE 4
 
//#define SIS_ASM
 
 
int64 CSISMath::OMP_CCU8_A(const uchar * vec1, int len)
{
    int s = 0;
    int64 sum = 0;
 
    int64 sumT[MOMP_MAX_THREAD_SIZE] = {0};
#pragma omp parallel num_threads(MOMP_MAX_THREAD_SIZE)
    {
        int i= 0;
        int templen= len- 16;
        __m128i    mmResult, mmResult2;
        __m128i    mmA;
        __m128i    mmAA, mmBB;    
        __m128i mmZeroData= _mm_setzero_si128();
 
        int64 res64[2];
        mmResult= _mm_setzero_si128();
#pragma omp for private(i)
        for(i= 0; i <= templen; i+= 16)
        {
            mmA= _mm_load_si128((__m128i*) (vec1+ i));
 
            mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmAA);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
 
            mmAA= _mm_unpacklo_epi8(mmA, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmAA);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
        }
        _mm_storeu_si128((__m128i*)res64, mmResult);
        sumT[omp_get_thread_num()]= res64[0]+ res64[1];
    }
 
    int i = 0;
    for (i = 0; i < MOMP_MAX_THREAD_SIZE; i++)
    {
        sum += sumT[i];
    }
 
    i= len- (len&15);
    for(; i < len; i++ )
    {
        s += vec1[i] * vec1[i];
    }
 
    return sum + s;
}
 
int64    CSISMath::OMP_CCU8_UA(const uchar *vec1, int len)
{
    int s = 0;
    int64 sum = 0;
    
    int64 sumT[MOMP_MAX_THREAD_SIZE];
#pragma omp parallel num_threads(MOMP_MAX_THREAD_SIZE)
    {
        int i = 0;
        int templen= len- 16;
        int64    res64[2];
        __m128i    mmResult, mmResult2;
        __m128i mmA, mmAA, mmBB;
        __m128i mmZeroData= _mm_setzero_si128();
        mmResult= mmZeroData;
#pragma omp for private(i)
        for(i= 0; i <= templen; i+= 16)
        {
            mmA= _mm_loadu_si128((__m128i*) (vec1+ i));
 
            mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmAA);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
 
            mmAA= _mm_unpacklo_epi8(mmA, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmAA);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
        }
        _mm_storeu_si128((__m128i*)res64, mmResult);
        sumT[omp_get_thread_num()]= res64[0]+ res64[1];
    }
 
    int i = 0;
    for(i= 0; i< MOMP_MAX_THREAD_SIZE; i++)
    {
        sum+= sumT[i];
    }
 
 
    i= len- (len&15);
    for(; i < len; i++ )
    {
        s += vec1[i] * vec1[i];
    }
 
    return sum+ s;
}
 
int64    CSISMath::OMP_CCU8_AUA( const uchar * vec1, const uchar * vec2, int len )
{
    int s = 0;
    int64 sum = 0;
    int64 temp[MOMP_MAX_THREAD_SIZE];
    
 
 
#pragma omp parallel num_threads(MOMP_MAX_THREAD_SIZE)
    {
        int i = 0;
        int    templen= len- 16;
        __m128i    mmResult, mmResult2;
        __m128i    mmA, mmB;
        __m128i    mmAA, mmBB;
        __m128i mmZeroData= _mm_setzero_si128();
        mmResult= _mm_setzero_si128();
        int64 res64[2];
 
#pragma omp for private(i)
        for(i= 0; i <= templen; i+= 16)
        {
            mmA= _mm_load_si128((__m128i*) (vec1+ i));
            mmB= _mm_loadu_si128((__m128i*) (vec2+ i));
 
            mmAA= _mm_unpackhi_epi8(mmA, mmZeroData);
            mmBB= _mm_unpackhi_epi8(mmB, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmBB);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
 
            mmAA= _mm_unpacklo_epi8(mmA, mmZeroData);
            mmBB= _mm_unpacklo_epi8(mmB, mmZeroData);
            mmResult2= _mm_madd_epi16(mmAA, mmBB);
            mmAA= _mm_unpackhi_epi32(mmResult2, mmZeroData);
            mmBB= _mm_unpacklo_epi32(mmResult2, mmZeroData);
            mmResult= _mm_add_epi64(mmAA, mmResult);
            mmResult= _mm_add_epi64(mmBB, mmResult);
        }
        _mm_storeu_si128((__m128i*)res64, mmResult);
        temp[omp_get_thread_num()]= res64[0]+ res64[1];
    }
    int i = 0;
 
    for(i= 0; i< MOMP_MAX_THREAD_SIZE; i++)
    {
        sum+= temp[i];
    }
 
    i= len- (len&15);
    for(; i < len; i++ )
    {
        s += vec1[i] * vec1[i];
    }
    return sum+ s;
}