{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
{- |
Signal generators that generate the signal in chunks
that can be processed natively by the processor.
Some of the functions for plain signals can be re-used without modification.
E.g. rendering a signal and reading from and to signals work
because the vector type as element type warrents correct alignment.
We can convert between atomic and chunked signals.

The article
<http://perilsofparallel.blogspot.com/2008/09/larrabee-vs-nvidia-mimd-vs-simd.html>
explains the difference between Vector and SIMD computing.
According to that the SSE extensions in Intel processors
must be called Vector computing.
But since we use the term Vector already in the mathematical sense,
I like to use the term "packed" that is used in Intel mnemonics like mulps.
-}
module Synthesizer.LLVM.Generator.SignalPacked (
   pack, packRotate,
   packSmall,
   unpack, unpackRotate,
   constant,
   exponential2,
   exponentialBounded2,
   osciCore,
   osci,
   parabolaFadeInInf, parabolaFadeOutInf,
   rampInf, rampSlope,
   noise,
   noiseCore, noiseCoreAlt,
   ) where

import qualified Synthesizer.LLVM.Causal.Process as Causal
import qualified Synthesizer.LLVM.Generator.Private as Priv
import qualified Synthesizer.LLVM.Generator.Core as Core
import qualified Synthesizer.LLVM.Generator.Signal as Sig
import qualified Synthesizer.LLVM.Frame.SerialVector.Class as SerialClass
import qualified Synthesizer.LLVM.Frame.SerialVector.Code as SerialCode
import qualified Synthesizer.LLVM.Frame.SerialVector as Serial
import qualified Synthesizer.LLVM.Random as Rnd

import Synthesizer.Causal.Class (($*))

import qualified LLVM.DSL.Expression as Expr
import LLVM.DSL.Expression (Exp)

import qualified LLVM.Extra.Multi.Vector as MultiVector
import qualified LLVM.Extra.Multi.Value.Marshal as Marshal
import qualified LLVM.Extra.Multi.Value.Vector as MultiValueVec
import qualified LLVM.Extra.Multi.Value as MultiValue
import qualified LLVM.Extra.Memory as Memory
import qualified LLVM.Extra.MaybeContinuation as Maybe
import qualified LLVM.Extra.Control as U
import qualified LLVM.Extra.Arithmetic as A
import qualified LLVM.Extra.Tuple as Tuple

import qualified Type.Data.Num.Decimal as TypeNum
import Type.Data.Num.Decimal ((:*:))

import qualified LLVM.Core as LLVM

import qualified Control.Monad.Trans.Class as MT
import qualified Control.Monad.Trans.State as MS
import Control.Monad.HT ((<=<))
import Control.Monad (replicateM)
import Control.Applicative ((<$>))

import qualified Algebra.Ring as Ring

import Data.Tuple.HT (mapSnd)
import Data.Word (Word32, Word)
import Data.Int (Int32)

import NumericPrelude.Numeric
import NumericPrelude.Base



{- |
Convert a signal of scalar values into one using processor vectors.
If the signal length is not divisible by the chunk size,
then the last chunk is dropped.
-}
pack, packRotate ::
   (SerialClass.Write v, a ~ SerialClass.Element v) =>
   Sig.T a -> Sig.T v
pack :: forall v a. (Write v, a ~ Element v) => T a -> T v
pack = T a -> T v
forall v a. (Write v, a ~ Element v) => T a -> T v
packRotate

packRotate :: forall v a. (Write v, a ~ Element v) => T a -> T v
packRotate (Priv.Cons forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (a, state)
next forall r. CodeGenFunction r (global, state)
start forall r. global -> CodeGenFunction r ()
stop) = (forall r c.
 Phi c =>
 global -> Value (Ptr local) -> state -> T r c (v, state))
-> (forall r. CodeGenFunction r (global, state))
-> (forall r. global -> CodeGenFunction r ())
-> T v
forall a global local state.
(C global, IsSized local, C state) =>
(forall r c.
 Phi c =>
 global -> Value (Ptr local) -> state -> T r c (a, state))
-> (forall r. CodeGenFunction r (global, state))
-> (forall r. global -> CodeGenFunction r ())
-> T a
Priv.Cons
   (\global
global Value (Ptr local)
local state
s -> do
      Iterator WriteMode (WriteIt v) v
wInit <- CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
-> T r c (Iterator WriteMode (WriteIt v) v)
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
 -> T r c (Iterator WriteMode (WriteIt v) v))
-> CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
-> T r c (Iterator WriteMode (WriteIt v) v)
forall a b. (a -> b) -> a -> b
$ CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
forall r. CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
forall v r.
Write v =>
CodeGenFunction r (WriteIterator (WriteIt v) v)
SerialClass.writeStart
      (Iterator WriteMode (WriteIt v) v
w2,Value Word
_,state
s2) <-
         CodeGenFunction
  r
  (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
-> T r c (Iterator WriteMode (WriteIt v) v, Value Word, state)
forall z r a. Phi z => CodeGenFunction r (Value Bool, a) -> T r z a
Maybe.fromBool (CodeGenFunction
   r
   (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
 -> T r c (Iterator WriteMode (WriteIt v) v, Value Word, state))
-> CodeGenFunction
     r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
-> T r c (Iterator WriteMode (WriteIt v) v, Value Word, state)
forall a b. (a -> b) -> a -> b
$
         (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
-> ((Value Bool,
     (Iterator WriteMode (WriteIt v) v, Value Word, state))
    -> CodeGenFunction r (Value Bool))
-> ((Value Bool,
     (Iterator WriteMode (WriteIt v) v, Value Word, state))
    -> CodeGenFunction
         r
         (Value Bool,
          (Iterator WriteMode (WriteIt v) v, Value Word, state)))
-> CodeGenFunction
     r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
forall a r.
Phi a =>
a
-> (a -> CodeGenFunction r (Value Bool))
-> (a -> CodeGenFunction r a)
-> CodeGenFunction r a
U.whileLoop
            (Bool -> Value Bool
forall a. IsConst a => a -> Value a
LLVM.valueOf Bool
True,
             (Iterator WriteMode (WriteIt v) v
wInit,
              Word -> Value Word
forall a. IsConst a => a -> Value a
LLVM.valueOf (Word -> Value Word) -> Word -> Value Word
forall a b. (a -> b) -> a -> b
$ (Iterator WriteMode (WriteIt v) v -> Word
forall v i mode it.
(Sized v, Integral i) =>
Iterator mode it v -> i
SerialClass.sizeOfIterator Iterator WriteMode (WriteIt v) v
wInit :: Word),
              state
s))
            (\(Value Bool
cont,(Iterator WriteMode (WriteIt v) v
_w0,Value Word
i0,state
_s0)) ->
               Value Bool -> Value Bool -> CodeGenFunction r (Value Bool)
forall a r. Logic a => a -> a -> CodeGenFunction r a
forall r.
Value Bool -> Value Bool -> CodeGenFunction r (Value Bool)
A.and Value Bool
cont (Value Bool -> CodeGenFunction r (Value Bool))
-> CodeGenFunction r (Value Bool) -> CodeGenFunction r (Value Bool)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
                  CmpPredicate
-> Value Word
-> Value Word
-> CodeGenFunction r (CmpResult (Value Word))
forall r.
CmpPredicate
-> Value Word
-> Value Word
-> CodeGenFunction r (CmpResult (Value Word))
forall a r.
Comparison a =>
CmpPredicate -> a -> a -> CodeGenFunction r (CmpResult a)
A.cmp CmpPredicate
LLVM.CmpGT Value Word
i0 Value Word
forall a. Additive a => a
A.zero)
            (\(Value Bool
_,(Iterator WriteMode (WriteIt v) v
w0,Value Word
i0,state
s0)) -> T r
  (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
  (Iterator WriteMode (WriteIt v) v, Value Word, state)
-> CodeGenFunction
     r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
forall a r.
Undefined a =>
T r (Value Bool, a) a -> CodeGenFunction r (Value Bool, a)
Maybe.toBool (T r
   (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
   (Iterator WriteMode (WriteIt v) v, Value Word, state)
 -> CodeGenFunction
      r
      (Value Bool,
       (Iterator WriteMode (WriteIt v) v, Value Word, state)))
-> T r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
     (Iterator WriteMode (WriteIt v) v, Value Word, state)
-> CodeGenFunction
     r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
forall a b. (a -> b) -> a -> b
$ do
               (a
a,state
s1) <- global
-> Value (Ptr local)
-> state
-> T r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
     (a, state)
forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (a, state)
next global
global Value (Ptr local)
local state
s0
               CodeGenFunction
  r (Iterator WriteMode (WriteIt v) v, Value Word, state)
-> T r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
     (Iterator WriteMode (WriteIt v) v, Value Word, state)
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction
   r (Iterator WriteMode (WriteIt v) v, Value Word, state)
 -> T r
      (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
      (Iterator WriteMode (WriteIt v) v, Value Word, state))
-> CodeGenFunction
     r (Iterator WriteMode (WriteIt v) v, Value Word, state)
-> T r
     (Value Bool, (Iterator WriteMode (WriteIt v) v, Value Word, state))
     (Iterator WriteMode (WriteIt v) v, Value Word, state)
forall a b. (a -> b) -> a -> b
$ do
                  Iterator WriteMode (WriteIt v) v
w1 <- Element v
-> Iterator WriteMode (WriteIt v) v
-> CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
forall v r.
Write v =>
Element v
-> WriteIterator (WriteIt v) v
-> CodeGenFunction r (WriteIterator (WriteIt v) v)
forall r.
Element v
-> Iterator WriteMode (WriteIt v) v
-> CodeGenFunction r (Iterator WriteMode (WriteIt v) v)
SerialClass.writeNext a
Element v
a Iterator WriteMode (WriteIt v) v
w0
                  Value Word
i1 <- Value Word -> CodeGenFunction r (Value Word)
forall a r.
(IsArithmetic a, IsConst a, Num a) =>
Value a -> CodeGenFunction r (Value a)
A.dec Value Word
i0
                  (Iterator WriteMode (WriteIt v) v, Value Word, state)
-> CodeGenFunction
     r (Iterator WriteMode (WriteIt v) v, Value Word, state)
forall a. a -> CodeGenFunction r a
forall (m :: * -> *) a. Monad m => a -> m a
return (Iterator WriteMode (WriteIt v) v
w1,Value Word
i1,state
s1))
      v
v <- CodeGenFunction r v -> T r c v
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction r v -> T r c v) -> CodeGenFunction r v -> T r c v
forall a b. (a -> b) -> a -> b
$ Iterator WriteMode (WriteIt v) v -> CodeGenFunction r v
forall v r.
Write v =>
WriteIterator (WriteIt v) v -> CodeGenFunction r v
forall r. Iterator WriteMode (WriteIt v) v -> CodeGenFunction r v
SerialClass.writeStop Iterator WriteMode (WriteIt v) v
w2
      (v, state) -> T r c (v, state)
forall a. a -> T r c a
forall (m :: * -> *) a. Monad m => a -> m a
return (v
v, state
s2))
   CodeGenFunction r (global, state)
forall r. CodeGenFunction r (global, state)
start
   global -> CodeGenFunction r ()
forall r. global -> CodeGenFunction r ()
stop

{-
We could reformulate it in terms of WriteIterator
that accesses elements using LLVM.extract.
We might move the loop counter into the Iterator,
but we have to assert that the counter is not duplicated.

packIndex ::
   (SerialClass.Write v, a ~ SerialClass.Element v) =>
   Sig.T a -> Sig.T v
packIndex = alter (\(Core next start stop) -> Core
   (\param s -> do
      (v2,_,s2) <-
         Maybe.fromBool $
         U.whileLoop
            (LLVM.valueOf True, (Tuple.undef, A.zero, s))
            (\(cont,(v0,i0,_s0)) ->
               A.and cont =<<
                  A.cmp LLVM.CmpLT i0 (LLVM.valueOf $ SerialClass.size v0))
            (\(_,(v0,i0,s0)) -> Maybe.toBool $ do
               (a,s1) <- next param s0
               Maybe.lift $ do
                  v1 <- Vector.insert i0 a v0
                  i1 <- A.inc i0
                  return (v1,i1,s1))
      return (v2, s2))
   start
   stop)
-}


{- |
Like 'pack' but duplicates the code for creating elements.
That is, for vectors of size n, the code of the input signal
will be emitted n times.
This is efficient only for simple input generators.
-}
packSmall ::
   (SerialClass.Write v, a ~ SerialClass.Element v) =>
   Sig.T a -> Sig.T v
packSmall :: forall v a. (Write v, a ~ Element v) => T a -> T v
packSmall (Priv.Cons forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (a, state)
next forall r. CodeGenFunction r (global, state)
start forall r. global -> CodeGenFunction r ()
stop) = (forall r c.
 Phi c =>
 global -> Value (Ptr local) -> state -> T r c (v, state))
-> (forall r. CodeGenFunction r (global, state))
-> (forall r. global -> CodeGenFunction r ())
-> T v
forall a global local state.
(C global, IsSized local, C state) =>
(forall r c.
 Phi c =>
 global -> Value (Ptr local) -> state -> T r c (a, state))
-> (forall r. CodeGenFunction r (global, state))
-> (forall r. global -> CodeGenFunction r ())
-> T a
Priv.Cons
   (\global
global Value (Ptr local)
local ->
      StateT state (T r c) v -> state -> T r c (v, state)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
MS.runStateT (StateT state (T r c) v -> state -> T r c (v, state))
-> StateT state (T r c) v -> state -> T r c (v, state)
forall a b. (a -> b) -> a -> b
$
      (Int -> StateT state (T r c) v) -> StateT state (T r c) v
forall v (m :: * -> *). Sized v => (Int -> m v) -> m v
SerialClass.withSize ((Int -> StateT state (T r c) v) -> StateT state (T r c) v)
-> (Int -> StateT state (T r c) v) -> StateT state (T r c) v
forall a b. (a -> b) -> a -> b
$ \Int
n ->
         T r c v -> StateT state (T r c) v
forall (m :: * -> *) a. Monad m => m a -> StateT state m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
MT.lift (T r c v -> StateT state (T r c) v)
-> ([a] -> T r c v) -> [a] -> StateT state (T r c) v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CodeGenFunction r v -> T r c v
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction r v -> T r c v)
-> ([a] -> CodeGenFunction r v) -> [a] -> T r c v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> CodeGenFunction r v
[Element v] -> CodeGenFunction r v
forall r. [Element v] -> CodeGenFunction r v
forall v r. Write v => [Element v] -> CodeGenFunction r v
SerialClass.assemble
         ([a] -> StateT state (T r c) v)
-> StateT state (T r c) [a] -> StateT state (T r c) v
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
         Int -> StateT state (T r c) a -> StateT state (T r c) [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n ((state -> T r c (a, state)) -> StateT state (T r c) a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
MS.StateT ((state -> T r c (a, state)) -> StateT state (T r c) a)
-> (state -> T r c (a, state)) -> StateT state (T r c) a
forall a b. (a -> b) -> a -> b
$ global -> Value (Ptr local) -> state -> T r c (a, state)
forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (a, state)
next global
global Value (Ptr local)
local))
   CodeGenFunction r (global, state)
forall r. CodeGenFunction r (global, state)
start
   global -> CodeGenFunction r ()
forall r. global -> CodeGenFunction r ()
stop


unpack, unpackRotate ::
   (SerialClass.Read v, a ~ SerialClass.Element v,
    SerialClass.ReadIt v ~ itv, Memory.C itv) =>
   Sig.T v -> Sig.T a
unpack :: forall v a itv.
(Read v, a ~ Element v, ReadIt v ~ itv, C itv) =>
T v -> T a
unpack = T v -> T a
forall v a itv.
(Read v, a ~ Element v, ReadIt v ~ itv, C itv) =>
T v -> T a
unpackRotate

unpackRotate :: forall v a itv.
(Read v, a ~ Element v, ReadIt v ~ itv, C itv) =>
T v -> T a
unpackRotate (Priv.Cons forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (v, state)
next forall r. CodeGenFunction r (global, state)
start forall r. global -> CodeGenFunction r ()
stop) = (forall r c.
 Phi c =>
 global
 -> Value (Ptr local)
 -> (Value Word, ReadIterator itv v, state)
 -> T r c (a, (Value Word, ReadIterator itv v, state)))
-> (forall r.
    CodeGenFunction
      r (global, (Value Word, ReadIterator itv v, state)))
-> (forall r. global -> CodeGenFunction r ())
-> T a
forall a global local state.
(C global, IsSized local, C state) =>
(forall r c.
 Phi c =>
 global -> Value (Ptr local) -> state -> T r c (a, state))
-> (forall r. CodeGenFunction r (global, state))
-> (forall r. global -> CodeGenFunction r ())
-> T a
Priv.Cons
   (\global
global Value (Ptr local)
local (Value Word
i0,ReadIterator itv v
r0,state
s0) -> do
      Value Bool
endOfVector <-
         CodeGenFunction r (Value Bool) -> T r c (Value Bool)
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction r (Value Bool) -> T r c (Value Bool))
-> CodeGenFunction r (Value Bool) -> T r c (Value Bool)
forall a b. (a -> b) -> a -> b
$ CmpPredicate
-> Value Word
-> Value Word
-> CodeGenFunction r (CmpResult (Value Word))
forall r.
CmpPredicate
-> Value Word
-> Value Word
-> CodeGenFunction r (CmpResult (Value Word))
forall a r.
Comparison a =>
CmpPredicate -> a -> a -> CodeGenFunction r (CmpResult a)
A.cmp CmpPredicate
LLVM.CmpEQ Value Word
i0 (Word -> Value Word
forall a. IsConst a => a -> Value a
LLVM.valueOf (Word
0::Word))
      (Value Word
i2,ReadIterator itv v
r2,state
s2) <-
         CodeGenFunction
  r (Value Bool, (Value Word, ReadIterator itv v, state))
-> T r c (Value Word, ReadIterator itv v, state)
forall z r a. Phi z => CodeGenFunction r (Value Bool, a) -> T r z a
Maybe.fromBool (CodeGenFunction
   r (Value Bool, (Value Word, ReadIterator itv v, state))
 -> T r c (Value Word, ReadIterator itv v, state))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
-> T r c (Value Word, ReadIterator itv v, state)
forall a b. (a -> b) -> a -> b
$
         Value Bool
-> (Value Bool, (Value Word, ReadIterator itv v, state))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
forall a r.
Phi a =>
Value Bool -> a -> CodeGenFunction r a -> CodeGenFunction r a
U.ifThen Value Bool
endOfVector (Bool -> Value Bool
forall a. IsConst a => a -> Value a
LLVM.valueOf Bool
True, (Value Word
i0,ReadIterator itv v
r0,state
s0)) (CodeGenFunction
   r (Value Bool, (Value Word, ReadIterator itv v, state))
 -> CodeGenFunction
      r (Value Bool, (Value Word, ReadIterator itv v, state)))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
forall a b. (a -> b) -> a -> b
$ do
            (Value Bool
cont1, (v
v1,state
s1)) <- T r (Value Bool, (v, state)) (v, state)
-> CodeGenFunction r (Value Bool, (v, state))
forall a r.
Undefined a =>
T r (Value Bool, a) a -> CodeGenFunction r (Value Bool, a)
Maybe.toBool (T r (Value Bool, (v, state)) (v, state)
 -> CodeGenFunction r (Value Bool, (v, state)))
-> T r (Value Bool, (v, state)) (v, state)
-> CodeGenFunction r (Value Bool, (v, state))
forall a b. (a -> b) -> a -> b
$ global
-> Value (Ptr local)
-> state
-> T r (Value Bool, (v, state)) (v, state)
forall r c.
Phi c =>
global -> Value (Ptr local) -> state -> T r c (v, state)
next global
global Value (Ptr local)
local state
s0
            ReadIterator itv v
r1 <- v -> CodeGenFunction r (ReadIterator (ReadIt v) v)
forall r. v -> CodeGenFunction r (ReadIterator (ReadIt v) v)
forall v r.
Read v =>
v -> CodeGenFunction r (ReadIterator (ReadIt v) v)
SerialClass.readStart v
v1
            (Value Bool, (Value Word, ReadIterator itv v, state))
-> CodeGenFunction
     r (Value Bool, (Value Word, ReadIterator itv v, state))
forall a. a -> CodeGenFunction r a
forall (m :: * -> *) a. Monad m => a -> m a
return (Value Bool
cont1, (Word -> Value Word
forall a. IsConst a => a -> Value a
LLVM.valueOf (Word -> Value Word) -> Word -> Value Word
forall a b. (a -> b) -> a -> b
$ v -> Word
forall v i. (Sized v, Integral i) => v -> i
SerialClass.size v
v1, ReadIterator itv v
r1, state
s1))
      CodeGenFunction r (a, (Value Word, ReadIterator itv v, state))
-> T r c (a, (Value Word, ReadIterator itv v, state))
forall r a z. CodeGenFunction r a -> T r z a
Maybe.lift (CodeGenFunction r (a, (Value Word, ReadIterator itv v, state))
 -> T r c (a, (Value Word, ReadIterator itv v, state)))
-> CodeGenFunction r (a, (Value Word, ReadIterator itv v, state))
-> T r c (a, (Value Word, ReadIterator itv v, state))
forall a b. (a -> b) -> a -> b
$ do
         (a
a,ReadIterator (ReadIt v) v
r3) <- ReadIterator (ReadIt v) v
-> CodeGenFunction r (Element v, ReadIterator (ReadIt v) v)
forall v r.
Read v =>
ReadIterator (ReadIt v) v
-> CodeGenFunction r (Element v, ReadIterator (ReadIt v) v)
forall r.
ReadIterator (ReadIt v) v
-> CodeGenFunction r (Element v, ReadIterator (ReadIt v) v)
SerialClass.readNext ReadIterator itv v
ReadIterator (ReadIt v) v
r2
         Value Word
i3 <- Value Word -> CodeGenFunction r (Value Word)
forall a r.
(IsArithmetic a, IsConst a, Num a) =>
Value a -> CodeGenFunction r (Value a)
A.dec Value Word
i2
         (a, (Value Word, ReadIterator itv v, state))
-> CodeGenFunction r (a, (Value Word, ReadIterator itv v, state))
forall a. a -> CodeGenFunction r a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, (Value Word
i3,ReadIterator (ReadIt v) v
r3,state
s2)))
   ((state -> (Value Word, ReadIterator itv v, state))
-> (global, state)
-> (global, (Value Word, ReadIterator itv v, state))
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (\state
s -> (Value Word
forall a. Additive a => a
A.zero, ReadIterator itv v
forall a. Undefined a => a
Tuple.undef, state
s)) ((global, state)
 -> (global, (Value Word, ReadIterator itv v, state)))
-> CodeGenFunction r (global, state)
-> CodeGenFunction
     r (global, (Value Word, ReadIterator itv v, state))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodeGenFunction r (global, state)
forall r. CodeGenFunction r (global, state)
start)
   global -> CodeGenFunction r ()
forall r. global -> CodeGenFunction r ()
stop


{-
We could reformulate it in terms of ReadIterator
that accesses elements using LLVM.extract.
We might move the loop counter into the Iterator,
but we have to assert that the counter is not duplicated.

unpackIndex ::
   (SerialClass.Write v, a ~ SerialClass.Element v, Memory.C v) =>
   Sig.T v -> Sig.T a
unpackIndex = alter (\(Core next start stop) -> Core
   (\param (i0,v0,s0) -> do
      endOfVector <-
         Maybe.lift $ A.cmp LLVM.CmpGE i0 (LLVM.valueOf $ SerialClass.size v0)
      (i2,v2,s2) <-
         Maybe.fromBool $
         U.ifThen endOfVector (LLVM.valueOf True, (i0,v0,s0)) $ do
            (cont1, (v1,s1)) <- Maybe.toBool $ next param s0
            return (cont1, (A.zero, v1, s1))
      Maybe.lift $ do
         a <- Vector.extract i2 v2
         i3 <- A.inc i2
         return (a, (i3,v2,s2)))
   (\p -> do
      s <- start p
      let v = Tuple.undef
      return (LLVM.valueOf $ SerialClass.size v, v, s))
   stop)
-}



type Serial n a = SerialCode.Value n a

withSize ::
   (TypeNum.Positive n) =>
   (TypeNum.Singleton n -> Sig.T (Serial n a)) ->
   Sig.T (Serial n a)
withSize :: forall n a.
Positive n =>
(Singleton n -> T (Serial n a)) -> T (Serial n a)
withSize Singleton n -> T (Serial n a)
f = Singleton n -> T (Serial n a)
f Singleton n
forall x. Integer x => Singleton x
TypeNum.singleton

withSizeRing ::
   (Ring.C b, TypeNum.Positive n) =>
   (b -> Sig.T (Serial n a)) ->
   Sig.T (Serial n a)
withSizeRing :: forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing b -> T (Serial n a)
f =
   (Singleton n -> T (Serial n a)) -> T (Serial n a)
forall n a.
Positive n =>
(Singleton n -> T (Serial n a)) -> T (Serial n a)
withSize ((Singleton n -> T (Serial n a)) -> T (Serial n a))
-> (Singleton n -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ b -> T (Serial n a)
f (b -> T (Serial n a))
-> (Singleton n -> b) -> Singleton n -> T (Serial n a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> b
forall a. C a => Integer -> a
fromInteger (Integer -> b) -> (Singleton n -> Integer) -> Singleton n -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Singleton n -> Integer
forall n. Integer n => Singleton n -> Integer
TypeNum.integerFromSingleton


constant ::
   (Marshal.Vector n a) =>
   Exp a -> Sig.T (Serial n a)
constant :: forall n a. Vector n a => Exp a -> T (Serial n a)
constant = Exp (T n a) -> T (Serial n a)
forall ae al. (Aggregate ae al, C al) => ae -> T al
Sig.constant (Exp (T n a) -> T (Serial n a))
-> (Exp a -> Exp (T n a)) -> Exp a -> T (Serial n a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample


exponential2 ::
   (Marshal.Vector n a, MultiVector.Transcendental a,
    MultiValue.RationalConstant a) =>
   Exp a -> Exp a -> Sig.T (Serial n a)
exponential2 :: forall n a.
(Vector n a, Transcendental a, RationalConstant a) =>
Exp a -> Exp a -> T (Serial n a)
exponential2 Exp a
halfLife Exp a
start = (Exp a -> T (Serial n a)) -> T (Serial n a)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp a -> T (Serial n a)) -> T (Serial n a))
-> (Exp a -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ \Exp a
n ->
   Exp (T n a) -> Exp (T n a) -> T (Serial n a)
forall a. (C a, PseudoRing a) => Exp a -> Exp a -> MV a
Core.exponential
      (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (Exp a
0.5 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
** (Exp a
n Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/ Exp a
halfLife)))
      ((Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a
0.5 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
** Exp a -> Exp a
forall a. C a => a -> a
recip Exp a
halfLife Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*) Exp a
start)

exponentialBounded2 ::
   (Marshal.Vector n a, MultiVector.Transcendental a,
    MultiValue.RationalConstant a,
    MultiVector.IntegerConstant a, MultiVector.Real a) =>
   Exp a -> Exp a -> Exp a -> Sig.T (Serial n a)
exponentialBounded2 :: forall n a.
(Vector n a, Transcendental a, RationalConstant a,
 IntegerConstant a, Real a) =>
Exp a -> Exp a -> Exp a -> T (Serial n a)
exponentialBounded2 Exp a
bound Exp a
halfLife Exp a
start = (Exp a -> T (Serial n a)) -> T (Serial n a)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp a -> T (Serial n a)) -> T (Serial n a))
-> (Exp a -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ \Exp a
n ->
   Exp (T n a) -> Exp (T n a) -> Exp (T n a) -> T (Serial n a)
forall a.
(C a, PseudoRing a, Real a, IntegerConstant a) =>
Exp a -> Exp a -> Exp a -> MV a
Core.exponentialBounded
      (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample Exp a
bound)
      (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (Exp a
0.5 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
** (Exp a
n Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/ Exp a
halfLife)))
      ((Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a
0.5 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
** Exp a -> Exp a
forall a. C a => a -> a
recip Exp a
halfLife Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*) Exp a
start)

osciCore ::
   (Marshal.Vector n t, MultiVector.PseudoRing t, MultiVector.Fraction t,
    MultiValue.IntegerConstant t) =>
   Exp t -> Exp t -> Sig.T (Serial n t)
osciCore :: forall n t.
(Vector n t, PseudoRing t, Fraction t, IntegerConstant t) =>
Exp t -> Exp t -> T (Serial n t)
osciCore Exp t
phase Exp t
freq = (Exp t -> T (Serial n t)) -> T (Serial n t)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp t -> T (Serial n t)) -> T (Serial n t))
-> (Exp t -> T (Serial n t)) -> T (Serial n t)
forall a b. (a -> b) -> a -> b
$ \Exp t
n ->
   Exp (T n t) -> Exp (T n t) -> T (Serial n t)
forall t. (Fraction t, C t) => Exp t -> Exp t -> MV t
Core.osci
      ((Exp t -> Exp t) -> Exp t -> Exp (T n t)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp t -> Exp t
forall a. Fraction a => Exp a -> Exp a
Expr.fraction (Exp t -> Exp t) -> (Exp t -> Exp t) -> Exp t -> Exp t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Exp t
freq Exp t -> Exp t -> Exp t
forall a. C a => a -> a -> a
+)) Exp t
phase)
      (Exp t -> Exp (T n t)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (Exp t -> Exp t
forall a. Fraction a => Exp a -> Exp a
Expr.fraction (Exp t
n Exp t -> Exp t -> Exp t
forall a. C a => a -> a -> a
* Exp t
freq)))

osci ::
   (Marshal.Vector n t, MultiVector.PseudoRing t, MultiVector.Fraction t,
    MultiValue.IntegerConstant t) =>
   (forall r. Serial n t -> LLVM.CodeGenFunction r y) ->
   Exp t -> Exp t -> Sig.T y
osci :: forall n t y.
(Vector n t, PseudoRing t, Fraction t, IntegerConstant t) =>
(forall r. Serial n t -> CodeGenFunction r y)
-> Exp t -> Exp t -> T y
osci forall r. Serial n t -> CodeGenFunction r y
wave Exp t
phase Exp t
freq = (forall r. Serial n t -> CodeGenFunction r y)
-> T (Serial n t) -> T y
forall a b. (forall r. a -> CodeGenFunction r b) -> T a -> T b
Priv.map Serial n t -> CodeGenFunction r y
forall r. Serial n t -> CodeGenFunction r y
wave (T (Serial n t) -> T y) -> T (Serial n t) -> T y
forall a b. (a -> b) -> a -> b
$ Exp t -> Exp t -> T (Serial n t)
forall n t.
(Vector n t, PseudoRing t, Fraction t, IntegerConstant t) =>
Exp t -> Exp t -> T (Serial n t)
osciCore Exp t
phase Exp t
freq


rampInf, rampSlope, parabolaFadeInInf, parabolaFadeOutInf ::
   (Marshal.Vector n a, MultiVector.Field a, MultiVector.IntegerConstant a,
    MultiValue.RationalConstant a) =>
   Exp a -> Sig.T (Serial n a)
rampSlope :: forall n a.
(Vector n a, Field a, IntegerConstant a, RationalConstant a) =>
Exp a -> T (Serial n a)
rampSlope Exp a
slope = (Exp a -> T (Serial n a)) -> T (Serial n a)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp a -> T (Serial n a)) -> T (Serial n a))
-> (Exp a -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ \Exp a
n ->
   Exp (T n a) -> Exp (T n a) -> T (Serial n a)
forall a. (C a, Additive a) => Exp a -> Exp a -> MV a
Core.ramp
      (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (Exp a
n Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
* Exp a
slope))
      ((Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a
slope Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
+) Exp a
0)
rampInf :: forall n a.
(Vector n a, Field a, IntegerConstant a, RationalConstant a) =>
Exp a -> T (Serial n a)
rampInf Exp a
dur = Exp a -> T (Serial n a)
forall n a.
(Vector n a, Field a, IntegerConstant a, RationalConstant a) =>
Exp a -> T (Serial n a)
rampSlope (Exp a -> Exp a
forall a. (Field a, IntegerConstant a) => Exp a -> Exp a
Expr.recip Exp a
dur)

parabolaFadeInInf :: forall n a.
(Vector n a, Field a, IntegerConstant a, RationalConstant a) =>
Exp a -> T (Serial n a)
parabolaFadeInInf Exp a
dur = (Exp a -> T (Serial n a)) -> T (Serial n a)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp a -> T (Serial n a)) -> T (Serial n a))
-> (Exp a -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ \Exp a
n ->
   let d :: Exp a
d = Exp a
nExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/Exp a
dur
   in Exp (T n a) -> Exp (T n a) -> Exp (T n a) -> T (Serial n a)
forall a. (C a, Additive a) => Exp a -> Exp a -> Exp a -> MV a
Core.parabola
         (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (-Exp a
2Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
dExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
d))
         ((Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
subtract (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
forall a b. (a -> b) -> a -> b
$ Exp a
2 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/ Exp a
dur Exp a -> Integer -> Exp a
forall a. C a => a -> Integer -> a
^ Integer
2) (Exp a
dExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*(Exp a
2Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
-Exp a
d)))
         ((\Exp (T n a)
t -> Exp (T n a)
tExp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
*(Exp (T n a)
2Exp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
-Exp (T n a)
t)) (Exp (T n a) -> Exp (T n a)) -> Exp (T n a) -> Exp (T n a)
forall a b. (a -> b) -> a -> b
$ (Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a -> Exp a
forall a. C a => a -> a
recip Exp a
dur Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
+) Exp a
0)

parabolaFadeOutInf :: forall n a.
(Vector n a, Field a, IntegerConstant a, RationalConstant a) =>
Exp a -> T (Serial n a)
parabolaFadeOutInf Exp a
dur = (Exp a -> T (Serial n a)) -> T (Serial n a)
forall b n a.
(C b, Positive n) =>
(b -> T (Serial n a)) -> T (Serial n a)
withSizeRing ((Exp a -> T (Serial n a)) -> T (Serial n a))
-> (Exp a -> T (Serial n a)) -> T (Serial n a)
forall a b. (a -> b) -> a -> b
$ \Exp a
n ->
   let d :: Exp a
d = Exp a
nExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/Exp a
dur
   in Exp (T n a) -> Exp (T n a) -> Exp (T n a) -> T (Serial n a)
forall a. (C a, Additive a) => Exp a -> Exp a -> Exp a -> MV a
Core.parabola
         (Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (-Exp a
2Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
dExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
d))
         ((Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
subtract (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
forall a b. (a -> b) -> a -> b
$ Exp a
2 Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/ Exp a
dur Exp a -> Integer -> Exp a
forall a. C a => a -> Integer -> a
^ Integer
2) (-Exp a
dExp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
d))
         ((\Exp (T n a)
t -> Exp (T n a)
1Exp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
-Exp (T n a)
tExp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
*Exp (T n a)
t) (Exp (T n a) -> Exp (T n a)) -> Exp (T n a) -> Exp (T n a)
forall a b. (a -> b) -> a -> b
$ (Exp a -> Exp a) -> Exp a -> Exp (T n a)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate (Exp a -> Exp a
forall a. C a => a -> a
recip Exp a
dur Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
+) Exp a
0)


{- |
For the mysterious rate parameter see 'Sig.noise'.
-}
noise ::
   (MultiVector.NativeFloating n a ar) =>
   (MultiVector.PseudoRing a, MultiVector.IntegerConstant a) =>
   (MultiValue.Algebraic a, MultiValue.RationalConstant a) =>
   (TypeNum.Positive n, TypeNum.Positive (n :*: TypeNum.D32)) =>
   Exp Word32 -> Exp a -> Sig.T (Serial n a)
noise :: forall n a ar.
(NativeFloating n a ar, PseudoRing a, IntegerConstant a,
 Algebraic a, RationalConstant a, Positive n,
 Positive (n :*: D32)) =>
Exp Word32 -> Exp a -> T (Serial n a)
noise Exp Word32
seed Exp a
rate =
   let m2 :: Integer
m2 = Integer -> Integer -> Integer
forall a. C a => a -> a -> a
div Integer
forall a. Integral a => a
Rnd.modulus Integer
2
       r :: Exp (T n a)
r = Exp a -> Exp (T n a)
forall n a. (Positive n, C a) => Exp a -> Exp (T n a)
Serial.upsample (Exp a -> Exp (T n a)) -> Exp a -> Exp (T n a)
forall a b. (a -> b) -> a -> b
$ Exp a -> Exp a
forall a. Algebraic a => Exp a -> Exp a
Expr.sqrt (Exp a
3Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
*Exp a
rate) Exp a -> Exp a -> Exp a
forall a. C a => a -> a -> a
/ Integer -> Exp a
forall a. IntegerConstant a => Integer -> Exp a
Expr.fromInteger' Integer
m2
   in (Exp (T n Word32) -> Exp (T n a))
-> T (Serial n Word32) (Serial n a)
forall ae a be b.
(Aggregate ae a, Aggregate be b) =>
(ae -> be) -> T a b
Causal.map
         (\Exp (T n Word32)
y -> Exp (T n a)
r Exp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
* ((forall r. Serial n Word32 -> CodeGenFunction r (Serial n a))
-> Exp (T n Word32) -> Exp (T n a)
forall ae am b.
Aggregate ae am =>
(forall r. am -> CodeGenFunction r (T b)) -> ae -> Exp b
Expr.liftM Serial n Word32 -> CodeGenFunction r (Serial n a)
forall r. Serial n Word32 -> CodeGenFunction r (Serial n a)
forall n a ar r.
(NativeFloating n a ar, Positive n, Positive (n :*: D32)) =>
Serial n Word32 -> CodeGenFunction r (Serial n a)
int31tofp Exp (T n Word32)
y Exp (T n a) -> Exp (T n a) -> Exp (T n a)
forall a. C a => a -> a -> a
- Integer -> Exp (T n a)
forall a. IntegerConstant a => Integer -> Exp a
Expr.fromInteger' (Integer
m2Integer -> Integer -> Integer
forall a. C a => a -> a -> a
+Integer
1))) T (Serial n Word32) (Serial n a)
-> SignalOf T (Serial n Word32) -> SignalOf T (Serial n a)
forall (process :: * -> * -> *) a b.
C process =>
process a b -> SignalOf process a -> SignalOf process b
$*
      Exp Word32 -> T (Serial n Word32)
forall n.
(Positive n, Positive (n :*: D32)) =>
Exp Word32 -> T (Serial n Word32)
noiseCoreAlt Exp Word32
seed

{-
sitofp is a single instruction on x86
and thus we use it, since the arguments are below 2^31.

It would be better to use LLVM's range annotation, instead.
-}
int31tofp ::
   (MultiVector.NativeFloating n a ar,
    TypeNum.Positive n, TypeNum.Positive (n :*: TypeNum.D32)) =>
   Serial n Word32 -> LLVM.CodeGenFunction r (Serial n a)
int31tofp :: forall n a ar r.
(NativeFloating n a ar, Positive n, Positive (n :*: D32)) =>
Serial n Word32 -> CodeGenFunction r (Serial n a)
int31tofp =
   (T (Vector n a) -> Serial n a)
-> CodeGenFunction r (T (Vector n a))
-> CodeGenFunction r (Serial n a)
forall a b. (a -> b) -> CodeGenFunction r a -> CodeGenFunction r b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap T (Vector n a) -> Serial n a
forall n a. T (Vector n a) -> Value n a
SerialCode.fromOrdinary (CodeGenFunction r (T (Vector n a))
 -> CodeGenFunction r (Serial n a))
-> (Serial n Int32 -> CodeGenFunction r (T (Vector n a)))
-> Serial n Int32
-> CodeGenFunction r (Serial n a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T (Vector n Int32) -> CodeGenFunction r (T (Vector n a))
forall i ir a ar r.
(NativeInteger i ir, NativeFloating a ar,
 ShapeOf ir ~ ShapeOf ar) =>
T i -> CodeGenFunction r (T a)
MultiValueVec.fromIntegral (T (Vector n Int32) -> CodeGenFunction r (T (Vector n a)))
-> (Serial n Int32 -> T (Vector n Int32))
-> Serial n Int32
-> CodeGenFunction r (T (Vector n a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   Serial n Int32 -> T (Vector n Int32)
forall n a. Value n a -> T (Vector n a)
SerialCode.toOrdinary (Serial n Int32 -> T (Vector n Int32))
-> (Serial n Int32 -> Serial n Int32)
-> Serial n Int32
-> T (Vector n Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Serial n Int32 -> Serial n Int32
forall n. Id (Serial n Int32)
forceInt32
      (Serial n Int32 -> CodeGenFunction r (Serial n a))
-> (Serial n Word32 -> CodeGenFunction r (Serial n Int32))
-> Serial n Word32
-> CodeGenFunction r (Serial n a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (Repr (T n Word32) -> CodeGenFunction r (Repr (T n Int32)))
-> Serial n Word32 -> CodeGenFunction r (Serial n Int32)
forall (m :: * -> *) a b.
Monad m =>
(Repr a -> m (Repr b)) -> T a -> m (T b)
MultiValue.liftM Repr (T n Word32) -> CodeGenFunction r (Repr (T n Int32))
Value (Vector n Word32)
-> CodeGenFunction r (Value (Vector n Int32))
forall (value :: * -> *) a b r.
(ValueCons value, IsSized a, IsSized b, SizeOf a ~ SizeOf b) =>
value a -> CodeGenFunction r (value b)
LLVM.bitcast

type Id a = a -> a

forceInt32 :: Id (Serial n Int32)
forceInt32 :: forall n. Id (Serial n Int32)
forceInt32 = Serial n Int32 -> Serial n Int32
forall a. a -> a
id

noiseCore, noiseCoreAlt ::
   (TypeNum.Positive n, TypeNum.Positive (n :*: TypeNum.D32)) =>
   Exp Word32 -> Sig.T (Serial n Word32)
noiseCore :: forall n.
(Positive n, Positive (n :*: D32)) =>
Exp Word32 -> T (Serial n Word32)
noiseCore    = (Exp (T n Word32) -> Exp (T n Word32))
-> Exp (T n Word32) -> MV (T n Word32)
forall a. C a => (Exp a -> Exp a) -> Exp a -> MV a
Sig.iterate ((forall r.
 Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32)))
-> Exp (T n Word32) -> Exp (T n Word32)
forall a b.
(forall r. Repr a -> CodeGenFunction r (Repr b)) -> Exp a -> Exp b
Expr.liftReprM Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32))
Value (Vector n Word32)
-> CodeGenFunction r (Value (Vector n Word32))
forall r.
Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32))
forall n r.
Positive n =>
Value (Vector n Word32)
-> CodeGenFunction r (Value (Vector n Word32))
Rnd.nextVector)   (Exp (T n Word32) -> MV (T n Word32))
-> (Exp Word32 -> Exp (T n Word32))
-> Exp Word32
-> MV (T n Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp Word32 -> Exp (T n Word32)
forall n. Positive n => Exp Word32 -> Exp (T n Word32)
vectorSeed
noiseCoreAlt :: forall n.
(Positive n, Positive (n :*: D32)) =>
Exp Word32 -> T (Serial n Word32)
noiseCoreAlt = (Exp (T n Word32) -> Exp (T n Word32))
-> Exp (T n Word32) -> MV (T n Word32)
forall a. C a => (Exp a -> Exp a) -> Exp a -> MV a
Sig.iterate ((forall r.
 Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32)))
-> Exp (T n Word32) -> Exp (T n Word32)
forall a b.
(forall r. Repr a -> CodeGenFunction r (Repr b)) -> Exp a -> Exp b
Expr.liftReprM Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32))
Value (Vector n Word32)
-> CodeGenFunction r (Value (Vector n Word32))
forall r.
Repr (T n Word32) -> CodeGenFunction r (Repr (T n Word32))
forall n r.
Positive n =>
Value (Vector n Word32)
-> CodeGenFunction r (Value (Vector n Word32))
Rnd.nextVector64) (Exp (T n Word32) -> MV (T n Word32))
-> (Exp Word32 -> Exp (T n Word32))
-> Exp Word32
-> MV (T n Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp Word32 -> Exp (T n Word32)
forall n. Positive n => Exp Word32 -> Exp (T n Word32)
vectorSeed

vectorSeed :: (TypeNum.Positive n) => Exp Word32 -> Exp (Serial.T n Word32)
vectorSeed :: forall n. Positive n => Exp Word32 -> Exp (T n Word32)
vectorSeed Exp Word32
seed =
   (Exp Word32 -> Exp Word32) -> Exp Word32 -> Exp (T n Word32)
forall n a.
(Positive n, C a) =>
(Exp a -> Exp a) -> Exp a -> Exp (T n a)
Serial.iterate ((forall r. Repr Word32 -> CodeGenFunction r (Repr Word32))
-> Exp Word32 -> Exp Word32
forall a b.
(forall r. Repr a -> CodeGenFunction r (Repr b)) -> Exp a -> Exp b
Expr.liftReprM Repr Word32 -> CodeGenFunction r (Repr Word32)
Value Word32 -> CodeGenFunction r (Value Word32)
forall r. Repr Word32 -> CodeGenFunction r (Repr Word32)
forall r. Value Word32 -> CodeGenFunction r (Value Word32)
Rnd.nextCG) (Exp Word32 -> Exp (T n Word32)) -> Exp Word32 -> Exp (T n Word32)
forall a b. (a -> b) -> a -> b
$
   Exp Word32 -> Exp Word32 -> Exp Word32
forall a. Integral a => Exp a -> Exp a -> Exp a
Expr.irem Exp Word32
seed (Integer -> Exp Word32
forall a. C a => Integer -> a
fromInteger Integer
forall a. Integral a => a
Rnd.modulus Exp Word32 -> Exp Word32 -> Exp Word32
forall a. C a => a -> a -> a
- Exp Word32
1) Exp Word32 -> Exp Word32 -> Exp Word32
forall a. C a => a -> a -> a
+ Exp Word32
1