From cbc72622a9e4df8c80dea759a3ed0d37e50179af Mon Sep 17 00:00:00 2001
From: Nick Hung <nick@andestech.com>
Date: Fri, 12 Dec 2014 11:10:10 +0800
Subject: [PATCH] [PATCH,NDS32] libm nds32

---
 newlib/configure.host                  |  4 +-
 newlib/libm/machine/configure.in       |  1 +
 newlib/libm/machine/nds32/Makefile.am  | 30 +++++++++++++++
 newlib/libm/machine/nds32/configure.in | 35 +++++++++++++++++
 newlib/libm/machine/nds32/w_sqrt.S     | 68 ++++++++++++++++++++++++++++++++++
 newlib/libm/machine/nds32/wf_sqrt.S    | 68 ++++++++++++++++++++++++++++++++++
 6 files changed, 204 insertions(+), 2 deletions(-)
 create mode 100644 newlib/libm/machine/nds32/Makefile.am
 create mode 100644 newlib/libm/machine/nds32/configure.in
 create mode 100644 newlib/libm/machine/nds32/w_sqrt.S
 create mode 100644 newlib/libm/machine/nds32/wf_sqrt.S

diff --git a/newlib/configure.host b/newlib/configure.host
index c7a0401..9f530ec 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -238,9 +238,9 @@ case "${host_cpu}" in
 	;;
   nds32*)
 	machine_dir=nds32
-	newlib_cflags="${newlib_cflags} -DPREFER_SIZE_OVER_SPEED "
-	newlib_cflags="${newlib_cflags} -DSMALL_MEMORY "
+	libm_machine_dir=nds32
 	newlib_cflags="${newlib_cflags} -ffunction-sections -fdata-sections"
+	newlib_cflags="${newlib_cflags} -DHAVE_RENAME"
 	;;
   nios2*)
 	machine_dir=nios2
diff --git a/newlib/libm/machine/configure.in b/newlib/libm/machine/configure.in
index 85c5c41..b1a40e3 100644
--- a/newlib/libm/machine/configure.in
+++ b/newlib/libm/machine/configure.in
@@ -28,6 +28,7 @@ if test -n "${libm_machine_dir}"; then
 	aarch64) AC_CONFIG_SUBDIRS(aarch64) ;;
 	arm) AC_CONFIG_SUBDIRS(arm) ;;
 	i386) AC_CONFIG_SUBDIRS(i386) ;;
+	nds32) AC_CONFIG_SUBDIRS(nds32) ;;
 	spu) AC_CONFIG_SUBDIRS(spu) ;;
   esac;
   if test "${use_libtool}" = "yes"; then
diff --git a/newlib/libm/machine/nds32/Makefile.am b/newlib/libm/machine/nds32/Makefile.am
new file mode 100644
index 0000000..b5afbd3
--- /dev/null
+++ b/newlib/libm/machine/nds32/Makefile.am
@@ -0,0 +1,30 @@
+## Process this file with automake to generate Makefile.in
+
+AUTOMAKE_OPTIONS = cygnus
+
+INCLUDES = -I $(newlib_basedir)/../newlib/libm/common $(NEWLIB_CFLAGS) \
+	$(CROSS_CFLAGS) $(TARGET_CFLAGS)
+
+if HAS_NDS32_FPU_SP_FALSE
+GPR_SOURCES =
+else
+FPU_SP_SOURCES = wf_sqrt.S
+endif
+
+if HAS_NDS32_FPU_DP_FALSE
+else
+FPU_DP_SOURCES = w_sqrt.S
+endif
+
+LIB_SOURCES = $(GPR_SOURCES) $(FPU_SP_SOURCES) $(FPU_DP_SOURCES)
+
+noinst_LIBRARIES = lib.a
+lib_a_SOURCES = $(LIB_SOURCES)
+lib_a_CFLAGS = $(AM_CFLAGS)
+lib_a_CCASFLAGS = $(AM_CCASFLAGS)
+noinst_DATA =
+
+include $(srcdir)/../../../Makefile.shared
+
+ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
+CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host
diff --git a/newlib/libm/machine/nds32/configure.in b/newlib/libm/machine/nds32/configure.in
new file mode 100644
index 0000000..2b7a3c0
--- /dev/null
+++ b/newlib/libm/machine/nds32/configure.in
@@ -0,0 +1,35 @@
+dnl This is the newlib/libm/machine/nds32 configure.in file.
+dnl Process this file with autoconf to produce a configure script.
+
+AC_PREREQ(2.59)
+AC_INIT([newlib],[NEWLIB_VERSION])
+AC_CONFIG_SRCDIR([Makefile.am])
+
+dnl Can't be done in NEWLIB_CONFIGURE because that confuses automake.
+AC_CONFIG_AUX_DIR(../../../..)
+
+NEWLIB_CONFIGURE(../../..)
+
+
+dnl Use builtin macro to detect if FPU extension support is on.
+AC_NO_EXECUTABLES
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+[[#if (__NDS32_EXT_FPU_SP__)
+warning: Has nds32 FPU SP extension support
+#endif
+]])],
+                  [has_nds32_fpu_sp="no"],
+		  [has_nds32_fpu_sp="yes"])
+AM_CONDITIONAL(HAS_NDS32_FPU_SP, test "$has_nds32_fpu_sp" = "yes")
+
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+[[#if (__NDS32_EXT_FPU_DP__)
+warning: Has nds32 FPU DP extension support
+#endif
+]])],
+                  [has_nds32_fpu_dp="no"],
+		  [has_nds32_fpu_dp="yes"])
+AM_CONDITIONAL(HAS_NDS32_FPU_DP, test "$has_nds32_fpu_dp" = "yes")
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/newlib/libm/machine/nds32/w_sqrt.S b/newlib/libm/machine/nds32/w_sqrt.S
new file mode 100644
index 0000000..a3c543d
--- /dev/null
+++ b/newlib/libm/machine/nds32/w_sqrt.S
@@ -0,0 +1,68 @@
+/*
+Copyright (c) 2013-2014 Andes Technology Corporation.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+
+    Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+    The name of the company may not be used to endorse or promote
+    products derived from this software without specific prior written
+    permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED.  IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+	.text
+	.align	1
+	.global  sqrt
+	.type    sqrt, @function
+sqrt:
+	/* The input argument is supposed to be stored in $fd0.
+	   The return value is supposed to be stored in $fd0 either.  */
+
+	/* Clear the IEEE cumulative exceptions flags. ($fpcsr.b[6:2])  */
+	FMFCSR  $r0
+	bitci   $r0, $r0, #0b1111100
+	FMTCSR	$r0
+
+	fsqrtd   $fd0, $fd0
+
+	/* Check the IEEE cumulative exceptions flags.  */
+	FMFCSR	$r0
+	bmski33 $r0, #2		/* Is $fpcsr.IVO('b2) set ?  */
+	bnez	$r0, .L_EDOM	/* Set errno as EDOM.  */
+
+	bmski33 $r0, #4		/* Is $fpcsr.OVF('b4) set ?  */
+	bnez	$r0, .L_ERANGE	/* Set errno as ERANGE.  */
+
+	bmski33 $r0, #5		/* Is $fpcsr.UDF('b5) set ?  */
+	bnez	$r0, .L_ERANGE	/* Set errno as ERANGE.  */
+
+	/* No error at all.  Just ret.  */
+	ret
+
+.L_EDOM:
+	movi	$r0, #33	/* EDOM: Math arg out of domain of func.  */
+	j	.L_Set_errno
+.L_ERANGE:
+	movi	$r0, #34	/* ERANGE: Math result not representable.  */
+.L_Set_errno:
+	l.w	$r15, _impure_ptr
+	swi	$r0, [$r15]	/* Set errno.  */
+	ret
+	.size sqrt, .-sqrt
diff --git a/newlib/libm/machine/nds32/wf_sqrt.S b/newlib/libm/machine/nds32/wf_sqrt.S
new file mode 100644
index 0000000..e000d75
--- /dev/null
+++ b/newlib/libm/machine/nds32/wf_sqrt.S
@@ -0,0 +1,68 @@
+/*
+Copyright (c) 2013-2014 Andes Technology Corporation.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+
+    Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+    The name of the company may not be used to endorse or promote
+    products derived from this software without specific prior written
+    permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED.  IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+	.text
+	.align	1
+	.global  sqrtf
+	.type    sqrtf, @function
+sqrtf:
+	/* The input argument is supposed to be stored in $fs0.
+	   The return value is supposed to be stored in $fs0 either.  */
+
+	/* Clear the IEEE cumulative exceptions flags. ($fpcsr.b[6:2])  */
+	FMFCSR  $r0
+	bitci   $r0, $r0, #0b1111100
+	FMTCSR	$r0
+
+	fsqrts  $fs0, $fs0
+
+	/* Check the IEEE cumulative exceptions flags.  */
+	FMFCSR	$r5
+	bmski33 $r5, #2		/* Is $fpcsr.IVO('b2) set ?  */
+	bnez	$r5, .L_EDOM	/* Set errno as EDOM.  */
+
+	bmski33 $r5, #4		/* Is $fpcsr.OVF('b4) set ?  */
+	bnez	$r5, .L_ERANGE	/* Set errno as ERANGE.  */
+
+	bmski33 $r5, #5		/* Is $fpcsr.UDF('b5) set ?  */
+	bnez	$r5, .L_ERANGE	/* Set errno as ERANGE.  */
+
+	/* No error at all.  Just ret.  */
+	ret
+
+.L_EDOM:
+	movi	$r0, #33	/* EDOM: Math arg out of domain of func.  */
+	j	.L_Set_errno
+.L_ERANGE:
+	movi	$r0, #34	/* ERANGE: Math result not representable.  */
+.L_Set_errno:
+	l.w	$r15, _impure_ptr
+	swi	$r0, [$r15]	/* Set errno.  */
+	ret
+	.size sqrtf, .-sqrtf
-- 
1.9.1

