summaryrefslogtreecommitdiff
path: root/src/Helium/Instructions/Instances/Barrett.agda
blob: 606a9e967886ab35d207ce4e9586f17a6b015d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
------------------------------------------------------------------------
-- Agda Helium
--
-- Implementation of Barrett reduction.
------------------------------------------------------------------------

{-# OPTIONS --safe --without-K #-}

module Helium.Instructions.Instances.Barrett where

open import Data.Bool using (false)
open import Data.Sum using (inj₁)
open import Data.Vec using ([])
open import Data.Vec.Relation.Unary.All using ([])
open import Helium.Instructions.Base
open import Helium.Instructions.Core

-- Given:
--   m     = ⌊ (1 << l) * n⁻¹ ⌋
--   -n    = n
--   n     < 2 ^ 32
--   | z | < 2 ^ 31
-- Computes:
--   z mod n
barret : (m -n : Expression [] (bits 32)) (t z : VecReg) (im : GenReg) → Procedure []
barret m -n t z im =
  index R (lit (im ′f)) ≔ m ∙
  invoke vqrdmulh-s32,t,z,m [] ∙
  index R (lit (im ′f)) ≔ -n ∙
  invoke vmla-s32,z,t,-n [] ∙end
  where
  vqrdmulh-s32,t,z,m =
    ExecBeats (vqrdmulh (record
      { size = 32bit
      ; dest = t
      ; src₁ = z
      ; src₂ = inj₁ im
      }))
  vmla-s32,z,t,-n =
    ExecBeats (vmla (record
      { size     = 32bit
      ; unsigned = false
      ; acc      = z
      ; src₁     = t
      ; src₂     = im
      }))