Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:04+00:00 2026-05-26T20:37:04+00:00

Still working on my SHA1 implementation in Haskell. I’ve now got a working implementation

  • 0

Still working on my SHA1 implementation in Haskell. I’ve now got a working implementation and this is the inner loop:

iterateBlock' :: Int -> [Word32] -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> [Word32]
iterateBlock' 80 ws a b c d e    = [a, b, c, d, e]
iterateBlock' t (w:ws) a b c d e = iterateBlock' (t+1) ws a' b' c' d' e'
    where
    a' = rotate a 5 + f t b c d + e + w + k t
    b' = a
    c' = rotate b 30
    d' = c
    e' = d

The profiler tells me that this function takes 1/3 of the runtime of my implementation. I can think of no way to further optimize it other than maybe inlining the temp variables but I believe -O2 will do that for me anyway.

Can anyone see a significant optimization that can be further applied?

FYI the k and f calls are below. They are so simple I don’t think there is a way to optimize these other. Unless the Data.Bits module is slow?

f :: Int -> Word32 -> Word32 -> Word32 -> Word32
f t b c d
    | t <= 19   = (b .&. c) .|. ((complement b) .&. d)
    | t <= 39   = b `xor` c `xor` d
    | t <= 59   = (b .&. c) .|. (b .&. d) .|. (c .&. d)
    | otherwise = b `xor` c `xor` d

k :: Int -> Word32
k t
    | t <= 19   = 0x5A827999
    | t <= 39   = 0x6ED9EBA1
    | t <= 59   = 0x8F1BBCDC
    | otherwise = 0xCA62C1D6
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:37:05+00:00Added an answer on May 26, 2026 at 8:37 pm

    Looking at the core produced by ghc-7.2.2, the inlining works out well. What doesn’t work so well is that in each iteration a couple of Word32 values are first unboxed, to perform the work, and then reboxed for the next iteration. Unboxing and re-boxing can cost a surprisingly large amount of time (and allocation).
    You can probably avoid that by using Word instead of Word32. You couldn’t use rotate from Data.Bits then, but would have to implement it yourself (not hard) to have it work also on 64-bit systems. For a' you would have to manually mask out the high bits.

    Another point that looks suboptimal is that in each iteration t is compared to 19, 39 and 59 (if it’s large enough), so that the loop body contains four branches. It will probably be faster if you split iterateBlock' into four loops (0-19, 20-39, 40-59, 60-79) and use constants k1, …, k4, and four functions f1, …, f4 (without the t parameter) to avoid branches and have smaller code-size for each loop.

    And, as Thomas said, using a list for the block data isn’t optimal, an unboxed Word array/vector would probably help too.

    With the bang patterns, the core looks much better. Two or three less-than-ideal points remain.

                          (GHC.Prim.narrow32Word#
                             (GHC.Prim.plusWord#
                                (GHC.Prim.narrow32Word#
                                   (GHC.Prim.plusWord#
                                      (GHC.Prim.narrow32Word#
                                         (GHC.Prim.plusWord#
                                            (GHC.Prim.narrow32Word#
                                               (GHC.Prim.plusWord#
                                                  (GHC.Prim.narrow32Word#
                                                     (GHC.Prim.or#
                                                        (GHC.Prim.uncheckedShiftL# sc2_sEn 5)
                                                        (GHC.Prim.uncheckedShiftRL# sc2_sEn 27)))
                                                  y#_aBw))
                                            sc6_sEr))
                                      y#1_XCZ))
                                y#2_XD6))
    

    See all these narrow32Word#? They’re cheap, but not free. Only the outermost is needed, there may be a bit to harvest by hand-coding the steps and using Word.

    Then the comparisons of t with 19, …, they appear twice, once to determine the k constant, and once for the f transform. The comparisons alone are cheap, but they cause branches and without them, further inlining may be possible. I expect a bit could be gained here too.

    And still, the list. That means w can’t be unboxed, the core could be simpler if w were unboxable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Still working on lisp recipes and idioms. I have a list like this: ((a
I'm still working with this huge list of URLs, all the help I have
Still working my way through this program. Next task on my to-do list is
I'm still working on my first Grails application. This time, my problem is to
Still working my way around this, but is there any function/command I can use
I'm still working on that bitmap I/O problem from a week ago. I got
Still working on this part of my script to parse my JSON jsonArray =
I'm still working on this database for a small retail store (a scenario, thankfully!)
Hopefully this is the last of many questions about triggers! Still working with the
Ok, referring back to my previous question, I am still working on learning haskell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.