{"id":43173,"date":"2015-01-12T07:00:00","date_gmt":"2015-01-12T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/01\/12\/limiting-the-bottom-byte-of-an-xmm-register-and-clearing-the-other-bytes\/"},"modified":"2019-03-13T12:11:57","modified_gmt":"2019-03-13T19:11:57","slug":"20150112-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150112-00\/?p=43173","title":{"rendered":"Limiting the bottom byte of an XMM register and clearing the other bytes"},"content":{"rendered":"<p>Suppose you have a value in an XMM register and you want to limit the bottom byte to a particular value <i>and<\/i> set all the other bytes to zero. (Yes, I needed to do this.) <\/p>\n<p>One way to do this is to apply the two steps in sequence: <\/p>\n<pre>\n; value to truncate\/limit is in xmm0\n\n; First, zero out the top 15 bytes\n    pslldq  xmm0, 15\n    psrldq  xmm0, 15\n\n; Now limit the bottom byte to N\n    mov     al, N\n    movd    xmm1, eax\n    pminub  xmm0, xmm1\n<\/pre>\n<p>But you can do it all in one step by realizing that min(<var>x<\/var>, 0) = 0 for all unsigned values <var>x<\/var>. <\/p>\n<pre>\n; value to truncate\/limit is in xmm0\n    mov     eax, N\n    movd    xmm1, eax\n    pminub  xmm0, xmm1\n<\/pre>\n<p>In pictures: <\/p>\n<table BORDER=\"0\" STYLE=\"border-collapse: collapse;text-align: center\">\n<tr>\n<td>xmm0<\/td>\n<td><\/td>\n<td>xmm1<\/td>\n<td><\/td>\n<td>xmm0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black;width: 2em\">?<\/td>\n<td STYLE=\"padding: 0px 1ex\">min<\/td>\n<td STYLE=\"border: solid 1px black;width: 2em\">0<\/td>\n<td STYLE=\"padding: 0px 1ex\">=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\">?<\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">0<\/td>\n<\/tr>\n<tr>\n<td STYLE=\"border: solid 1px black\"><var>x<\/var><\/td>\n<td>min<\/td>\n<td STYLE=\"border: solid 1px black\"><var>N<\/var><\/td>\n<td>=<\/td>\n<td STYLE=\"border: solid 1px black\">min(<var>x<\/var>, <var>N<\/var>)<\/td>\n<\/tr>\n<\/table>\n<p>In intrinsics: <\/p>\n<pre>\n__m128i min_low_byte_and_set_upper_bytes_to_zero(__m128i x, uint8_t N)\n{\n return _mm_min_epi8(x, _mm_cvtsi32_si128(N));\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>More SSE tweaking.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-43173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>More SSE tweaking.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=43173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=43173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}