File: rotate-and-shift.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (625 lines) | stat: -rw-r--r-- 22,830 bytes parent folder | download | duplicates (2)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
; X86ISA Library

; Note: The license below is based on the template at:
; http://opensource.org/licenses/BSD-3-Clause

; Copyright (C) 2015, Regents of the University of Texas
; Copyright (C) 2024, Kestrel Technology, LLC
; All rights reserved.

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are
; met:

; o Redistributions of source code must retain the above copyright
;   notice, this list of conditions and the following disclaimer.

; o 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.

; o 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
; HOLDER 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.

; Original Author(s):
; Shilpi Goel         <shigoel@cs.utexas.edu>
; Contributing Author(s):
; Alessandro Coglio (www.alessandrocoglio.info)

(in-package "X86ISA")

;; ======================================================================

(include-book "shifts-spec"
              :ttags (:undef-flg))
(include-book "rotates-spec"
              :ttags (:undef-flg))
(include-book "../decoding-and-spec-utils"
              :ttags (:undef-flg))
(local (include-book "centaur/bitops/ihs-extensions" :dir :system))

;; ======================================================================
;; INSTRUCTION: SAL/SAR/SHL/SHR/RCL/RCR/ROL/ROR
;; ======================================================================

(local
 (defrule add-to-*ip-integerp-type
   (implies (and (integerp *ip)
                 (integerp delta))
            (integerp (mv-nth 1 (add-to-*ip proc-mode *ip delta x86))))
   :in-theory (e/d (add-to-*ip) ())
   :rule-classes (:rewrite :type-prescription)))

(def-inst x86-sal/sar/shl/shr/rcl/rcr/rol/ror

  :guard (not (equal (modr/m->reg modr/m) 6))

  :guard-hints (("Goal"
                 :in-theory (e/d ()
                                 (unsigned-byte-p
                                  not force (force)))))

  :parents (one-byte-opcodes)

  :returns (x86 x86p :hyp (x86p x86)
                :hints (("Goal" :in-theory
                         (e/d ()
                              (trunc
                               select-operand-size
                               mv-nth-0-of-add-to-*ip-when-64-bit-modep
                               mv-nth-1-of-add-to-*ip-when-64-bit-modep
                               signed-byte-p
                               unsigned-byte-p
                               not force (force))))))

  :long
  "<p>
  Op/En: MI<br/>
  C0/0: ROL r/m8, imm8<br/>
  C0/1: ROR r/m8, imm8<br/>
  C0/2: RCL r/m8, imm8<br/>
  C0/3: RCR r/m8, imm8<br/>
  C0/4: SAL/SHL r/m8, imm8<br/>
  C0/5: SHR r/m8, imm8<br/>
  C0/7: SAR r/m8, imm8<br/>
  C1/0: ROL r/m16/32/64, imm8<br/>
  C1/1: ROR r/m16/32/64, imm8<br/>
  C1/2: RCL r/m16/32/64, imm8<br/>
  C1/3: RCR r/m16/32/64, imm8<br/>
  C1/4: SAL/SHL r/m16/32/64, imm8<br/>
  C1/5: SHR r/m16/32/64, imm8<br/>
  C1/7: SAR r/m16/32/64. imm8<br/>
  </p>

  <p>
  Op/En: M1<br/>
  D0/0: ROL r/m8, 1<br/>
  D0/1: ROR r/m8, 1<br/>
  D0/2: RCL r/m8, 1<br/>
  D0/3: RCR r/m8, 1<br/>
  D0/4: SAL/SHL r/m8, 1<br/>
  D0/5: SHR r/m8, 1<br/>
  D0/7: SAR r/m8, 1<br/>
  D1/0: ROL r/m16/32/64, 1<br/>
  D1/1: ROR r/m16/32/64, 1<br/>
  D1/2: RCL r/m16/32/64, 1<br/>
  D1/3: RCR r/m16/32/64, 1<br/>
  D1/4: SAL/SHL r/m16/32/64, 1<br/>
  D1/5: SHR r/m16/32/64, 1<br/>
  D1/7: SAR r/m16/32/64, 1<br/>
  </p>

  <p>
  Op/En: MC<br/>
  D2/0: ROL r/m8, CL<br/>
  D2/1: ROR r/m8, CL<br/>
  D2/2: RCL r/m8, CL<br/>
  D2/3: RCR r/m8, CL<br/>
  D2/4: SAL/SHL r/m8, CL<br/>
  D2/5: SHR r/m8, CL<br/>
  D2/7: SAR r/m8, CL<br/>
  D3/0: ROL r/m16/32/64, CL<br/>
  D3/1: ROR r/m16/32/64, CL<br/>
  D3/2: RCL r/m16/32/64, CL<br/>
  D3/3: RCR r/m16/32/64, CL<br/>
  D3/4: SAL/SHL r/m16/32/64, CL<br/>
  D3/5: SHR r/m16/32/64, CL<br/>
  D3/7: SAR r/m16/32/64, CL<br/>
  </p>"

  :modr/m t

  :body

  (b* ((p2 (prefixes->seg prefixes))
       (p4? (equal #.*addr-size-override* (prefixes->adr prefixes)))

       (byte-operand? (or (equal opcode #xC0)
                          (equal opcode #xD0)
                          (equal opcode #xD2)))
       ((the (integer 0 8) ?reg/mem-size)
        (select-operand-size
         proc-mode byte-operand? rex-byte nil prefixes nil nil nil x86))

       (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))

       (inst-ac? t)
       ((mv flg0 ?reg/mem (the (unsigned-byte 3) increment-RIP-by) addr x86)
        (x86-operand-from-modr/m-and-sib-bytes
         proc-mode #.*gpr-access* reg/mem-size inst-ac?
         nil ;; Not a memory pointer operand
         seg-reg p4? temp-rip rex-byte r/m mod sib
         ;; Bytes of immediate data (only relevant when RIP-relative
         ;; addressing is done to get ?reg/mem operand)
         (if (or (equal opcode #xC0)
                 (equal opcode #xC1))
             1
           0)
         x86))
       ((when flg0)
        (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg0))

       ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
        (add-to-*ip proc-mode temp-rip increment-RIP-by x86))
       ((when flg) (!!ms-fresh :rip-increment-error flg))

       ((mv flg1 shift/rotate-by x86)
        (case opcode
          ((#xD0 #xD1)
           (mv nil 1 x86))
          ((#xD2 #xD3)
           (mv nil (rr08 *rcx* rex-byte x86) x86))
          ((#xC0 #xC1)
           (rme-size-opt proc-mode 1 temp-rip #.*cs* :x nil x86))
          (otherwise ;; will not be reached
           (mv nil 0 x86))))
       ((when flg1)
        (!!ms-fresh :rme-size-error flg1))

       ;; REX does not promote to 64-bits for byte-operand opcodes
       (countMask (if (and (not byte-operand?)
                           (logbitp #.*w* rex-byte))
                      #x3F
                    #x1F))
       (shift/rotate-by (logand countMask shift/rotate-by))

       ((mv flg (the (signed-byte #.*max-linear-address-size+1*) temp-rip))
        (if (or (equal opcode #xC0)
                (equal opcode #xC1))
            (add-to-*ip proc-mode temp-rip 1 x86)
          (mv nil temp-rip)))
       ((when flg) (!!ms-fresh :rip-increment-error flg))

       (badlength? (check-instruction-length start-rip temp-rip 0))
       ((when badlength?)
        (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)

       ;; Computing the flags and the result:
       (input-rflags (the (unsigned-byte 32) (rflags x86)))

       ((mv result
            (the (unsigned-byte 32) output-rflags)
            (the (unsigned-byte 32) undefined-flags))
        (case reg
          (0
           ;; ROL
           (rol-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (1
           ;; ROR
           (ror-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (2
           ;; RCL
           (rcl-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (3
           ;; RCR
           (rcr-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (4
           ;; SAL/SHL
           (sal/shl-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (5
           ;; SHR
           (shr-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          (7
           ;; SAR
           (sar-spec reg/mem-size reg/mem shift/rotate-by input-rflags))
          ;; The guard for this function will ensure that we don't
          ;; reach here.
          (otherwise
           (mv 0 0 0))))

       ;; Update the x86 state:

       (x86 (write-user-rflags output-rflags undefined-flags x86))

       ((mv flg2 x86)
        (x86-operand-to-reg/mem proc-mode reg/mem-size
                                 inst-ac?
                                 nil ;; Not a memory pointer operand
                                 ;; TO-DO@Shilpi: Remove this trunc.
                                 (trunc reg/mem-size result)
                                 seg-reg
                                 addr
                                 rex-byte
                                 r/m
                                 mod
                                 x86))
       ;; Note: If flg2 is non-nil, we bail out without changing the x86 state.
       ((when flg2)
        (!!ms-fresh :x86-operand-to-reg/mem flg2))

       (x86 (write-*ip proc-mode temp-rip x86)))
    x86))

;; ======================================================================
;; INSTRUCTION: SHLD/SHRD
;; ======================================================================

(def-inst x86-shld/shrd

  :returns (x86 x86p :hyp (x86p x86))

  :parents (one-byte-opcodes)

  :short "Double-precision shift left or right."

  :long
  "<p>
   Op/En: MRI<br/>
   0F A4: SHLD r/m16, r16, imm8<br/>
   0F A4: SHLD r/m32, r32, imm8<br/>
   0F A4: SHLD r/m64, r64, imm8<br/>
   </p>

   <p>
   Op/En: MRC<br/>
   0F A5: SHLD r/m16, r16, CL<br/>
   0F A5: SHLD r/m32, r32, CL<br/>
   0F A5: SHLD r/m64, r64, CL<br/>
   </p>

   <p>
   Op/En: MRI<br/>
   0F AC: SHRD r/m16, r16, imm8<br/>
   0F AC: SHRD r/m32, r32, imm8<br/>
   0F AC: SHRD r/m64, r64, imm8<br/>
   </p>

   <p>
   Op/En: MRC<br/>
   0F AD: SHRD r/m16, r16, CL<br/>
   0F AD: SHRD r/m32, r32, CL<br/>
   0F AD: SHRD r/m64, r64, CL<br/>
   </p>"

  :modr/m t

  :body

  (b* ((p2 (prefixes->seg prefixes))
       (p4? (equal #.*addr-size-override* (prefixes->adr prefixes)))

       ((the (integer 2 8) operand-size)
        (select-operand-size proc-mode
                             nil ; not a byte operand
                             rex-byte
                             nil ; not an immediate operand
                             prefixes
                             nil ; no 64-bit default in 64-bit mode
                             nil ; don't ignore REX in 64-bit mode
                             nil ; don't ignore P3 in 64-bit mode
                             x86))

       (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))

       ;; read destination operand:

       (inst-ac? t)
       ((mv flg dst-value increment-rip-by dst-addr x86)
        (x86-operand-from-modr/m-and-sib-bytes proc-mode
                                               #.*gpr-access*
                                               operand-size
                                               inst-ac?
                                               nil ; not memory pointer operand
                                               seg-reg
                                               p4?
                                               temp-rip
                                               rex-byte
                                               r/m
                                               mod sib
                                               1 ; imm8
                                               x86))
       ((when flg) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg))

       ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
        (add-to-*ip proc-mode temp-rip increment-rip-by x86))
       ((when flg) (!!ms-fresh :rip-increment-error flg))

       ;; read source operand:

       (src-value (rgfi-size operand-size
                             (reg-index reg rex-byte #.*r*)
                             rex-byte
                             x86))

       ;; read count operand:

       ((mv flg count x86)
        (case opcode
          ((#xA4 #xAC) (rme-size-opt proc-mode 1 temp-rip #.*cs* :x nil x86))
          ((#xA5 #xAD) (mv nil (rr08 *rcx* rex-byte x86) x86))
          (otherwise (mv nil 0 x86)))) ; unreachable
       ((when flg) (!!ms-fresh :rme-size-error flg))

       ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
        (case opcode
          ((#xA4 #xAC) (add-to-*ip proc-mode temp-rip 1 x86))
          ((#xA5 #xAD) (mv nil temp-rip))
          (otherwise (mv nil 0)))) ; unreachable
       ((when flg) (!!ms-fresh :rip-increment-error flg))

       ;; check instruction length now that we have read all of it:

       (badlength? (check-instruction-length start-rip temp-rip 0))
       ((when badlength?)
        (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)

       ;; mask count according to pseudocode (the text only mentions masking
       ;; when CL is used, but the pseudocode masks also if imm8 is used):

       (count-mask (if (logbitp #.*w* rex-byte)
                       #x3f
                     #x1f))
       (count (logand count-mask count))

       ;; compute result and flags:

       (input-rflags (the (unsigned-byte 32) (rflags x86)))

       ((mv result
            result-undefined?
            (the (unsigned-byte 32) output-rflags)
            (the (unsigned-byte 32) undefined-flags))
        (case opcode
          ((#xA4 #xA5) (shld-spec
                        operand-size dst-value src-value count input-rflags))
          ((#xAC #xAD) (shrd-spec
                        operand-size dst-value src-value count input-rflags))
          (otherwise (mv 0 nil 0 0)))) ; unreachable

       ((mv result x86)
        (if result-undefined?
            (undef-read x86)
          (mv result x86)))

       ;; update the state:

       (x86 (write-user-rflags output-rflags undefined-flags x86))

       ((mv flg x86)
        (x86-operand-to-reg/mem proc-mode
                                operand-size
                                inst-ac?
                                nil ;; not memory pointer operand
                                (trunc operand-size result) ; TODO: remove trunc
                                seg-reg
                                dst-addr
                                rex-byte
                                r/m
                                mod
                                x86))
       ((when flg) (!!ms-fresh :x86-operand-to-reg/mem flg))

       (x86 (write-*ip proc-mode temp-rip x86)))

    x86))

;; ======================================================================
;; INSTRUCTION: SARX/SHLX/SHRX
;; ======================================================================

(def-inst x86-sarx/shlx/shrx

  :returns (x86 x86p :hyp (x86p x86))

  :parents (three-byte-opcodes)

  :short "SARX/SHLX/SHRX: Shift without affecting flags."

  :vex t

  :modr/m t

  :guard (vex-prefixes-byte0-p vex-prefixes)

  :body

  (b* ((p2 (prefixes->seg prefixes))
       (p4? (equal #.*addr-size-override* (prefixes->adr prefixes)))
       (seg-reg (select-segment-register proc-mode p2 p4? mod r/m sib x86))

       (rex-byte (rex-byte-from-vex-prefixes vex-prefixes))

       ;; The operand size is always 32 in 32-bit mode.
       ;; In 64-bit mode, it is 32 or 64 based on whether VEX.W is 0 or 1.
       ((the (integer 4 8) operand-size)
        (if (and (equal proc-mode #.*64-bit-mode*)
                 (equal (vex->w vex-prefixes) 1))
            8
          4))

       ;; Since the Intel manual does not say anything specific about registers,
       ;; the registers for the operands must be the general-purpose ones.
       ;; This is confirmed by looking at some disassembled code.

       ;; The first source operand, i.e. Operand 2 in the Intel manual,
       ;; is specified in the Mod and R/M bits of the ModR/M byte.
       ;; This operand is the value to be shifted.
       ;; We ignore the increment-rip-by result, because it is always 0,
       ;; since these instructions do not involve immediate operands.
       ;; We also ignore the returned address of the operand,
       ;; because the operand is only used as source in these instructions.
       (inst-ac? t)
       ((mv flg val-to-shift & & x86)
        (x86-operand-from-modr/m-and-sib-bytes proc-mode
                                               *gpr-access*
                                               operand-size
                                               inst-ac?
                                               nil ; not memory pointer operand
                                               seg-reg
                                               p4?
                                               temp-rip
                                               rex-byte
                                               r/m
                                               mod
                                               sib
                                               0 ; num-imm-bytes
                                               x86))
       ((when flg) (!!ms-fresh :x86-operand-from-modr/m-and-sib-bytes flg))

       ;; The second source operand, i.e. Operand 3 in the Intel manual,
       ;; is specified in the vvvv bits of the VEX prefix,
       ;; in negated (one's complement) form;
       ;; see Figure 2-9 of the Intel manual Volume 2 of Dec 2023.
       ;; This operand is the count by which
       ;; the other source operand must be shifted.
       ;; Based on the operand size,
       ;; the count is masked to keep the low 5 or 6 bits,
       ;; as specified in the pseudocode of the Intel manual
       ;; for these instructions.
       (cnt-reg (vex-vvvv-reg-index (vex->vvvv vex-prefixes)))
       (cnt-to-shift (rgfi-size operand-size
                                cnt-reg
                                rex-byte
                                x86))
       (cnt-mask (if (= operand-size 4) #x1f #x3f))
       (cnt-to-shift (logand cnt-to-shift cnt-mask))

       ;; We calculate the result of the shift,
       ;; based on the pp bits in the VEX prefixes,
       ;; which provide an opcode extension
       ;; to select one of the three instructions.
       ;; See Figure 2-9 of Intel manual Volume 2 of Dec 2023.
       (result (case (vex->pp vex-prefixes)
                 (#b01 (shlx-spec operand-size val-to-shift cnt-to-shift)) ; 66
                 (#b10 (sarx-spec operand-size val-to-shift cnt-to-shift)) ; F2
                 (#b11 (shrx-spec operand-size val-to-shift cnt-to-shift)) ; F3
                 (otherwise 0))) ; cannot happen

       ;; The destination operand, i.e. Operand 1 in the Intel manual,
       ;; is specified in the Reg/Opcode bits of the ModR/M byte,
       ;; where in this case those bits specify a register
       ;; (not an opcode extension).
       ;; So we store the result into that register.
       (x86 (!rgfi-size operand-size
                        (reg-index reg rex-byte #.*r*)
                        result
                        rex-byte
                        x86))

       ;; Update the program counter.
       (x86 (write-*ip proc-mode temp-rip x86)))

    x86)

  :guard-hints (("Goal" :in-theory (enable shlx-spec sarx-spec shrx-spec))))

;; ======================================================================
;; INSTRUCTION: PSLLDQ/PSRLDQ
;; ======================================================================

(def-inst x86-pslldq/psrldq

  :parents (two-byte-opcodes)

  :short "Shift double quadword left/right logical (SSE variant)."

  :long
  "<code>
   PSLLDQ xmm1, imm8
   PSRLDQ xmm1, imm8
   </code>"

  :modr/m t

  :guard (member-equal (modr/m->reg modr/m) '(7 3))

  :returns (x86 x86p :hyp (x86p x86))

  :body

  (b* (;; The index of the XMM register
       ;; is in the R/M portion of the ModR/M byte.
       ;; It can be extended by one bit via the REX byte.
       (reg-index (reg-index r/m rex-byte #.*b*))

       ;; Read the 128-bit (i.e. 16-byte) operand from the XMM register.
       (operand (xmmi-size 16 reg-index x86))

       ;; Read the shift count, which is an 8-bit immediate operand.
       ((mv flg count x86)
        (rme-size-opt proc-mode 1 temp-rip #.*cs* :x nil x86))
       ((when flg) (!!ms-fresh :rme-size-error flg))

       ;; Increment the instruction pointer by 1, to account for the imm8.
       ((mv flg (the (signed-byte #.*max-linear-address-size*) temp-rip))
        (add-to-*ip proc-mode temp-rip 1 x86))
       ((when flg) (!!ms-fresh :rip-increment-error flg))

       ;; Ensure the instruction is not too long.
       (badlength? (check-instruction-length start-rip temp-rip 0))
       ((when badlength?)
        (!!fault-fresh :gp 0 :instruction-length badlength?)) ;; #GP(0)

       ;; According to Intel Manual (Dec 2023) Volume 2 Section 3.1.1.3,
       ;; imm8 indicates an 8-bit signed immediate operand,
       ;; i.e. an integer between -128 and +127.
       ;; However, the Intel Manual describes PSRLDQ
       ;; as if the count were unsigned:
       ;; it says what happens if it is greater than 15,
       ;; but it does not say what happens if it is negative.
       ;; We interpret this as if the imm8 is actually signed in this case;
       ;; thus, a negative value (if signed) is treated as
       ;; an unsigned value larger than 15.
       ;; If the count is larger than 15, the result is just 0.
       ;; If instead the count is between 0 and 15,
       ;; the operand is shifted by that number of bytes to the left or right,
       ;; and in the case of a left shift we only keep the low 128 bits.
       ;; We automatically get 0 as result if the count is larger than 15.
       ;; The left (PSLLDQ) vs. right (PSRLDQ)
       ;; is determined by the Reg byte, which is an opcode extension.
       ((the (unsigned-byte 128) result)
        (case reg
          (7 (logand (1- (expt 2 128)) (ash operand (* 8 count))))
          (3 (ash operand (- (* 8 count))))
          (t (prog2$ (acl2::impossible) 0))))

       ;; Write the result into the register.
       (x86 (!xmmi-size 16 reg-index result x86))

       ;; Update the instruction pointer.
       (x86 (write-*ip proc-mode temp-rip x86)))

    x86)

  :guard-hints (("Goal" :in-theory (disable unsigned-byte-p natp)))

  :prepwork
  ((defrulel verify-guards-lemma
     (implies (and (unsigned-byte-p 128 a)
                   (natp b))
              (unsigned-byte-p 128 (ash a (- (* 8 b))))))))

;; ======================================================================