Skip to content

Instantly share code, notes, and snippets.

@haf
Last active January 1, 2016 11:49
Show Gist options
  • Save haf/8140280 to your computer and use it in GitHub Desktop.
Save haf/8140280 to your computer and use it in GitHub Desktop.
CRC32 in succinct F#
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