Skip to content

Commit

Permalink
add fast tanh
Browse files Browse the repository at this point in the history
  • Loading branch information
heuermh committed Apr 14, 2024
1 parent b7b83c6 commit 491ce79
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions import.ck
Expand Up @@ -89,6 +89,7 @@ Machine.add(path + "lick/fn/Ceil.ck");
Machine.add(path + "lick/fn/Cos.ck"); Machine.add(path + "lick/fn/Cos.ck");
Machine.add(path + "lick/fn/Cosh.ck"); Machine.add(path + "lick/fn/Cosh.ck");
Machine.add(path + "lick/fn/Exp.ck"); Machine.add(path + "lick/fn/Exp.ck");
Machine.add(path + "lick/fn/FastTanh.ck");
Machine.add(path + "lick/fn/Floor.ck"); Machine.add(path + "lick/fn/Floor.ck");
Machine.add(path + "lick/fn/Identity.ck"); Machine.add(path + "lick/fn/Identity.ck");
Machine.add(path + "lick/fn/Inverse.ck"); Machine.add(path + "lick/fn/Inverse.ck");
Expand Down Expand Up @@ -383,6 +384,7 @@ Machine.add(path + "lick/dist/Invert.ck");
Machine.add(path + "lick/dist/Offset.ck"); Machine.add(path + "lick/dist/Offset.ck");
Machine.add(path + "lick/dist/Phase.ck"); Machine.add(path + "lick/dist/Phase.ck");
Machine.add(path + "lick/dist/AtanDist.ck"); Machine.add(path + "lick/dist/AtanDist.ck");
Machine.add(path + "lick/dist/FastTanhDist.ck");
Machine.add(path + "lick/dist/TanhDist.ck"); Machine.add(path + "lick/dist/TanhDist.ck");
Machine.add(path + "lick/dist/RibbonDist.ck"); Machine.add(path + "lick/dist/RibbonDist.ck");
Machine.add(path + "lick/dist/FrostburnDist.ck"); Machine.add(path + "lick/dist/FrostburnDist.ck");
Expand Down
29 changes: 29 additions & 0 deletions lick/dist/FastTanhDist.ck
@@ -0,0 +1,29 @@
/*
LiCK Library for ChucK.
Copyright (c) 2007-2024 held jointly by the individual authors.
This file is part of LiCK.
LiCK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LiCK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiCK. If not, see <http://www.gnu.org/licenses/>.
*/

public class FastTanhDist extends WaveShaper
{
{
FastTanh fastTanh;
fastTanh @=> shape;
}
}
42 changes: 42 additions & 0 deletions lick/fn/FastTanh.ck
@@ -0,0 +1,42 @@
/*
LiCK Library for ChucK.
Copyright (c) 2007-2024 held jointly by the individual authors.
This file is part of LiCK.
LiCK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LiCK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LiCK. If not, see <http://www.gnu.org/licenses/>.
*/

public class FastTanh extends FloatFunction
{
fun float evaluate(float arg)
{
return tanh(arg);
}

fun static float tanh(float v)
{
if (v > 1.0f)
{
return 1.0f;
}
if (v < -1.0f)
{
return -1.0f;
}
return v * (9.0f + v * v) / (9.0f + 3.0f * v * v);
}
}

0 comments on commit 491ce79

Please sign in to comment.