{"id":102780,"date":"2019-08-13T07:00:00","date_gmt":"2019-08-13T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102780"},"modified":"2019-09-13T21:34:47","modified_gmt":"2019-09-14T04:34:47","slug":"20190813-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190813-00\/?p=102780","title":{"rendered":"The SuperH-3, part 7: Bitwise logical operations"},"content":{"rendered":"<p>The SH-3 has a rather basic collection of bitwise logical operations.<\/p>\n<pre>    AND Rm, Rn          ; Rn &amp;= Rm\r\n    AND #imm, r0        ; r0 &amp;= unsigned 8-bit immediate\r\n\r\n    OR  Rm, Rn          ; Rn |= Rm\r\n    OR  #imm, r0        ; r0 |= unsigned 8-bit immediate\r\n\r\n    XOR Rm, Rn          ; Rn ^= Rm\r\n    XOR #imm, r0        ; r0 ^= unsigned 8-bit immediate\r\n\r\n    NOT Rm, Rn          ; Rn = ~Rm\r\n<\/pre>\n<p>Nothing fancy. No <i>nor<\/i> or <i>nand<\/i> or <i>andnot<\/i> or other goofy bitwise operations. Just plain vanilla stuff. Do note that the 8-bit immediate is unsigned here.<\/p>\n<p>There is also an instruction for testing bits wthout modifying anything other than the <var>T<\/var> flag.<\/p>\n<pre>    TST Rm, Rn              ; T = ((Rn &amp; Rm) == 0)\r\n    TST #imm, r0            ; T = ((r0 &amp; signed 8-bit immediate) == 0)\r\n<\/pre>\n<p>The <i>test<\/i> instruction performs a bitwise <i>and<\/i> and compares the result with zero. In this case, the 8-bit immediate is signed.<\/p>\n<p>But wait, there&#8217;s something goofy after all: Load\/modify\/store instructions!<\/p>\n<pre>    AND.B #imm, @(r0, GBR)  ; @(r0 + gbr) &amp;= 8-bit immediate\r\n    OR.B  #imm, @(r0, GBR)  ; @(r0 + gbr) |= 8-bit immediate\r\n    XOR.B #imm, @(r0, GBR)  ; @(r0 + gbr) ^= 8-bit immediate\r\n    TST.B #imm, @(r0, GBR)  ; T = ((@(r0 + gbr) &amp; 8-bit immediate) == 0)\r\n<\/pre>\n<p>These <code>.B<\/code> versions of the bitwise logical operations operate on a byte in memory indexed by the <var>r0<\/var> and <var>gbr<\/var> registers. Okay, so <code>TST.B<\/code> is not a load\/modify\/store; it&#8217;s just a load, but I included it in this group because he wants to be with his friends.<\/p>\n<p>In practice, the Microsoft compiler does not generate these instructions.<\/p>\n<p>Finally, we have this guy, the only truly atomic instruction in the SH-3 instruction set.<\/p>\n<pre>    TAS.B @Rn              ; T = (@Rn == 0), @Rn |= 0x80\r\n<\/pre>\n<p>The <i>test-and-set<\/i> instruction reads a byte from memory, compares it against zero (setting <var>T<\/var> accordingly), and then sets the high bit and writes the result back out. This was clearly designed for building low-level synchronization primitives, but I&#8217;m not sure anybody actually uses it.<\/p>\n<p>I say that it is the only truly atomic operation because it holds the data bus locked for the duration of its operation. The load\/modify\/store instructions we saw above do not lock the bus, so it&#8217;s possible for a coprocessor to modify the memory out from under the SH-3.<\/p>\n<p>That&#8217;s it for the logical operations. <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190814-00\/?p=102782\"> Next up are the bit shifting operations<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just the basic operations, not much fancy.<\/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":[2],"class_list":["post-102780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Just the basic operations, not much fancy.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102780","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=102780"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102780\/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=102780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}