Last active
January 1, 2016 11:49
-
-
Save haf/8140280 to your computer and use it in GitHub Desktop.
CRC32 in succinct F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let IEEE = 0xedb88320u | |
/// The seed value default: all ones, CRC depends fully on its input. | |
let seed = 0xffffffffu | |
let inline (!!) v = v ^^^ 0xFFFFFFFFu | |
let crc_table = Array.init 256 (fun i -> | |
(uint32 i, [0..7]) | |
||> List.fold (fun value _ -> | |
match value &&& 1u with | |
| 0u -> value >>> 1 | |
| _ -> (value >>> 1) ^^^ IEEE)) | |
let step state buffer = | |
(state, buffer) | |
||> Array.fold (fun crc byt -> crc_table.[int(byt ^^^ byte crc)] ^^^ (crc >>> 8)) | |
let finalise (state : uint32) : byte [] = | |
!! state |> BigEndian.uint32_to_bytes_mem | |
let single_step = finalise << step seed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment