From 7f28cd1f88145a701e5dbbf50558bb65fce79f61 Mon Sep 17 00:00:00 2001
From: Vicente Olivert Riera <Vincent.Riera@imgtec.com>
Date: Thu, 14 Jul 2016 17:20:51 +0100
Subject: [PATCH] Complete support for MIPS n32 ABI

Pull request: https://github.com/xianyi/OpenBLAS/pull/926

Signed-off-by: Vicente Olivert Riera <Vincent.Riera@imgtec.com>
---
 Makefile.system | 27 +++++++++------------------
 c_check         |  9 +++++++--
 f_check         |  7 ++++++-
 3 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/Makefile.system b/Makefile.system
index 24a7a64..bbcdb82 100644
--- a/Makefile.system
+++ b/Makefile.system
@@ -502,13 +502,16 @@ endif
 
 ifdef NO_BINARY_MODE
 
-ifeq ($(ARCH), $(filter $(ARCH),mips64 mips))
+ifeq ($(ARCH), $(filter $(ARCH),mips64))
 ifdef BINARY64
 CCOMMON_OPT += -mabi=64
 else
-CCOMMON_OPT += -mabi=32
+CCOMMON_OPT += -mabi=n32
 endif
 BINARY_DEFINED = 1
+else ifeq ($(ARCH), $(filter $(ARCH),mips))
+CCOMMON_OPT += -mabi=32
+BINARY_DEFINED = 1
 endif
 
 ifeq ($(CORE), LOONGSON3A)
@@ -599,12 +602,14 @@ ifneq ($(NO_LAPACK), 1)
 EXTRALIB += -lgfortran
 endif
 ifdef NO_BINARY_MODE
-ifeq ($(ARCH), $(filter $(ARCH),mips64 mips))
+ifeq ($(ARCH), $(filter $(ARCH),mips64))
 ifdef BINARY64
 FCOMMON_OPT += -mabi=64
 else
-FCOMMON_OPT += -mabi=32
+FCOMMON_OPT += -mabi=n32
 endif
+else ifeq ($(ARCH), $(filter $(ARCH),mips))
+FCOMMON_OPT += -mabi=32
 endif
 else
 ifdef BINARY64
@@ -688,20 +693,6 @@ endif
 endif
 endif
 
-ifeq ($(filter $(ARCH),mips64 mips))
-ifndef BINARY64
-FCOMMON_OPT += -m32
-else
-FCOMMON_OPT += -m64
-endif
-else
-ifdef BINARY64
-FCOMMON_OPT += -mabi=64
-else
-FCOMMON_OPT += -mabi=32
-endif
-endif
-
 ifeq ($(USE_OPENMP), 1)
 FCOMMON_OPT += -mp
 endif
diff --git a/c_check b/c_check
index 50ff360..9f457df 100644
--- a/c_check
+++ b/c_check
@@ -79,8 +79,13 @@ if ($os eq "AIX") {
     $defined = 1;
 }
 
-if (($architecture eq "mips") || ($architecture eq "mips64")) {
-    $compiler_name .= " -mabi=32" if ($binary eq "32");
+if ($architecture eq "mips") {
+    $compiler_name .= " -mabi=32";
+    $defined = 1;
+}
+
+if ($architecture eq "mips64") {
+    $compiler_name .= " -mabi=n32" if ($binary eq "32");
     $compiler_name .= " -mabi=64" if ($binary eq "64");
     $defined = 1;
 }
diff --git a/f_check b/f_check
index 4c03ac7..3520e8b 100644
--- a/f_check
+++ b/f_check
@@ -223,7 +223,12 @@ if (!$?) {
 	}
        #For gfortran MIPS
 	if ($?) {
-	    $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
+            $mips_data = `$compiler_bin -E -dM - < /dev/null`;
+            if ($mips_data =~ /_MIPS_ISA_MIPS64/) {
+	        $link = `$compiler $openmp -mabi=n32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
+	    } else {
+	        $link = `$compiler $openmp -mabi=32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
+	    }
 	}
 	$binary = "" if ($?);
     }
-- 
2.7.3

