{"id":126,"date":"2024-06-23T23:43:10","date_gmt":"2024-06-23T23:43:10","guid":{"rendered":"https:\/\/gabriel.marcanobrady.family\/blog\/?p=126"},"modified":"2024-06-23T23:43:10","modified_gmt":"2024-06-23T23:43:10","slug":"i-_want_io_long_long-or-sometimes-newlib-doesnt-want-long-long-integer-types","status":"publish","type":"post","link":"https:\/\/gabriel.marcanobrady.family\/blog\/2024\/06\/23\/i-_want_io_long_long-or-sometimes-newlib-doesnt-want-long-long-integer-types\/","title":{"rendered":"I _WANT_IO_LONG_LONG, or sometimes Newlib doesn&#8217;t want long long integer types"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I ran into an issue when trying to finish some implementation work for a paper that I have due in less than a week. Effectively, I was just trying to <code>printf<\/code> some long long types, and they were getting butchered. I reduced a reproducer on my Cortex-M4 device to the following line:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nprintf(&quot;%llu %ld\\n&quot;, 1llu, 2l);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What I expected to see:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n1 2\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What I saw:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n0 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Ok&#8230; that&#8217;s weird. I did a quick google search, and the summary is that if you&#8217;re using newlib nano, well, newlib doesn&#8217;t support long long types in printf and its ilk. Well, I&#8217;m <em><strong>not<\/strong><\/em> using newlib nano for this program. Thinking about it a bit harder, this really looks like <code>printf<\/code> is only reading 4 bytes for the <code>%llu<\/code> type. I finally decided to go read the source code in newlib for some clues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reading through the <code>printf<\/code> implementation eventually <a href=\"https:\/\/github.com\/bminor\/newlib\/blob\/26f7004bf73c421c3fd5e5a6ccf470d05337b435\/newlib\/libc\/stdio\/vfprintf.c#L1140\">led me here<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#ifndef _NO_LONGLONG\n\t\t\tif (flags &amp; QUADINT)\n\t\t\t\t*GET_ARG (N, ap, quad_ptr_t) = ret;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">OK, so there&#8217;s a check for this <code>_NO_LONGLONG<\/code> define. Where can it get set? Oh, <a href=\"https:\/\/github.com\/bminor\/newlib\/blob\/26f7004bf73c421c3fd5e5a6ccf470d05337b435\/newlib\/libc\/stdio\/vfprintf.c#L170C1-L173C21\">apparently in the same file<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define _NO_LONGLONG\n#if defined _WANT_IO_LONG_LONG \\\n\t&amp;&amp; (defined __GNUC__ || __STDC_VERSION__ &gt;= 199901L)\n# undef _NO_LONGLONG\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">OK, so it&#8217;s looking to see if <code>_WANT_IO_LONG_LONG<\/code> is defined. Apparently this is a configuration option, specifically <code>--enable-newlib-io-long-long<\/code>. Rebuilding newlib with that option finally lets <code>printf<\/code> work as intended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reading through the repository now that I had a configuration flag handy, I was able to find it in the README, where it states that this flag is disabled by default, but some hosts enable it in their configure.host files. Just something to keep in mind, I guess, when using newlib.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For those Gentoo users like me, to build newlib with this option, add <code>EXTRA_ECONF=\"--enable-newlib-io-long-long\"<\/code> to an <code>\/etc\/portage\/env\/<\/code> file and have your <code>cross-*\/newlib<\/code> of choice use it in <code>\/etc\/portage\/packages.env\/<\/code> .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into an issue when trying to finish some implementation work for a paper that I have due in less than a week. Effectively, I was just trying to printf some long long types, and they were getting butchered. I reduced a reproducer on my Cortex-M4 device to the following line: What I expected [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,3],"tags":[18],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-embedded-systems-2","category-engineering","tag-newlib"],"_links":{"self":[{"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":2,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gabriel.marcanobrady.family\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}