{"id":1182,"date":"2010-07-08T16:00:20","date_gmt":"2010-07-08T23:00:20","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1182"},"modified":"2011-08-07T16:42:27","modified_gmt":"2011-08-07T23:42:27","slug":"vc-inline-assembly-hack","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/vc-inline-assembly-hack\/","title":{"rendered":"VC++ inline assembly hack"},"content":{"rendered":"<p>Here's a cute little hack I came up with when doing some fixed-point arithmetic stuff in Visual C++ a while ago. I wanted to make a templated C++ class with some inline assembly, the generation of which depends on a numeric template parameter. Doing the obvious thing doesn't work:<\/p>\n<pre lang=\"cpp\">template<int N> class ArithmeticHelper<N>\r\n{\r\npublic:\r\n    static Int5 MultiplyShiftRight(Int5 x, Int5 y)\r\n    {\r\n        __asm {\r\n            mov eax, x\r\n            imul y\r\n            shrd eax, edx, N\r\n        }\r\n    }\r\n};<\/pre>\n<p>However, one can introduce numeric parameters into inline assembly code by pretending that they are the length of something, and using the \"length\" operator:<\/p>\n<pre lang=\"cpp\">template<int N> class ArithmeticHelper<N>\r\n{\r\nprivate:\r\n    static const int nn[N];  \/\/ VC's inline asm doesn't allow \"shl eax, N\" directly, but this works...\r\npublic:\r\n    static Int5 MultiplyShiftRight(Int5 x, Int5 y)\r\n    {\r\n        __asm {\r\n            mov eax, x\r\n            imul y\r\n            shrd eax, edx, length nn\r\n        }\r\n    }\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here's a cute little hack I came up with when doing some fixed-point arithmetic stuff in Visual C++ a while ago. I wanted to make a templated C++ class with some inline assembly, the generation of which depends on a numeric template parameter. Doing the obvious thing doesn't work: template class ArithmeticHelper { public: static [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1182","post","type-post","status-publish","format-standard","hentry","category-computer"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=1182"}],"version-history":[{"count":3,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1312,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1182\/revisions\/1312"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}