Fastest saturating add?

amindfv--- via Haskell-Cafe <[email protected]>
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <[email protected]>
I have a function that's called many (many) times, which involves a saturating addition (addition without overflow).

In other words, I'd like ((254 :: Word8) + 10) to equal 255 (which is maxBound::Word8), not 8 (which is what you get with overflow).

My current code, without particular regard to performance, is simply:

    satAdd :: Word8 -> Word8 -> Word8
    satAdd x y =
       let m = x + y
       in if m < x then 255 else m

This seems like the type of code somebody's already worked on the performance for, though I can't find much Haskell-specific in searches online.

Is there a faster way to do this?

Thanks!
Tom
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.