#!/usr/bin/env bash

# Copyright ©2015 The gonum Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

WARNING='//\
// Float32 implementations are autogenerated and not directly tested.\
'

# Level1 routines.

echo Generating level1single.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level1single.go
cat level1double.go \
| gofmt -r 'blas.Float64Level1 -> blas.Float32Level1' \
\
| gofmt -r 'float64 -> float32' \
| gofmt -r 'blas.DrotmParams -> blas.SrotmParams' \
\
| gofmt -r 'asm.DaxpyInc -> asm.SaxpyInc' \
| gofmt -r 'asm.DaxpyUnitary -> asm.SaxpyUnitary' \
| gofmt -r 'asm.DdotInc -> asm.SdotInc' \
| gofmt -r 'asm.DdotUnitary -> asm.SdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1S\2_" \
      -e 's_^// D_// S_' \
      -e "s_^\(func (Implementation) \)Id\(.*\)\$_$WARNING\1Is\2_" \
      -e 's_^// Id_// Is_' \
      -e 's_"math"_math "github.com/gonum/blas/native/internal/math32"_' \
>> level1single.go

echo Generating level1single_sdot.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level1single_sdot.go
cat level1double_ddot.go \
| gofmt -r 'float64 -> float32' \
\
| gofmt -r 'asm.DdotInc -> asm.SdotInc' \
| gofmt -r 'asm.DdotUnitary -> asm.SdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1S\2_" \
      -e 's_^// D_// S_' \
>> level1single_sdot.go

echo Generating level1single_dsdot.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level1single_dsdot.go
cat level1double_ddot.go \
| gofmt -r '[]float64 -> []float32' \
\
| gofmt -r 'asm.DdotInc -> asm.DsdotInc' \
| gofmt -r 'asm.DdotUnitary -> asm.DsdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1Ds\2_" \
      -e 's_^// D_// Ds_' \
>> level1single_dsdot.go

echo Generating level1single_sdsdot.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level1single_sdsdot.go
cat level1double_ddot.go \
| gofmt -r 'float64 -> float32' \
\
| gofmt -r 'asm.DdotInc(x, y, f(n), f(incX), f(incY), f(ix), f(iy)) -> alpha + float32(asm.DsdotInc(x, y, f(n), f(incX), f(incY), f(ix), f(iy)))' \
| gofmt -r 'asm.DdotUnitary(a, b) -> alpha + float32(asm.DsdotUnitary(a, b))' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1Sds\2_" \
      -e 's_^// D\(.*\)$_// Sds\1 plus a constant_' \
      -e 's_\\sum_alpha + \\sum_' \
      -e 's/n int/n int, alpha float32/' \
>> level1single_sdsdot.go


# Level2 routines.

echo Generating level2single.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level2single.go
cat level2double.go \
| gofmt -r 'blas.Float64Level2 -> blas.Float32Level2' \
\
| gofmt -r 'float64 -> float32' \
\
| gofmt -r 'Dscal -> Sscal' \
\
| gofmt -r 'asm.DaxpyInc -> asm.SaxpyInc' \
| gofmt -r 'asm.DaxpyUnitary -> asm.SaxpyUnitary' \
| gofmt -r 'asm.DdotInc -> asm.SdotInc' \
| gofmt -r 'asm.DdotUnitary -> asm.SdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1S\2_" \
      -e 's_^// D_// S_' \
>> level2single.go


# Level3 routines.

echo Generating level3single.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > level3single.go
cat level3double.go \
| gofmt -r 'blas.Float64Level3 -> blas.Float32Level3' \
\
| gofmt -r 'float64 -> float32' \
\
| gofmt -r 'asm.DaxpyUnitary -> asm.SaxpyUnitary' \
| gofmt -r 'asm.DdotUnitary -> asm.SdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1S\2_" \
      -e 's_^// D_// S_' \
>> level3single.go

echo Generating general_single.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > general_single.go
cat general_double.go \
| gofmt -r 'float64 -> float32' \
\
| gofmt -r 'general64 -> general32' \
| gofmt -r 'newGeneral64 -> newGeneral32' \
\
| sed -e 's/(g general64) print()/(g general32) print()/' \
      -e 's_"math"_math "github.com/gonum/blas/native/internal/math32"_' \
>> general_single.go

echo Generating sgemm.go
echo -e '// Generated code do not edit. Run `go generate`.\n' > sgemm.go
cat dgemm.go \
| gofmt -r 'float64 -> float32' \
| gofmt -r 'general64 -> general32' \
\
| gofmt -r 'dgemmParallel -> sgemmParallel' \
| gofmt -r 'computeNumBlocks64 -> computeNumBlocks32' \
| gofmt -r 'dgemmSerial -> sgemmSerial' \
| gofmt -r 'dgemmSerialNotNot -> sgemmSerialNotNot' \
| gofmt -r 'dgemmSerialTransNot -> sgemmSerialTransNot' \
| gofmt -r 'dgemmSerialNotTrans -> sgemmSerialNotTrans' \
| gofmt -r 'dgemmSerialTransTrans -> sgemmSerialTransTrans' \
\
| gofmt -r 'asm.DaxpyInc -> asm.SaxpyInc' \
| gofmt -r 'asm.DaxpyUnitary -> asm.SaxpyUnitary' \
| gofmt -r 'asm.DdotInc -> asm.SdotInc' \
| gofmt -r 'asm.DdotUnitary -> asm.SdotUnitary' \
\
| sed -e "s_^\(func (Implementation) \)D\(.*\)\$_$WARNING\1S\2_" \
      -e 's_^// D_// S_' \
      -e 's_^// d_// s_' \
>> sgemm.go
