{"id":450,"date":"2007-08-05T23:21:59","date_gmt":"2007-08-06T06:21:59","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/re-math-puzzle\/"},"modified":"2007-08-05T23:34:51","modified_gmt":"2007-08-06T06:34:51","slug":"re-math-puzzle","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/re-math-puzzle\/","title":{"rendered":"RE Math Puzzle"},"content":{"rendered":"<p>It&#8217;s late and it has been a long day, but I&#8217;m still trying to reverse engineer a particular math function. RE is such a wonderful endeavor for those who are keen on bizarre little number puzzles. Check this out and see if you can understand the overall effect:<\/p>\n<ul>\n<li>pick some number, we&#8217;ll call it A<\/li>\n<li>shift A left by 29, effectively multiplying A by 2<sup>29<\/sup><\/li>\n<li>subtract A from the result, assign to B, so now B = A * 2<sup>29<\/sup> &#8211; A<\/li>\n<li>multiply B by 8 and add A, assign to C: C = B * 8 + A<\/li>\n<\/ul>\n<p>What is the net result of the bit shifts and subtractions? I have a feeling that there must be multiple operations combined into one, sort of like when a DCT-based video codec dequantizes and reorders coefficients at the same time. Time for some more math:<\/p>\n<pre>\r\n  C = B * 8 + A\r\n  C = 8 * (A * 2<sup>29<\/sup> - A) + A\r\n  C = 8 * A * 2<sup>29<\/sup> - 8 * A + A\r\n  C = 2<sup>3<\/sup> * 2<sup>29<\/sup> * A - 7 * A\r\n  C = 2<sup>32<\/sup> * A - 7 * A\r\n<\/pre>\n<p>In order to negate an N-bit integer in 2s complement arithmetic, you can subtract that integer from 2<sup>N<\/sup>. For example, to negate 1 as a 32-bit integer: (2<sup>32<\/sup> = 0x100000000) &#8211; 1 = (0xFFFFFFFF = -1). So the algorithm above appears to negate a number while multiplying it by 7 at the same time. These people were resolute to never ever use an actual &#8216;imul&#8217; instruction unless absolutely necessary.<\/p>\n<p>Well, I&#8217;m glad we had that little talk. I can sleep soundly tonight. <em>I just hope this still makes sense to me in the morning.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s late and it has been a long day, but I&#8217;m still trying to reverse engineer a particular math function. RE is such a wonderful endeavor for those who are keen on bizarre little number puzzles. Check this out and see if you can understand the overall effect: pick some number, we&#8217;ll call it A [&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-450","post","type-post","status-publish","format-standard","hentry","category-reverse-engineering"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":0,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}