how to mov byte into 128 bit register?

By : poby
Source: Stackoverflow.com
Question!

I have a zeroed 128 bit register I want to shift left and add a byte. I can shift it with:

pslldq xmm0, 1 

...but now I want to copy al into the empty space. Something like:

or xmm0, al

which of course doesn't work. I only want the lowest 8 bits affected. This will be in a loop where successive values of al will be used to fill the register. So I need some kind of mov instruction or other alternative.

The ideal would be a single instruction to shift left 8 bits and insert but I don't think such exists.

I have spent a lot of time rummaging around in the x86-64 instruction set data but can't find anything that will allow me to do what I want. Can it be done?

UPDATE: I found an error in my code logic after trying pinsrb. pinsrb would be great but unfortunately it can only use an immediate index, not a register.

I'm taking bytes from non contiguous locations so I think I need to do it a byte at a time. The number of bytes can be anywhere from 1 to 16. The first byte I grab should end up in the lowest byte of xmm0, the next byte goes into the next lowest etc.

By : poby


Answers

Intel's intrinsics guide can be useful for finding vector instructions. It lists the asm mnemonic as well as the intrinsic (and you can search by mnemonic instead of intrinsic, since the search matches on the whole text of the entry).

Intel's PDF reference manual also has an index. The insn set ref manual is volume 2. See links to Intel's manuals in the tag wiki.


PINSRB could do exactly what you asked, but don't do that. You don't need to shift the vector left, because the integer -> vector insert with merging instructions (PINSR*) take an index for the insert position. (And already require a shuffle uop, so using the same position every time and shifting the vector is no good for performance.)

For this problem: inserting 16 bytes into a vector separately is not the most efficient approach. Assembling them in groups of 4 or 8 in integer registers might be a better way to go.

;; b0 .. b15 are whatever addressing mode you want.
;; if you could get more than 1 of b0..b15 with a single vector load (i.e. there is some locality in the source bytes)
;; then DON'T DO THIS: do vector loads and shuffle + combine (pshufb if needed)

movzx  eax, byte [b0]
mov    ah,  byte [b1]   # partial-reg merging after this is free on Haswell, cheap on SnB/IvB, and very slow on CPUs before Sandybridge.
movzx  edx, byte [b2]
mov    dh,  byte [b3]
shl    edx, 16
or     edx, eax
movd   xmm0, edx      # cheaper than pinsrd xmm0, edx, 0.  Also zeros the rest of the vector

movzx  eax, byte [b4]
mov    ah,  byte [b5]
movzx  edx, byte [b6]
mov    dh,  byte [b7]
shl    edx, 16
or     edx, eax
pinsrd xmm0, edx, 1
...
pinsrd xmm0, edx, 2
...
pinsrd xmm0, edx, 3

If you can't assume SSE4, then you could use pinsrw (SSE2). Or maybe it would be better to use movd and shuffle vectors together with PUNPCKLDQ / PUNPCKLDQD. (That link is to an HTML extract from Intel's manuals).

See Agner Fog's Optimizing Assembly guide (and instruction tables/microarch guide) to decide what sequence of instructions would actually be good.



It looks like this is a known issue with the plugin:

"When I flip it to the back, which is the long sidebar, it doesn't expand the container to fill the sidebar. Instead it just overlaps the content..."

You can follow the status on the github issue page.

Maybe you'd be interested in CSS flip tricks instead? Here is a cool example from David Walsh

/* simple */

.flip-container {
  -webkit-perspective: 1000;
  -moz-perspective: 1000;
  -ms-perspective: 1000;
  perspective: 1000;
  -ms-transform: perspective(1000px);
  -moz-transform: perspective(1000px);
  -moz-transform-style: preserve-3d;
  -ms-transform-style: preserve-3d;
  border: 1px solid #ccc;
}
/*
			.flip-container:hover .flipper, .flip-container.hover .flipper, #flip-toggle.flip .flipper {
				-webkit-transform: rotateY(180deg);
				-moz-transform: rotateY(180deg);
				-ms-transform: rotateY(180deg);
				transform: rotateY(180deg);
				filter: FlipH;
    			-ms-filter: "FlipH";
			}
			*/

/* START: Accommodating for IE */

.flip-container:hover .back,
.flip-container.hover .back {
  -webkit-transform: rotateY(0deg);
  -moz-transform: rotateY(0deg);
  -o-transform: rotateY(0deg);
  -ms-transform: rotateY(0deg);
  transform: rotateY(0deg);
}
.flip-container:hover .front,
.flip-container.hover .front {
  -webkit-transform: rotateY(180deg);
  -moz-transform: rotateY(180deg);
  -o-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
/* END: Accommodating for IE */

.flip-container,
.front,
.back {
  width: 320px;
  height: 427px;
}
.flipper {
  -webkit-transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  -ms-transition: 0.6s;
  -moz-transition: 0.6s;
  -moz-transform: perspective(1000px);
  -moz-transform-style: preserve-3d;
  -ms-transform-style: preserve-3d;
  transition: 0.6s;
  transform-style: preserve-3d;
  position: relative;
}
.front,
.back {
  -webkit-backface-visibility: hidden;
  -moz-backface-visibility: hidden;
  -ms-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  -webkit-transform: rotateY(0deg);
  -moz-transition: 0.6s;
  -moz-transform-style: preserve-3d;
  -moz-transform: rotateY(0deg);
  -o-transition: 0.6s;
  -o-transform-style: preserve-3d;
  -o-transform: rotateY(0deg);
  -ms-transition: 0.6s;
  -ms-transform-style: preserve-3d;
  -ms-transform: rotateY(0deg);
  transition: 0.6s;
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  position: absolute;
  top: 0;
  left: 0;
}
.front {
  -webkit-transform: rotateY(0deg);
  -ms-transform: rotateY(0deg);
  background: lightgreen;
  z-index: 2;
}
.back {
  background: lightblue;
  -webkit-transform: rotateY(-180deg);
  -moz-transform: rotateY(-180deg);
  -o-transform: rotateY(-180deg);
  -ms-transform: rotateY(-180deg);
  transform: rotateY(-180deg);
}
.front .name {
  font-size: 2em;
  display: inline-block;
  background: rgba(33, 33, 33, 0.9);
  color: #f8f8f8;
  font-family: Courier;
  padding: 5px 10px;
  border-radius: 5px;
  bottom: 60px;
  left: 25%;
  position: absolute;
  text-shadow: 0.1em 0.1em 0.05em #333;
  display: none;
  -webkit-transform: rotate(-20deg);
  -moz-transform: rotate(-20deg);
  -ms-transform: rotate(-20deg);
  transform: rotate(-20deg);
}
.back-logo {
  position: absolute;
  top: 40px;
  left: 90px;
  width: 160px;
  height: 117px;
  background: url(logo.png) 0 0 no-repeat;
}
.back-title {
  font-weight: bold;
  color: #00304a;
  position: absolute;
  top: 180px;
  left: 0;
  right: 0;
  text-align: center;
  text-shadow: 0.1em 0.1em 0.05em #acd7e5;
  font-family: Courier;
  font-size: 22px;
}
.back p {
  position: absolute;
  bottom: 40px;
  left: 0;
  right: 0;
  text-align: center;
  padding: 0 20px;
  font-size: 18px;
}
/* vertical */

.vertical.flip-container {
  position: relative;
}
.vertical .back {
  -webkit-transform: rotateX(180deg);
  -moz-transform: rotateX(180deg);
  -ms-transform: rotateX(180deg);
  transform: rotateX(180deg);
}
.vertical.flip-container .flipper {
  -webkit-transform-origin: 100% 213.5px;
  -moz-transform-origin: 100% 213.5px;
  -ms-transform-origin: 100% 213.5px;
  transform-origin: 100% 213.5px;
}
/*
			.vertical.flip-container:hover .flipper {
				-webkit-transform: rotateX(-180deg);
				-moz-transform: rotateX(-180deg);
				-ms-transform: rotateX(-180deg);
				transform: rotateX(-180deg);
			}
			*/

/* START: Accommodating for IE */

.vertical.flip-container:hover .back,
.vertical.flip-container.hover .back {
  -webkit-transform: rotateX(0deg);
  -moz-transform: rotateX(0deg);
  -o-transform: rotateX(0deg);
  -ms-transform: rotateX(0deg);
  transform: rotateX(0deg);
}
.vertical.flip-container:hover .front,
.vertical.flip-container.hover .front {
  -webkit-transform: rotateX(180deg);
  -moz-transform: rotateX(180deg);
  -o-transform: rotateX(180deg);
  transform: rotateX(180deg);
}
/* END: Accommodating for IE */
<p>Mouse over the image below to see it flip!</p>

<h2>Styled Card (Horizontal Flip)</h2>
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
  <div class="flipper">
    <div class="front" style="background: url(https://davidwalsh.name/demo/dwflip.jpg) 0 0 no-repeat;">
      <span class="name">David Walsh</span>
    </div>
    <div class="back" style="background:#f8f8f8;">
      <div class="back-logo"></div>
      <div class="back-title">@davidwalshblog</div>
      <p>Mozilla Web Developer, MooTools & jQuery Consultant, MooTools Core Developer, Javascript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</p>
    </div>
  </div>
</div>
<br />
<br />

<h2>Styled Card (Vertical Flip)</h2>
<div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
  <div class="flipper">
    <div class="front" style="background: url(https://davidwalsh.name/demo/dwflip.jpg) 0 0 no-repeat;">
      <span class="name">David Walsh</span>
    </div>
    <div class="back" style="background:#f8f8f8;">
      <div class="back-logo"></div>
      <div class="back-title">@davidwalshblog</div>
      <p>Mozilla Web Developer, MooTools & jQuery Consultant, MooTools Core Developer, Javascript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</p>
    </div>
  </div>
</div>
<br />
<br />


<h2>Toggle Flip (Horizontal)</h2>
<div class="flip-container" id="flip-toggle">
  <div class="flipper">
    <div class="front" style="background: url(https://davidwalsh.name/demo/dwflip.jpg) 0 0 no-repeat;">
      <span class="name">David Walsh</span>
    </div>
    <div class="back" style="background:#f8f8f8;">
      <div class="back-logo"></div>
      <div class="back-title">@davidwalshblog</div>
      <p>Mozilla Web Developer, MooTools & jQuery Consultant, MooTools Core Developer, Javascript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</p>
    </div>
  </div>
</div>
<button onclick="document.querySelector('#flip-toggle').classList.toggle('hover');" class="sexyButton">Toggle Flip</button>
<br />
<br />

<h2>Just The Basics (Horizontal Flip)</h2>
<!-- card -->
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
  <div class="flipper">
    <div class="front">
      front
    </div>
    <div class="back">
      back
    </div>
  </div>
</div>
<br />
<br />



You can add (almost) 180 before the modulus, and subtract it later.

Not exactly 180 because then you would get -180 instead of 180.

var val = 0;
for(var i=0; i<8; ++i) {
  console.log(val = (val + 90 + 179) % 360 - 179);
}

By : Oriol


This video can help you solving your question :)
By: admin