{"id":8578,"date":"2025-07-22T20:32:26","date_gmt":"2025-07-22T11:32:26","guid":{"rendered":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=8578"},"modified":"2025-07-22T23:30:13","modified_gmt":"2025-07-22T14:30:13","slug":"gcc-%e3%81%ae%e3%82%a4%e3%83%b3%e3%83%a9%e3%82%a4%e3%83%b3%e3%82%a2%e3%82%bb%e3%83%b3%e3%83%96%e3%83%a9%e3%81%a7%e3%80%81%e3%83%97%e3%83%aa%e3%83%97%e3%83%ad%e3%82%bb%e3%83%83%e3%82%b5%e3%81%ab","status":"publish","type":"post","link":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=8578","title":{"rendered":"GCC \u306e\u30a4\u30f3\u30e9\u30a4\u30f3\u30a2\u30bb\u30f3\u30d6\u30e9\u3067\u3001\u30d7\u30ea\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u3088\u3089\u305a\u306b\u30a2\u30bb\u30f3\u30d6\u30e9\u30d7\u30ed\u30b0\u30e9\u30e0\u4e2d\u306b\u30c8\u30fc\u30af\u30f3\u3092\u57cb\u3081\u8fbc\u3080\u65b9\u6cd5\uff08x86\/x86-64 \u9650\u5b9a\uff09"},"content":{"rendered":"<p>GCC \u306e\u30a4\u30f3\u30e9\u30a4\u30f3\u30a2\u30bb\u30f3\u30d6\u30e9\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u90e8\u5206\u306f\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u3067\u3042\u308b\u305f\u3081\u3001\u30d7\u30ea\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u4f7f\u3063\u3066\u30c8\u30fc\u30af\u30f3\u3092\u57cb\u3081\u8fbc\u3080\u306e\u306f\u96e3\u3057\u3044\u8a71\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p><!--more--><\/p>\n<blockquote>\n<pre>\r\n#define ITEMTYPE \"byte\"\r\n...\r\n    asm(R\"(\r\n        .intel_syntax noprefix\r\n        movzx eax, )\" ITEMTYPE R\"( ptr [rax]\r\n    )\")\r\n<\/pre>\n<\/blockquote>\n<p>\u4e00\u65b9\u3001C++ \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3059\u308b\u5834\u5408\u3001\u3053\u3046\u3044\u3063\u305f\u7f6e\u304d\u63db\u3048\u306f\u30d7\u30ea\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u3088\u3089\u306a\u3044\u65b9\u6cd5\u3067\u3084\u308a\u305f\u3044\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u3084\u308a\u65b9\u304c\u5206\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067 UtVideo \u3067\u306f\u30d7\u30ea\u30d7\u30ed\u30bb\u30c3\u30b5\u3067\u3084\u3063\u3066\u3044\u307e\u3059\uff08<a href=\"https:\/\/github.com\/umezawatakeshi\/utvideo\/blob\/utvideo-23.2.0\/utv_core\/HuffmanCode_x64.cpp#L11-L15\">\u547c\u3073\u51fa\u3057\u5074<\/a>\u3001<a href=\"https:\/\/github.com\/umezawatakeshi\/utvideo\/blob\/utvideo-23.2.0\/utv_core\/HuffmanCode_x64_HuffmanEncode.cpp#L119\">\u5b9f\u88c5\u5074<\/a>\uff09\u3002<\/p>\n<p>\u3067\u3001\u6628\u65e5\u4e45\u3057\u3076\u308a\u306b\u8abf\u3079\u3066\u3044\u305f\u3089\u3001\u3068\u308a\u3042\u3048\u305a x86\/x86-64 \u3067\u3067\u304d\u308b\u30c0\u30fc\u30c6\u30a3\u30fc\u30cf\u30c3\u30af\u3092\u898b\u3064\u3051\u307e\u3057\u305f\u3002<\/p>\n<blockquote>\n<pre>\r\n$ cat aaa.cc\r\nstatic constexpr char byte_[0] asm(\"<strong>byte<\/strong>\") = {};\r\n\r\nvoid foo()\r\n{\r\n    asm(R\"(\r\n        .intel_syntax noprefix\r\n        movzx eax, %p[aaa] ptr [rax]\r\n    )\":: [aaa]\"i\"(byte_));\r\n}\r\n$ gcc -S -o - -std=c++11 -masm=intel aaa.cc\r\n        .file   \"aaa.cc\"\r\n        .intel_syntax noprefix\r\n        .text\r\n        .section        .rodata\r\n        .type   byte, @object\r\n        .size   byte, 0\r\nbyte:\r\n        .text\r\n        .globl  _Z3foov\r\n        .type   _Z3foov, @function\r\n_Z3foov:\r\n.LFB0:\r\n        .cfi_startproc\r\n        endbr64\r\n        push    rbp\r\n        .cfi_def_cfa_offset 16\r\n        .cfi_offset 6, -16\r\n        mov     rbp, rsp\r\n        .cfi_def_cfa_register 6\r\n#APP\r\n# 5 \"aaa.cc\" 1\r\n\r\n        .intel_syntax noprefix\r\n        movzx rax, <strong>byte<\/strong> ptr [rax]\r\n\r\n# 0 \"\" 2\r\n#NO_APP\r\n        nop\r\n        pop     rbp\r\n        .cfi_def_cfa 7, 8\r\n        ret\r\n        .cfi_endproc\r\n.LFE0:\r\n        .size   _Z3foov, .-_Z3foov\r\n        .ident  \"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0\"\r\n        .section        .note.GNU-stack,\"\",@progbits\r\n        .section        .note.gnu.property,\"a\"\r\n        .align 8\r\n        .long   1f - 0f\r\n        .long   4f - 1f\r\n        .long   5\r\n0:\r\n        .string \"GNU\"\r\n1:\r\n        .align 8\r\n        .long   0xc0000002\r\n        .long   3f - 2f\r\n2:\r\n        .long   0x3\r\n3:\r\n        .align 8\r\n4:\r\n<\/pre>\n<\/blockquote>\n<p><code>byte_<\/code> \u3068\u3044\u3046\u540d\u524d\u306e\u5909\u6570\u306e\u5f8c\u308d\u306b\u3064\u3044\u3066\u3044\u308b <code>asm<\/code> \u306f\u30a2\u30bb\u30f3\u30d6\u30e9\u30ec\u30d9\u30eb\u3067\u306e\u30b7\u30f3\u30dc\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b\u8a18\u6cd5\u3067\u3001\u3053\u308c\u304c\u57cb\u3081\u8fbc\u307f\u305f\u3044\u6587\u5b57\u5217\u306b\u306a\u308a\u307e\u3059\uff08C \u3067\u306e\u5909\u6570\u540d\u306f\u4f55\u3067\u3082\u3044\u3044\uff09\u3002\u3053\u306e\u5909\u6570\uff08\u306e\u5148\u982d\u30a2\u30c9\u30ec\u30b9\u306e\u5024\uff09\u3092 <code>\"i\"<\/code> constraint \u3067 input operand \u3068\u3057\u3066\u6e21\u3057\u3001\u751f\u306e\u30b7\u30f3\u30dc\u30eb\u540d\u3092\u51fa\u529b\u3059\u308b\u305f\u3081\u306e <code>p<\/code> modifier \u3092\u4ed8\u3051\u3066\u53c2\u7167\u3055\u305b\u308b\u3068\u3001\u30aa\u30da\u30e9\u30f3\u30c9\u3068\u3057\u3066\u6307\u5b9a\u3057\u305f\u5024\u306b\u5bfe\u5fdc\u3059\u308b\u30b7\u30f3\u30dc\u30eb\u540d\u304c\u305d\u3053\u306b\u57cb\u3081\u8fbc\u307e\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u5229\u7528\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>\r\n$ cat bbb.cc\r\n#include &lt;stdint.h&gt;\r\n\r\nstatic constexpr char byte_[0] asm(\"byte\") = {};\r\nstatic constexpr char word_[0] asm(\"word\") = {};\r\n\r\ntemplate&lt;typename T&gt;\r\nvoid bar()\r\n{\r\n        static_assert(sizeof(T) == 1 || sizeof(T) == 2);\r\n\r\n        asm (R\"(\r\n                .intel_syntax noprefix\r\n                movzx rax, %p[aaa] ptr [rax]\r\n        )\"::[aaa]\"i\"((sizeof(T) == 1) ? byte_ : word_));\r\n}\r\n\r\ntemplate void bar&lt;uint8_t&gt;();\r\ntemplate void bar&lt;uint16_t&gt;();\r\n$ gcc -c -o bbb.o -std=c++11 -masm=intel bbb.cc\r\n$ objdump -d -Mintel bbb.o\r\n\r\nbbb.o:     file format elf64-x86-64\r\n\r\n\r\nDisassembly of section .text._Z3barIhEvv:\r\n\r\n0000000000000000 <_Z3barIhEvv>:\r\n   0:   f3 0f 1e fa             endbr64\r\n   4:   55                      push   rbp\r\n   5:   48 89 e5                mov    rbp,rsp\r\n   8:   48 0f b6 00             movzx  rax,BYTE PTR [rax]\r\n   c:   90                      nop\r\n   d:   5d                      pop    rbp\r\n   e:   c3                      ret\r\n\r\nDisassembly of section .text._Z3barItEvv:\r\n\r\n0000000000000000 <_Z3barItEvv>:\r\n   0:   f3 0f 1e fa             endbr64\r\n   4:   55                      push   rbp\r\n   5:   48 89 e5                mov    rbp,rsp\r\n   8:   48 0f b7 00             movzx  rax,WORD PTR [rax]\r\n   c:   90                      nop\r\n   d:   5d                      pop    rbp\r\n   e:   c3                      ret\r\n<\/pre>\n<\/blockquote>\n<p>\u30b7\u30f3\u30dc\u30eb\u3092\u57cb\u3081\u8fbc\u3080\u305f\u3081\u306e constexpr \u5909\u6570\u305d\u306e\u3082\u306e\u306f\u4f55\u3067\u3082\u3044\u3044\u306e\u3067\u3001\u30b5\u30a4\u30ba\u304c\u30bc\u30ed\u306e\u914d\u5217\u306b\u3057\u307e\u3059\u3002\u3053\u3046\u3059\u308b\u3068\u30b7\u30f3\u30dc\u30eb\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u304c\u30c7\u30fc\u30bf\u306f\u4f55\u3082\u51fa\u529b\u3055\u308c\u306a\u3044\u306e\u3067\u3001\u6700\u7d42\u7684\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5927\u304d\u304f\u306a\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3042\u3068\u3001constexpr \u5909\u6570\u304c static \u3068\u306f\u3044\u3048\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u6301\u3064\u306e\u304c\u5acc\u3060\u304b\u3089\u3068\u3044\u3063\u3066\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u306b\u3057\u305f\u5834\u5408\u3001<code>asm<\/code> \u3067\u6307\u5b9a\u3057\u305f\u540d\u524d\u306b\u30d7\u30ec\u30d5\u30a3\u30c3\u30af\u30b9\u3084\u30b5\u30d5\u30a3\u30c3\u30af\u30b9\u304c\u4ed8\u3044\u3066\u3057\u307e\u3046\u306e\u3067\u30c0\u30e1\u3067\u3059\u3002GCC \u3068 Clang \u3067\u3053\u3053\u306e\u6319\u52d5\u304c\u3061\u3087\u3063\u3068\u9055\u3044\u307e\u3059\u304c\u3001\u306a\u3093\u304b\u4ed8\u3044\u3061\u3083\u3063\u3066\u30c0\u30e1\u306a\u306e\u306f\u540c\u3058\u3067\u3059\u3002<\/p>\n<p><code>p<\/code> modifier \u306f\u3069\u3046\u3084\u3089 x86\/x86-64 \u306b\u3057\u304b\u306a\u3044\u305f\u3081\u3001\u4ed6\u306e\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3067\u540c\u3058\u3053\u3068\u306f\u305f\u3076\u3093\u3067\u304d\u307e\u305b\u3093\u3002\u6c4e\u7528\u6027\u306e\u3042\u308b\u5229\u7528\u6cd5\u304c\u3042\u308b\u306e\u306a\u3089\u4ed6\u306e\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306b\u3082\u3042\u3063\u3066\u826f\u3055\u305d\u3046\u306a\u306e\u3067\u3059\u304c\u305d\u3046\u3044\u3046\u3053\u3068\u306f\u306a\u304f\u3001\u3067\u306f\u3069\u3046\u3044\u3046\u5229\u7528\u6cd5\u304c\u60f3\u5b9a\u3055\u308c\u3066\u308b\u3093\u3067\u3059\u304b\u306d\u2026\uff1f<\/p>\n<p>\u53c2\u8003\u6587\u732e: <a href=\"https:\/\/stackoverflow.com\/questions\/66576823\/is-it-possible-to-template-the-asm-opcode-in-c\">Is it possible to template the asm opcode in c++? &#8211; Stack Overflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GCC \u306e\u30a4\u30f3\u30e9\u30a4\u30f3\u30a2\u30bb\u30f3\u30d6\u30e9\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u90e8\u5206\u306f\u6587\u5b57\u5217\u30ea\u30c6\u30e9\u30eb\u3067\u3042\u308b\u305f\u3081\u3001\u30d7\u30ea\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u4f7f\u3063\u3066\u30c8\u30fc\u30af\u30f3\u3092\u57cb\u3081\u8fbc\u3080\u306e\u306f\u96e3\u3057\u3044\u8a71\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-8578","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8578"}],"version-history":[{"count":10,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8578\/revisions"}],"predecessor-version":[{"id":8588,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/8578\/revisions\/8588"}],"wp:attachment":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8578"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}