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