Quote integer division (quot) by a compile-time known divisor, which generates source code, employing arithmetic and bitwise operations only. This is usually 2.5x-3.5x faster than using normal quot.
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -ddump-splices -ddump-simpl -dsuppress-all #-}
module Example where
import Numeric.QuoteQuot
-- Equivalent to (`quot` 10).
quot10 :: Word -> Word
quot10 = $$(quoteQuot 10)quot10 12312
Here -ddump-splices demonstrates the chosen implementation
for division by 10:
Splicing expression quoteQuot 10 ======>
((`shiftR` 3) . ((\ (W# w_a9N4) ->
let !(# hi_a9N5, _ #) = (timesWord2# w_a9N4) 14757395258967641293##
in W# hi_a9N5) . id))And -ddump-simpl demonstrates generated Core:
quot10 = \ x_a5t2 ->
case x_a5t2 of { W# w_acHY ->
case timesWord2# w_acHY 14757395258967641293## of
{ (# hi_acIg, ds_dcIs #) ->
W# (uncheckedShiftRL# hi_acIg 3#)
}
}Benchmarks show that this implementation is 3.5x faster
than (`quot` 10).