[erlang-questions] Pointless md5 Problem

Thomas Lindgren <>
Thu May 7 10:12:40 CEST 2009






----- Original Message ----
> From: Matthias Lang <>
....
> 2. It might even be worth fossicking around in some of the HIPE
>     group's evil functions for destructive updates and seeing if there's
>     something useful there. Here's a blog post from someone who would
>     appear to approve:
> 
>       http://prog21.dadgum.com/41.html
> 
>     While you're at it, you could also try native compilation.

Counting occurrences of each byte value without messing around? How about ...

count([Byte|Bytes], X0, ..., X255) ->
   case Byte of
      0 -> count(Bytes, X0+1, ..., X255);
      ...
      255 -> count(Bytes, X0, ..., X255+1)
   end;
count([], X0, ..., X255) ->
   {X0, ..., X255}.

Okay, so the code is a terrible 10700 repetitive lines (see below for a taste). But a single clause boils down to

  %%  0 -> count(Bytes, X000+1, ..., X255);
  {label,5}.
    {gc_bif,'+',{f,0},259,[{x,8},{integer,1}],{x,8}}.
    {move,{x,258},{x,0}}.
    {call_only,257,{f,4}}.

One add, updating one register, then move on to the next list element. Pretty close to a byte array, don't you think? Only works as long as you don't do anything foolish, of course.

Best,
Thomas

PS. Here is the source code of the code above:

count([Byte|Bytes],
      X000, X001, X002, X003, X004, X005, X006, X007,
      X010, X011, X012, X013, X014, X015, X016, X017,
      X020, X021, X022, X023, X024, X025, X026, X027,
      X030, X031, X032, X033, X034, X035, X036, X037,
      X040, X041, X042, X043, X044, X045, X046, X047,
      X050, X051, X052, X053, X054, X055, X056, X057,
      X060, X061, X062, X063, X064, X065, X066, X067,
      X070, X071, X072, X073, X074, X075, X076, X077,

      X100, X101, X102, X103, X104, X105, X106, X107,
      X110, X111, X112, X113, X114, X115, X116, X117,
      X120, X121, X122, X123, X124, X125, X126, X127,
      X130, X131, X132, X133, X134, X135, X136, X137,
      X140, X141, X142, X143, X144, X145, X146, X147,
      X150, X151, X152, X153, X154, X155, X156, X157,
      X160, X161, X162, X163, X164, X165, X166, X167,
      X170, X171, X172, X173, X174, X175, X176, X177,

      X200, X201, X202, X203, X204, X205, X206, X207,
      X210, X211, X212, X213, X214, X215, X216, X217,
      X220, X221, X222, X223, X224, X225, X226, X227,
      X230, X231, X232, X233, X234, X235, X236, X237,
      X240, X241, X242, X243, X244, X245, X246, X247,
      X250, X251, X252, X253, X254, X255, X256, X257,
      X260, X261, X262, X263, X264, X265, X266, X267,
      X270, X271, X272, X273, X274, X275, X276, X277,

      X300, X301, X302, X303, X304, X305, X306, X307,
      X310, X311, X312, X313, X314, X315, X316, X317,
      X320, X321, X322, X323, X324, X325, X326, X327,
      X330, X331, X332, X333, X334, X335, X336, X337,
      X340, X341, X342, X343, X344, X345, X346, X347,
      X350, X351, X352, X353, X354, X355, X356, X357,
      X360, X361, X362, X363, X364, X365, X366, X367,
      X370, X371, X372, X373, X374, X375, X376, X377) ->
    case Byte of
        0 ->
            count(Bytes,
                  X000+1, X001, X002, X003, X004, X005, X006, X007,
                  X010, X011, X012, X013, X014, X015, X016, X017,
                  X020, X021, X022, X023, X024, X025, X026, X027,
                  X030, X031, X032, X033, X034, X035, X036, X037,
                  X040, X041, X042, X043, X044, X045, X046, X047,
                  X050, X051, X052, X053, X054, X055, X056, X057,
                  X060, X061, X062, X063, X064, X065, X066, X067,
                  X070, X071, X072, X073, X074, X075, X076, X077,

                  X100, X101, X102, X103, X104, X105, X106, X107,
                  X110, X111, X112, X113, X114, X115, X116, X117,
                  X120, X121, X122, X123, X124, X125, X126, X127,
                  X130, X131, X132, X133, X134, X135, X136, X137,
                  X140, X141, X142, X143, X144, X145, X146, X147,
                  X150, X151, X152, X153, X154, X155, X156, X157,
                  X160, X161, X162, X163, X164, X165, X166, X167,
                  X170, X171, X172, X173, X174, X175, X176, X177,

                  X200, X201, X202, X203, X204, X205, X206, X207,
                  X210, X211, X212, X213, X214, X215, X216, X217,
                  X220, X221, X222, X223, X224, X225, X226, X227,
                  X230, X231, X232, X233, X234, X235, X236, X237,
                  X240, X241, X242, X243, X244, X245, X246, X247,
                  X250, X251, X252, X253, X254, X255, X256, X257,
                  X260, X261, X262, X263, X264, X265, X266, X267,
                  X270, X271, X272, X273, X274, X275, X276, X277,

                  X300, X301, X302, X303, X304, X305, X306, X307,
                  X310, X311, X312, X313, X314, X315, X316, X317,
                  X320, X321, X322, X323, X324, X325, X326, X327,
                  X330, X331, X332, X333, X334, X335, X336, X337,
                  X340, X341, X342, X343, X344, X345, X346, X347,
                  X350, X351, X352, X353, X354, X355, X356, X357,
                  X360, X361, X362, X363, X364, X365, X366, X367,
                  X370, X371, X372, X373, X374, X375, X376, X377);
          ...


      




More information about the erlang-questions mailing list