<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi list.<div><br></div><div>I started playing with a simple implementation of MD5 (basically straight from the wiki page) </div><div>When the implementation produce the correct output, I did some timing for fun and also</div><div>tried to native compile it. </div><div>To my astonishment I discovered that the code was slower when native compiled.</div><div>I am using R15B01 on mac. Can this be true? And in this case why? </div><div><br></div><div>Note that I do not care to optimize the implementation it self! I am more interested why</div><div>the native compiler produce slower code on this example.</div><div><br></div><div>Thanks</div><div><br></div><div>Tony</div><div><br></div><div><br></div><div>%%%%%%%</div><div><div>-module(md5).</div><div><br></div><div>-compile(export_all).</div><div><br></div><div>k(I) when I >= 0, I < 64 -> %% may construct table!</div><div> trunc(abs(math:sin(I+1)) * (1 bsl 32)).</div><div><br></div><div>r(I) when I >= 0, I < 64 -></div><div> element(I+1,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}).</div><div><br></div><div>hex(Bin) -></div><div> [ element(H+1,{$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$a,$b,$c,$d,$e,$f}) ||</div><div><span class="Apple-tab-span" style="white-space:pre"> </span><<H:4>> <= Bin ].</div><div><br></div><div>md5_test(Input) -></div><div> {hex(md5(Input)), hex(erlang:md5(Input))}.</div><div><br></div><div>md5(List) when is_list(List) -></div><div> md5(list_to_binary(List));</div><div>md5(Bits) when is_bitstring(Bits) -></div><div> Length = bit_size(Bits),</div><div> Pad = (512 - (Length+1+64) rem 512) rem 512,</div><div> Bits1 = <<Bits/bitstring,1:1,0:Pad,Length:64/little>>,</div><div> md5(Bits1,16#67452301,16#efcdab89,16#98badcfe,16#10325476).</div><div><br></div><div>md5(<<>>, H0,H1,H2,H3) -></div><div> <<H0:32/little,H1:32/little,H2:32/little,H3:32/little>>;</div><div>md5(Bits,H0,H1,H2,H3) -></div><div> <<Chunk:64/binary, Rest/binary>> = Bits,</div><div> W = list_to_tuple([ X || <<X:32/little>> <= Chunk ]),</div><div> {A,B,C,D} = md5_(0, W, H0, H1, H2, H3),</div><div> md5(Rest, H0+A,H1+B,H2+C,H3+D).</div><div><br></div><div>md5_(I, W, A, B, C, D) when I < 16 -></div><div> F = (B band C) bor ((bnot B) band D),</div><div> G = I,</div><div> R = rotate(A+F+k(I)+element(G+1,W), r(I)),</div><div> md5_(I+1,W,D,B+R,B,C);</div><div>md5_(I, W, A, B, C, D) when I < 32 -></div><div> F = (D band B) bor ((bnot D) band C),</div><div> G = (5*I + 1) rem 16,</div><div> R = rotate(A+F+k(I)+element(G+1,W), r(I)),</div><div> md5_(I+1,W,D,B+R,B,C);</div><div>md5_(I, W, A, B, C, D) when I < 48 -></div><div> F = (B bxor C) bxor D,</div><div> G = (3*I+5) rem 16,</div><div> R = rotate(A+F+k(I)+element(G+1,W), r(I)),</div><div> md5_(I+1,W,D,B+R,B,C);</div><div>md5_(I, W, A, B, C, D) when I < 64 -></div><div> F = C bxor (B bor (bnot D)),</div><div> G = (7*I) rem 16,</div><div> R = rotate(A+F+k(I)+element(G+1,W), r(I)),</div><div> md5_(I+1,W,D,B+R,B,C);</div><div>md5_(64, _W, A, B, C, D) -></div><div> {A,B,C,D}.</div><div><br></div><div>rotate(X0, C) -></div><div> X = X0 band 16#ffffffff,</div><div> ((X bsl C) bor (X bsr (32-C))) band 16#ffffffff.</div></div><div><br></div><div><br></div><div>%%%%%%%</div><div><div>
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; "><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">"Installing applications can lead to corruption over time. </span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"</span></div><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; "><br></span></div></span><br class="Apple-interchange-newline">
</div>
<br></div></body></html>