smoothstep.sh revision 1.1 1 #!/bin/sh
2 #
3 # Generate a discrete lookup table for a sigmoid function in the smoothstep
4 # family (https://en.wikipedia.org/wiki/Smoothstep), where the lookup table
5 # entries correspond to x in [1/nsteps, 2/nsteps, ..., nsteps/nsteps]. Encode
6 # the entries using a binary fixed point representation.
7 #
8 # Usage: smoothstep.sh <variant> <nsteps> <bfp> <xprec> <yprec>
9 #
10 # <variant> is in {smooth, smoother, smoothest}.
11 # <nsteps> must be greater than zero.
12 # <bfp> must be in [0..62]; reasonable values are roughly [10..30].
13 # <xprec> is x decimal precision.
14 # <yprec> is y decimal precision.
15
16 #set -x
17
18 cmd="sh smoothstep.sh $*"
19 variant=$1
20 nsteps=$2
21 bfp=$3
22 xprec=$4
23 yprec=$5
24
25 case "${variant}" in
26 smooth)
27 ;;
28 smoother)
29 ;;
30 smoothest)
31 ;;
32 *)
33 echo "Unsupported variant"
34 exit 1
35 ;;
36 esac
37
38 smooth() {
39 step=$1
40 y=`echo ${yprec} k ${step} ${nsteps} / sx _2 lx 3 ^ '*' 3 lx 2 ^ '*' + p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g'`
41 h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g' | tr '.' ' ' | awk '{print $1}' `
42 }
43
44 smoother() {
45 step=$1
46 y=`echo ${yprec} k ${step} ${nsteps} / sx 6 lx 5 ^ '*' _15 lx 4 ^ '*' + 10 lx 3 ^ '*' + p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g'`
47 h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g' | tr '.' ' ' | awk '{print $1}' `
48 }
49
50 smoothest() {
51 step=$1
52 y=`echo ${yprec} k ${step} ${nsteps} / sx _20 lx 7 ^ '*' 70 lx 6 ^ '*' + _84 lx 5 ^ '*' + 35 lx 4 ^ '*' + p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g'`
53 h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g' | tr '.' ' ' | awk '{print $1}' `
54 }
55
56 cat <<EOF
57 #ifndef JEMALLOC_INTERNAL_SMOOTHSTEP_H
58 #define JEMALLOC_INTERNAL_SMOOTHSTEP_H
59
60 /*
61 * This file was generated by the following command:
62 * $cmd
63 */
64 /******************************************************************************/
65
66 /*
67 * This header defines a precomputed table based on the smoothstep family of
68 * sigmoidal curves (https://en.wikipedia.org/wiki/Smoothstep) that grow from 0
69 * to 1 in 0 <= x <= 1. The table is stored as integer fixed point values so
70 * that floating point math can be avoided.
71 *
72 * 3 2
73 * smoothstep(x) = -2x + 3x
74 *
75 * 5 4 3
76 * smootherstep(x) = 6x - 15x + 10x
77 *
78 * 7 6 5 4
79 * smootheststep(x) = -20x + 70x - 84x + 35x
80 */
81
82 #define SMOOTHSTEP_VARIANT "${variant}"
83 #define SMOOTHSTEP_NSTEPS ${nsteps}
84 #define SMOOTHSTEP_BFP ${bfp}
85 #define SMOOTHSTEP \\
86 /* STEP(step, h, x, y) */ \\
87 EOF
88
89 s=1
90 while [ $s -le $nsteps ] ; do
91 $variant ${s}
92 x=`echo ${xprec} k ${s} ${nsteps} / p | dc | tr -d '\\\\\n' | sed -e 's#^\.#0.#g'`
93 printf ' STEP(%4d, UINT64_C(0x%016x), %s, %s) \\\n' ${s} ${h} ${x} ${y}
94
95 s=$((s+1))
96 done
97 echo
98
99 cat <<EOF
100 #endif /* JEMALLOC_INTERNAL_SMOOTHSTEP_H */
101 EOF
102