summaryrefslogtreecommitdiff
path: root/src/arch/arm/isa/insts/neon64_mem.isa
blob: 03ad29409d0f532087ba70db31c4f4556e950344 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// -*- mode: c++ -*-

// Copyright (c) 2012-2014 ARM Limited
// All rights reserved
//
// The license below extends only to copyright in the software and shall
// not be construed as granting a license to any other intellectual
// property including but not limited to intellectual property relating
// to a hardware implementation of the functionality of the software
// licensed hereunder.  You may use the software subject to the license
// terms below provided that you ensure that this notice is replicated
// unmodified and in its entirety in all distributions of the software,
// modified or unmodified, in source code or in binary form.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met: redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer;
// redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution;
// neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Authors: Mbou Eyole
//          Giacomo Gabrielli

let {{

    header_output = ''
    decoder_output = ''
    exec_output = ''

    zeroSveVecRegUpperPartCode = '''
        ArmISA::ISA::zeroSveVecRegUpperPart(%s,
            ArmStaticInst::getCurSveVecLen<uint64_t>(xc->tcBase()));
    '''

    def mkMemAccMicroOp(name):
        global header_output, decoder_output, exec_output
        SPAlignmentCheckCodeNeon = '''
            if (baseIsSP && bits(XURa, 3, 0) &&
                SPAlignmentCheckEnabled(xc->tcBase())) {
                return std::make_shared<SPAlignmentFault>();
            }
        '''
        eaCode = SPAlignmentCheckCodeNeon + '''
            EA = XURa + imm;
        '''
        memDecl = '''
            const int MaxNumBytes = 16;
            union MemUnion {
                uint8_t bytes[MaxNumBytes];
                uint32_t floatRegBits[MaxNumBytes / 4];
            };
        '''

        # Do endian conversion for all the elements
        convCode = '''
            VReg x = {0, 0};

            x.lo = (((XReg) memUnion.floatRegBits[1]) << 32) |
                (XReg) memUnion.floatRegBits[0];
            x.hi = (((XReg) memUnion.floatRegBits[3]) << 32) |
                (XReg) memUnion.floatRegBits[2];

            const unsigned eCount = 16 / (1 << eSize);

            if (isBigEndian64(xc->tcBase())) {
                for (unsigned i = 0; i < eCount; i++) {
                    switch (eSize) {
                      case 0x3:  // 64-bit
                        writeVecElem(&x, (XReg) gtobe(
                            (uint64_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      case 0x2:  // 32-bit
                        writeVecElem(&x, (XReg) gtobe(
                            (uint32_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      case 0x1:  // 16-bit
                        writeVecElem(&x, (XReg) gtobe(
                            (uint16_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      default:  // 8-bit
                        break;  // Nothing to do here
                    }
                }
            } else {
                for (unsigned i = 0; i < eCount; i++) {
                    switch (eSize) {
                      case 0x3:  // 64-bit
                        writeVecElem(&x, (XReg) gtole(
                            (uint64_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      case 0x2:  // 32-bit
                        writeVecElem(&x, (XReg) gtole(
                            (uint32_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      case 0x1:  // 16-bit
                        writeVecElem(&x, (XReg) gtole(
                            (uint16_t) readVecElem(x, i, eSize)), i, eSize);
                        break;
                      default:  // 8-bit
                        break;  // Nothing to do here
                    }
                }
            }

            memUnion.floatRegBits[0] = (uint32_t) x.lo;
            memUnion.floatRegBits[1] = (uint32_t) (x.lo >> 32);
            memUnion.floatRegBits[2] = (uint32_t) x.hi;
            memUnion.floatRegBits[3] = (uint32_t) (x.hi >> 32);
        '''

        # Offload everything into registers
        regSetCode = ''
        for reg in range(4):
            regSetCode += '''
            AA64FpDestP%(reg)d_uw = gtoh(memUnion.floatRegBits[%(reg)d]);
            ''' % { 'reg' : reg }

        # Pull everything in from registers
        regGetCode = ''
        for reg in range(4):
            regGetCode += '''
            memUnion.floatRegBits[%(reg)d] = htog(AA64FpDestP%(reg)d_uw);
            ''' % { 'reg' : reg }

        loadMemAccCode = convCode + regSetCode
        storeMemAccCode = regGetCode + convCode

        loadIop = InstObjParams(name + 'ld',
                'MicroNeonLoad64',
                'MicroNeonMemOp',
            {   'mem_decl' : memDecl,
                'memacc_code' : loadMemAccCode,
                'ea_code' : simd64EnabledCheckCode + eaCode,
            },
            [ 'IsMicroop', 'IsMemRef', 'IsLoad' ])
        loadIop.snippets["memacc_code"] += zeroSveVecRegUpperPartCode % \
            "AA64FpDest"
        storeIop = InstObjParams(name + 'st',
                'MicroNeonStore64',
                'MicroNeonMemOp',
            {   'mem_decl' : memDecl,
                'memacc_code' : storeMemAccCode,
                'ea_code' : simd64EnabledCheckCode + eaCode,
            },
            [ 'IsMicroop', 'IsMemRef', 'IsStore' ])

        exec_output += NeonLoadExecute64.subst(loadIop) + \
            NeonLoadInitiateAcc64.subst(loadIop) + \
            NeonLoadCompleteAcc64.subst(loadIop) + \
            NeonStoreExecute64.subst(storeIop) + \
            NeonStoreInitiateAcc64.subst(storeIop) + \
            NeonStoreCompleteAcc64.subst(storeIop)
        header_output += MicroNeonMemDeclare64.subst(loadIop) + \
            MicroNeonMemDeclare64.subst(storeIop)

    def mkMarshalMicroOp(name, Name, numRegs=4):
        global header_output, decoder_output, exec_output

        getInputCodeOp1L = ''
        for v in range(numRegs):
            for p in range(4):
                getInputCodeOp1L += '''
            writeVecElem(&input[%(v)d], (XReg) AA64FpOp1P%(p)dV%(v)d_uw,
                         %(p)d, 0x2);
            ''' % { 'v' : v, 'p' : p }

        getInputCodeOp1S = ''
        for v in range(numRegs):
            for p in range(4):
                getInputCodeOp1S += '''
            writeVecElem(&input[%(v)d], (XReg) AA64FpOp1P%(p)dV%(v)dS_uw,
                         %(p)d, 0x2);
            ''' % { 'v' : v, 'p' : p }

        if name == 'deint_neon_uop':

            eCode = '''
                // input data from scratch area
                VReg input[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
                VReg output[2];  // output data to arch. SIMD regs
                VReg temp;
                temp.lo = 0;
                temp.hi = 0;
            '''
            for p in range(4):
                eCode += '''
                writeVecElem(&temp, (XReg) AA64FpDestP%(p)dV1L_uw, %(p)d, 0x2);
                ''' % { 'p' : p }
            eCode += getInputCodeOp1L

            # Note that numRegs is not always the same as numStructElems; in
            # particular, for LD1/ST1, numStructElems is 1 but numRegs can be
            # 1, 2, 3 or 4

            eCode += '''
                output[0].lo = 0;
                output[0].hi = 0;
                output[1].lo = 0;
                output[1].hi = 0;

                int eCount = dataSize / (8 << eSize);
                int eSizeBytes = 1 << eSize;  // element size in bytes
                int numBytes = step * dataSize / 4;
                int totNumBytes = numRegs * dataSize / 8;

                int structElemNo, pos, a, b;
                XReg data;

                for (int r = 0; r < 2; ++r) {
                    for (int i = 0; i < eCount; ++i) {
                        if (numBytes < totNumBytes) {
                            structElemNo = r + (step * 2);
                            if (numStructElems == 1) {
                                pos = (eSizeBytes * i) +
                                    (eCount * structElemNo * eSizeBytes);
                            } else {
                                pos = (numStructElems * eSizeBytes * i) +
                                    (structElemNo * eSizeBytes);
                            }
                            a = pos / 16;
                            b = (pos % 16) / eSizeBytes;
                            data = (XReg) readVecElem(input[a], (XReg) b,
                                                      eSize);
                            writeVecElem(&output[r], data, i, eSize);
                            numBytes += eSizeBytes;
                        }
                    }
                }
            '''
            for p in range(4):
                eCode += '''
                AA64FpDestP%(p)dV0L_uw = (uint32_t) readVecElem(output[0],
                    %(p)d, 0x2);
                ''' % { 'p' : p }
            eCode += '''
                if ((numRegs % 2 == 0) || (numRegs == 3 && step == 0)) {
            '''
            for p in range(4):
                eCode += '''
                    AA64FpDestP%(p)dV1L_uw = (uint32_t) readVecElem(
                        output[1], %(p)d, 0x2);
                ''' % { 'p' : p }
            eCode += '''
                } else {
            '''
            for p in range(4):
                eCode += '''
                    AA64FpDestP%(p)dV1L_uw = (uint32_t) readVecElem(temp,
                        %(p)d, 0x2);
                ''' % { 'p' : p }
            eCode += '''
                }
            '''

            iop = InstObjParams(name, Name, 'MicroNeonMixOp64',
                                { 'code' : eCode, 'op_class' : 'No_OpClass' },
                                ['IsMicroop'])
            header_output += MicroNeonMixDeclare64.subst(iop)
            exec_output += MicroNeonMixExecute64.subst(iop)

        elif name == 'int_neon_uop':

            eCode = '''
                // input data from arch. SIMD regs
                VReg input[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
                VReg output[2];  // output data to scratch area
            '''

            eCode += getInputCodeOp1S

            # Note that numRegs is not always the same as numStructElems; in
            # particular, for LD1/ST1, numStructElems is 1 but numRegs can be
            # 1, 2, 3 or 4

            eCode += '''
                int eCount = dataSize / (8 << eSize);
                int eSizeBytes = 1 << eSize;
                int totNumBytes = numRegs * dataSize / 8;
                int numOutputElems = 128 / (8 << eSize);
                int stepOffset = step * 32;

                for (int i = 0; i < 2; ++i) {
                    output[i].lo = 0;
                    output[i].hi = 0;
                }

                int r = 0, k = 0, i, j;
                XReg data;

                for (int pos = stepOffset; pos < 32 + stepOffset;
                        pos += eSizeBytes) {
                    if (pos < totNumBytes) {
                        if (numStructElems == 1) {
                            i = (pos / eSizeBytes) % eCount;
                            j = pos / (eCount * eSizeBytes);
                        } else {
                            i = pos / (numStructElems * eSizeBytes);
                            j = (pos % (numStructElems * eSizeBytes)) /
                                eSizeBytes;
                        }
                        data = (XReg) readVecElem(input[j], (XReg) i, eSize);
                        writeVecElem(&output[r], data, k, eSize);
                        k++;
                        if (k == numOutputElems){
                            k = 0;
                            ++r;
                        }
                    }
                }
                '''
            for v in range(2):
                for p in range(4):
                    eCode += '''
                AA64FpDestP%(p)dV%(v)d_uw = (uint32_t) readVecElem(
                    output[%(v)d], %(p)d, 0x2);
                ''' % { 'v': v, 'p': p}

            iop = InstObjParams(name, Name, 'MicroNeonMixOp64',
                                { 'code' : eCode, 'op_class' : 'No_OpClass' },
                                ['IsMicroop'])
            header_output += MicroNeonMixDeclare64.subst(iop)
            exec_output += MicroNeonMixExecute64.subst(iop)

        elif name == 'unpack_neon_uop':

            eCode = '''
                //input data from scratch area
                VReg input[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
                //output data to arch. SIMD regs
                VReg output[2] = { {0, 0}, {0, 0} };
            '''

            eCode += getInputCodeOp1L

            # Fill output regs with register data initially.  Note that
            # elements in output register outside indexed lanes are left
            # untouched
            for v in range(2):
                for p in range(4):
                    eCode += '''
                writeVecElem(&output[%(v)d], (XReg) AA64FpDestP%(p)dV%(v)dL_uw,
                             %(p)d, 0x2);
                ''' % { 'v': v, 'p': p}
            eCode += '''
                int eCount = dataSize / (8 << eSize);
                int eCount128 = 128 / (8 << eSize);
                int eSizeBytes = 1 << eSize;
                int totNumBytes = numStructElems * eSizeBytes;
                int numInputElems = eCount128;
                int stepOffset = step * 2 * eSizeBytes;
                int stepLimit = 2 * eSizeBytes;

                int r = 0, i, j;
                XReg data;

                for (int pos = stepOffset; pos < stepLimit + stepOffset;
                        pos += eSizeBytes) {
                    if (pos < totNumBytes) {
                        r = pos / eSizeBytes;
                        j = r / numInputElems;
                        i = r % numInputElems;
                        data = (XReg) readVecElem(input[j], (XReg) i, eSize);

                        if (replicate) {
                            for (int i = 0; i < eCount128; ++i) {
                                if (i < eCount) {
                                    writeVecElem(&output[r % 2], data, i,
                                                 eSize);
                                } else {  // zero extend if necessary
                                    writeVecElem(&output[r % 2], (XReg) 0, i,
                                                 eSize);
                                }
                            }
                        } else {
                            writeVecElem(&output[r % 2], data, lane, eSize);
                        }
                    }
                }
            '''
            for v in range(2):
                for p in range(4):
                    eCode += '''
                AA64FpDestP%(p)dV%(v)dL_uw = (uint32_t) readVecElem(
                    output[%(v)d], %(p)d, 0x2);
                ''' % { 'v' : v, 'p' : p }

            iop = InstObjParams(name, Name, 'MicroNeonMixLaneOp64',
                                { 'code' : eCode }, ['IsMicroop'])
            header_output += MicroNeonMixLaneDeclare64.subst(iop)
            exec_output += MicroNeonMixExecute64.subst(iop)

        elif name == 'pack_neon_uop':

            eCode = '''
                // input data from arch. SIMD regs
                VReg input[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
                VReg output[2];  // output data to scratch area
            '''

            eCode += getInputCodeOp1S

            eCode += '''
                int eSizeBytes = 1 << eSize;
                int numOutputElems = 128 / (8 << eSize);
                int totNumBytes = numStructElems * eSizeBytes;
                int stepOffset = step * 32;
                int stepLimit = 32;

                int r = 0, i, j;
                XReg data;

                for (int i = 0; i < 2; ++i) {
                    output[i].lo = 0;
                    output[i].hi = 0;
                }

                for (int pos = stepOffset; pos < stepLimit + stepOffset;
                        pos += eSizeBytes) {
                    if (pos < totNumBytes) {
                        r = pos / 16;
                        j = pos / eSizeBytes;
                        i = (pos / eSizeBytes) %  numOutputElems;
                        data = (XReg) readVecElem(input[j], lane, eSize);
                        writeVecElem(&output[r % 2], data, i, eSize);
                    }
                }
            '''

            for v in range(2):
                for p in range(4):
                    eCode += '''
                AA64FpDestP%(p)dV%(v)d_uw = (uint32_t) readVecElem(
                    output[%(v)d], %(p)d, 0x2);
                ''' % { 'v' : v, 'p' : p }

            iop = InstObjParams(name, Name, 'MicroNeonMixLaneOp64',
                                { 'code' : eCode }, ['IsMicroop'])
            header_output += MicroNeonMixLaneDeclare64.subst(iop)
            exec_output += MicroNeonMixExecute64.subst(iop)

    # Generate instructions
    mkMemAccMicroOp('mem_neon_uop')
    mkMarshalMicroOp('deint_neon_uop', 'MicroDeintNeon64_1Reg', numRegs=1)
    mkMarshalMicroOp('deint_neon_uop', 'MicroDeintNeon64_2Reg', numRegs=2)
    mkMarshalMicroOp('deint_neon_uop', 'MicroDeintNeon64_3Reg', numRegs=3)
    mkMarshalMicroOp('deint_neon_uop', 'MicroDeintNeon64_4Reg', numRegs=4)
    mkMarshalMicroOp('int_neon_uop', 'MicroIntNeon64_1Reg', numRegs=1)
    mkMarshalMicroOp('int_neon_uop', 'MicroIntNeon64_2Reg', numRegs=2)
    mkMarshalMicroOp('int_neon_uop', 'MicroIntNeon64_3Reg', numRegs=3)
    mkMarshalMicroOp('int_neon_uop', 'MicroIntNeon64_4Reg', numRegs=4)
    mkMarshalMicroOp('unpack_neon_uop', 'MicroUnpackNeon64')
    mkMarshalMicroOp('pack_neon_uop', 'MicroPackNeon64')

}};

let {{

    iop = InstObjParams('vldmult64', 'VldMult64', 'VldMultOp64', '', [])
    header_output += VMemMultDeclare64.subst(iop)
    decoder_output += VMemMultConstructor64.subst(iop)

    iop = InstObjParams('vstmult64', 'VstMult64', 'VstMultOp64', '', [])
    header_output += VMemMultDeclare64.subst(iop)
    decoder_output += VMemMultConstructor64.subst(iop)

    iop = InstObjParams('vldsingle64', 'VldSingle64', 'VldSingleOp64', '', [])
    header_output += VMemSingleDeclare64.subst(iop)
    decoder_output += VMemSingleConstructor64.subst(iop)

    iop = InstObjParams('vstsingle64', 'VstSingle64', 'VstSingleOp64', '', [])
    header_output += VMemSingleDeclare64.subst(iop)
    decoder_output += VMemSingleConstructor64.subst(iop)

}};