#!/bin/sh

# substitute fast versions of memcmp32, memset32 and memcpy32 if they exist

EXT=$1
RT=$2
SRC=$3

grep -n __memcmp32 $SRC.$EXT | cut -f1 -d: >grep.out
if [ -s grep.out ]; then
  line1=$(head -1 grep.out)
  line2=$(tail -1 grep.out)
  line1=`expr $line1 - 1`
  line2=`expr $line2 + 1`
  sed -n "1,${line1}p" $SRC.$EXT >xpart1
  sed -n "${line2},\$p" $SRC.$EXT >xpart3
  cat xpart1 rt/$RT/memcmp32.$EXT xpart3 >$SRC.$EXT
  echo "memcmp32 compiled source code replaced by fast assembly" $line1 $line2
fi

grep -n __memcpy32 $SRC.$EXT | cut -f1 -d: >grep.out
if [ -s grep.out ]; then
  line1=$(head -1 grep.out)
  line2=$(tail -1 grep.out)
  line1=`expr $line1 - 1`
  line2=`expr $line2 + 1`
  sed -n "1,${line1}p" $SRC.$EXT >xpart1
  sed -n "${line2},\$p" $SRC.$EXT >xpart3
  cat xpart1 rt/$RT/memcpy32.$EXT xpart3 >$SRC.$EXT
  echo "memcpy32 compiled source code replaced by fast assembly" $line1 $line2
fi

grep -n __memset32 $SRC.$EXT | cut -f1 -d: >grep.out
if [ -s grep.out ]; then
  line1=$(head -1 grep.out)
  line2=$(tail -1 grep.out)
  line1=`expr $line1 - 1`
  line2=`expr $line2 + 1`
  sed -n "1,${line1}p" $SRC.$EXT >xpart1
  sed -n "${line2},\$p" $SRC.$EXT >xpart3
  cat xpart1 rt/$RT/memset32.$EXT xpart3 >$SRC.$EXT
  echo "memset32 compiled source code replaced by fast assembly" $line1 $line2
fi

rm -f grep.out xpart1 xpart3
