summaryrefslogtreecommitdiffstats
path: root/src/erasure-code/isa/xor_op.cc
blob: 2b56e977c7fd92c20b4a57b4c6bb78f286e25c6c (plain)
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
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 CERN (Switzerland)
 *                                                                                                                                                                                                            * Author: Andreas-Joachim Peters <Andreas.Joachim.Peters@cern.ch>                                                                                                                                            *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 */

// -----------------------------------------------------------------------------
#include "xor_op.h"
#include <stdio.h>
#include <string.h>
#include "arch/intel.h"

#include "include/ceph_assert.h"

// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------

void
// -----------------------------------------------------------------------------
byte_xor(unsigned char* cw, unsigned char* dw, unsigned char* ew)
// -----------------------------------------------------------------------------
{
  while (cw < ew)
    *dw++ ^= *cw++;
}

// -----------------------------------------------------------------------------

void
// -----------------------------------------------------------------------------
vector_xor(vector_op_t* cw,
           vector_op_t* dw,
           vector_op_t* ew)
// -----------------------------------------------------------------------------
{
  ceph_assert(is_aligned(cw, EC_ISA_VECTOR_OP_WORDSIZE));
  ceph_assert(is_aligned(dw, EC_ISA_VECTOR_OP_WORDSIZE));
  ceph_assert(is_aligned(ew, EC_ISA_VECTOR_OP_WORDSIZE));
  while (cw < ew) {
    *dw++ ^= *cw++;
  }
}


// -----------------------------------------------------------------------------

void
// -----------------------------------------------------------------------------
region_xor(unsigned char** src,
           unsigned char* parity,
           int src_size,
           unsigned size)
{
  if (!size) {
    // nothing to do
    return;
  }

  if (!src_size) {
    // nothing to do
    return;
  }

  if (src_size == 1) {
    // just copy source to parity
    memcpy(parity, src[0], size);
    return;
  }

  unsigned size_left = size;

  // ----------------------------------------------------------
  // region or vector XOR operations require aligned addresses
  // ----------------------------------------------------------

  bool src_aligned = true;
  for (int i = 0; i < src_size; i++) {
    src_aligned &= is_aligned(src[i], EC_ISA_VECTOR_OP_WORDSIZE);
  }

  if (src_aligned &&
      is_aligned(parity, EC_ISA_VECTOR_OP_WORDSIZE)) {

#ifdef __x86_64__
    if (ceph_arch_intel_sse2) {
      // -----------------------------
      // use SSE2 region xor function
      // -----------------------------
      unsigned region_size =
        (size / EC_ISA_VECTOR_SSE2_WORDSIZE) * EC_ISA_VECTOR_SSE2_WORDSIZE;

      size_left -= region_size;
      // 64-byte region xor
      region_sse2_xor((char**) src, (char*) parity, src_size, region_size);
    } else
#endif
    {
      // --------------------------------------------
      // use region xor based on vector xor operation
      // --------------------------------------------
      unsigned vector_words = size / EC_ISA_VECTOR_OP_WORDSIZE;
      unsigned vector_size = vector_words * EC_ISA_VECTOR_OP_WORDSIZE;
      memcpy(parity, src[0], vector_size);

      size_left -= vector_size;
      vector_op_t* p_vec = (vector_op_t*) parity;
      for (int i = 1; i < src_size; i++) {
        vector_op_t* s_vec = (vector_op_t*) src[i];
        vector_op_t* e_vec = s_vec + vector_words;
        vector_xor(s_vec, p_vec, e_vec);
      }
    }
  }

  if (size_left) {
    // --------------------------------------------------
    // xor the not aligned part with byte-wise region xor
    // --------------------------------------------------
    memcpy(parity + size - size_left, src[0] + size - size_left, size_left);
    for (int i = 1; i < src_size; i++) {
      byte_xor(src[i] + size - size_left, parity + size - size_left, src[i] + size);
    }
  }
}

// -----------------------------------------------------------------------------

void
// -----------------------------------------------------------------------------
region_sse2_xor(char** src,
                char* parity,
                int src_size,
                unsigned size)
// -----------------------------------------------------------------------------
{
#ifdef __x86_64__
  ceph_assert(!(size % EC_ISA_VECTOR_SSE2_WORDSIZE));
  unsigned char* p;
  int d, l;
  unsigned i;
  unsigned char* vbuf[256];

  for (int v = 0; v < src_size; v++) {
    vbuf[v] = (unsigned char*) src[v];
  }

  l = src_size;
  p = (unsigned char*) parity;

  for (i = 0; i < size; i += EC_ISA_VECTOR_SSE2_WORDSIZE) {
    asm volatile("movdqa %0,%%xmm0" : : "m" (vbuf[0][i]));
    asm volatile("movdqa %0,%%xmm1" : : "m" (vbuf[0][i + 16]));
    asm volatile("movdqa %0,%%xmm2" : : "m" (vbuf[0][i + 32]));
    asm volatile("movdqa %0,%%xmm3" : : "m" (vbuf[0][i + 48]));

    for (d = 1; d < l; d++) {
      asm volatile("movdqa %0,%%xmm4" : : "m" (vbuf[d][i]));
      asm volatile("movdqa %0,%%xmm5" : : "m" (vbuf[d][i + 16]));
      asm volatile("movdqa %0,%%xmm6" : : "m" (vbuf[d][i + 32]));
      asm volatile("movdqa %0,%%xmm7" : : "m" (vbuf[d][i + 48]));
      asm volatile("pxor %xmm4,%xmm0");
      asm volatile("pxor %xmm5,%xmm1");
      asm volatile("pxor %xmm6,%xmm2");
      asm volatile("pxor %xmm7,%xmm3");
    }
    asm volatile("movntdq %%xmm0,%0" : "=m" (p[i]));
    asm volatile("movntdq %%xmm1,%0" : "=m" (p[i + 16]));
    asm volatile("movntdq %%xmm2,%0" : "=m" (p[i + 32]));
    asm volatile("movntdq %%xmm3,%0" : "=m" (p[i + 48]));
  }

  asm volatile("sfence" : : : "memory");
#endif // __x86_64__
  return;
}