re-write median calculation
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
diff --git a/etc/tune_it.sh b/etc/tune_it.sh
index b323410..5e0fe7c 100755
--- a/etc/tune_it.sh
+++ b/etc/tune_it.sh
@@ -11,28 +11,13 @@ LCG() {
SEED=$(((1103515245 * $SEED + 12345) % 2147483648))
echo $SEED
}
-
median() {
- median=0;
- flag=1;
- for val in $* ; do
- if [ $flag -eq 1 ] ; then
- flag=$((flag + 1))
- continue
- elif [ $flag -eq 2 ] ; then
- median=$val
- flag=$((flag + 1))
- continue
- else
- if [ $median -lt $val ] ; then
- median=$((median + 1))
- fi
- if [ $median -gt $val ] ; then
- median=$((median - 1))
- fi
- fi
- done
- echo $median
+# read everything besides the header from file $1
+# | cut-out the required column $2
+# | sort all the entries numerically
+# | show only the first $3 entries
+# | show only the last entry
+ tail -n +2 $1 | cut -d' ' -f$2 | sort -n | head -n $3 | tail -n 1
}
MPWD=$(dirname $(readlink -f "$0"))
@@ -100,27 +85,23 @@ cat << END_OF_INPUT > $TOMMATH_CUTOFFS_H || die "Writing header to $TOMMATH_CUTO
*/
END_OF_INPUT
-# The Posix shell does not offer an array data type
+# The Posix shell does not offer an array data type so we create
+# the median with 'standard tools'^TM
-i=1;
-TMP=""
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
-TMP=$(median $TMP )
+# read the file (without the first line) and count the lines
+i=$(tail -n +2 $FILE_NAME | wc -l)
+# our median point will be at $i entries
+i=$(( (i / 2) + 1 ))
+TMP=$(median $FILE_NAME 1 $i)
echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP"
echo "#define MP_DEFAULT_KARATSUBA_MUL_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(km) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i )
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 2 $i)
echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP"
echo "#define MP_DEFAULT_KARATSUBA_SQR_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(ks) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 3 $i)
echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF $TMP"
echo "#define MP_DEFAULT_TOOM_MUL_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3m) Appending to $TOMMATH_CUTOFFS_H" $?
-i=$((i + 1))
-TMP=$(cat $FILE_NAME | cut -d' ' -f$i)
-TMP=$(median $TMP );
+TMP=$(median $FILE_NAME 4 $i)
echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF $TMP"
echo "#define MP_DEFAULT_TOOM_SQR_CUTOFF $TMP" >> $TOMMATH_CUTOFFS_H || die "(tc3s) Appending to $TOMMATH_CUTOFFS_H" $?