{"id":2209,"date":"2025-05-22T08:25:18","date_gmt":"2025-05-22T08:25:18","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=2209"},"modified":"2025-05-22T08:25:27","modified_gmt":"2025-05-22T08:25:27","slug":"could-ai-bots-generate-a-c-line-like-this-one-i-0x5f3759df-i-1","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/could-ai-bots-generate-a-c-line-like-this-one-i-0x5f3759df-i-1\/","title":{"rendered":"Could AI Bots Generate a C++ Line like this one: i = 0x5f3759df &#8211; ( i >> 1 );"},"content":{"rendered":"\n<p>Do you know which C++ line of code worth million of dollars?<\/p>\n\n\n\n<p>It was this magic C++ line :&nbsp;<strong>i = 0x5f3759df \u2013 ( i &gt;&gt; 1 );<\/strong><\/p>\n\n\n\n<p>This line was introduced first in the code of Quake to optimize the calculation of the inverse square root.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">float Q_rsqrt(float number)\n{\n  long i;\n  float x2, y;\n  const float threehalfs = 1.5F;\n\n  x2 = number * 0.5F;\n  y  = number;\n  i  = * ( long * ) &amp;y;                       \/\/ evil floating point bit level hacking\n  i  = 0x5f3759df - ( i &gt;&gt; 1 );               \/\/ what the fuck?\n  y  = * ( float * ) &amp;i;\n  y  = y * ( threehalfs - ( x2 * y * y ) );   \/\/ 1st iteration\n  \/\/ y  = y * ( threehalfs - ( x2 * y * y ) );   \/\/ 2nd iteration, this can be removed\n\n  return y;\n}<\/pre><\/div>\n\n\n\n<p><strong>Why optimizing Inverse square root is so important?<\/strong><\/p>\n\n\n\n<p>Inverse square roots find utility in video game graphics, particularly within the realm of 3D game engines. Various aspects of game programming, such as pathfinding, lighting, and reflections, heavily rely on vector normalization, a process that necessitates an inverse square root operation. However, performing inverse square roots, which involve floating-point division, can be computationally costly for processors. <\/p>\n\n\n\n<p>In fast-paced and visually immersive games like Quake III Arena, these computations occur millions of times per second. Therefore, even a slight enhancement in the performance of such calculations could notably augment the speed of graphics computation and ultimately enhance the game\u2019s frame rate. To circumvent the resource-intensive nature of the inverse square root function, the programmers of the id Tech 3 engine devised an exceptionally precise and rapid approximation.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"168\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/04\/image-1.png\" alt=\"\" class=\"wp-image-1704\" style=\"width:596px;height:auto\"\/><\/figure>\n\n\n\n<p>Without this clever optimization, perhaps Quake would not have achieved its status as a benchmark in the gaming industry.<\/p>\n\n\n\n<p><strong>Who is the brilliant mind behind this innovative solution?<\/strong><\/p>\n\n\n\n<p>Rys Sommerfeldt, Senior Manager of the European Game Engineering Team at AMD RTG, launched an&nbsp;<a href=\"https:\/\/www.beyond3d.com\/content\/articles\/8\/\" target=\"_blank\" rel=\"noreferrer noopener\">investigation<\/a>&nbsp;into the function\u2019s origins in 2004. And finally it appears that Greg Walsh is the author. Greg Walsh is a monument in the world of computing. He helped engineer the first WYSIWYG (\u201cwhat you see is what you get\u201d) word processor at Xerox PARC and helped found Ardent Computer. Greg worked closely with Cleve Moler, author of Matlab, while at Ardent and it was Cleve who Greg called the inspiration for the Fast Inverse Square Root function.<\/p>\n\n\n\n<p>This code line is cryptic, clever, and comes with one of the most iconic comments in all of software history. But it raises an increasingly relevant question in the age of AI-powered development:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Could an AI write this? Should it? And if not, why not?<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-short-answer-not-exactly-but-kind-of\">Short answer: <strong>Not exactly. But&#8230; kind of.<\/strong><\/h3>\n\n\n\n<p>AI code generation tools \u2014 like GitHub Copilot, ChatGPT, or Google Gemini \u2014 can absolutely <strong>reproduce<\/strong> this line if prompted with enough context.<\/p>\n\n\n\n<p>But the key word here is <strong>reproduce<\/strong>, not <strong>invent<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-not-let-s-break-it-down\">Why Not? Let\u2019s Break It Down.<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-this-line-wasn-t-discovered-it-was-crafted\">1. <strong>This line wasn\u2019t \u201cdiscovered\u201d \u2014 it was crafted<\/strong><\/h3>\n\n\n\n<p>The constant <code>0x5f3759df<\/code> is a magic number derived through trial, error, and deep understanding of floating point formats (IEEE 754). It took insight into:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Bit-level representations of floats<\/li>\n\n\n\n<li>Binary approximation techniques<\/li>\n\n\n\n<li>Performance bottlenecks in game engines<\/li>\n<\/ul>\n\n\n\n<p>An AI <strong>doesn\u2019t \u201cneed\u201d to understand<\/strong> these things the way humans do \u2014 and without training data showing this exact trick, it wouldn&#8217;t be likely to synthesize it from scratch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-ai-is-excellent-at-pattern-synthesis-not-invention\">2. <strong>AI is excellent at pattern synthesis, not invention<\/strong><\/h3>\n\n\n\n<p>LLMs (large language models) learn from code written by humans. They generalize across examples, but they\u2019re <strong>not inherently creative in the mathematical sense<\/strong>. They:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Combine known concepts<\/li>\n\n\n\n<li>Optimize known structures<\/li>\n\n\n\n<li>Predict probable next tokens<\/li>\n<\/ul>\n\n\n\n<p>Unless the training set contains something <em>like<\/em> this line, it\u2019s unlikely to surface organically in a novel setting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-this-line-is-anti-pattern-in-modern-coding\">3. <strong>This line is anti-pattern in modern coding<\/strong><\/h3>\n\n\n\n<p>From a software engineering perspective, this line is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Non-portable<\/li>\n\n\n\n<li>Unsafe (due to type punning)<\/li>\n\n\n\n<li>Hard to read and maintain<\/li>\n<\/ul>\n\n\n\n<p>Modern AI tools (especially those aligned with &#8220;safe&#8221; or &#8220;clean&#8221; code ideals) are less likely to output this unless explicitly prompted for <em>\u201cfast inverse square root hack\u201d<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-when-ai-can-recreate-it\">When AI <em>Can<\/em> Recreate It<\/h2>\n\n\n\n<p>If you ask ChatGPT something like:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>&#8220;Write the fast inverse square root function from Quake III in C&#8221;<\/p>\n<\/blockquote>\n\n\n\n<p>You\u2019ll likely get:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">float Q_rsqrt(float number) {\n    long i;\n    float x2, y;\n    const float threehalfs = 1.5F;\n\n    x2 = number * 0.5F;\n    y  = number;\n    i  = * ( long * ) &amp;y;                  \n    i  = 0x5f3759df - ( i &gt;&gt; 1 );          \n    y  = * ( float * ) &amp;i;\n    y  = y * ( threehalfs - ( x2 * y * y ) );\n    return y;\n}\n<\/pre><\/div>\n\n\n\n<p>AI tools trained on code from GitHub and StackOverflow <strong>have seen it<\/strong>, and will gladly share it if asked.<\/p>\n\n\n\n<p>But they <strong>won\u2019t invent<\/strong> this on their own as an optimization for <code>1.0f \/ sqrt(x)<\/code> unless their training set includes that hack or they&#8217;re prompted very specifically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-this-tells-us-about-human-vs-ai-creativity\">What This Tells Us About Human vs AI Creativity<\/h2>\n\n\n\n<p>This line \u2014 and the brilliant comment that follows it \u2014 is a reminder of what human developers bring to the table:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Insight<\/strong>: knowing where the cost is and what tricks might fix it<\/li>\n\n\n\n<li><strong>Risk-taking<\/strong>: doing something \u201cevil\u201d because it works<\/li>\n\n\n\n<li><strong>Exploration<\/strong>: finding magic numbers that actually <em>do the job<\/em><\/li>\n<\/ul>\n\n\n\n<p>AI today can assist, translate, and optimize \u2014 but lines like this come from <em>curiosity<\/em> and <em>intuition<\/em>, something we\u2019re still learning to teach machines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>So: <strong>could an AI write from scatch <code>i = 0x5f3759df - (i >> 1);<\/code>?<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Not yet.<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>Until AI can <em>genuinely<\/em> explore floating point internals, performance hacks, and unsafe memory operations <em>from first principles<\/em>, it\u2019s us humans who will keep writing the lines like these ones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know which C++ line of code worth million of dollars? It was this magic C++ line :&nbsp;i = 0x5f3759df \u2013 ( i &gt;&gt; 1 ); This line was introduced first in the code of Quake to optimize the calculation of the inverse square root.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2209","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=2209"}],"version-history":[{"count":5,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2209\/revisions"}],"predecessor-version":[{"id":2215,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2209\/revisions\/2215"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=2209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=2209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=2209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}