Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1738443

[PATCH 4.13 028/109] MIPS: math-emu: <MADDF|MSUBF>.D: Fix accuracy (64-bit case)

From Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Newsgroups linux.kernel
Subject [PATCH 4.13 028/109] MIPS: math-emu: <MADDF|MSUBF>.D: Fix accuracy (64-bit case)
Date 2017-09-24 23:00 +0200
Message-ID <utmam-50G-25@gated-at.bofh.it> (permalink)
References <utm0F-4Xd-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


4.13-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Douglas Leung <douglas.leung@imgtec.com>

commit 2cfa58259f4b65b33ebe8f167019a1f89c6c3289 upstream.

Implement fused multiply-add with correct accuracy.

Fused multiply-add operation has better accuracy than respective
sequential execution of multiply and add operations applied on the
same inputs. This is because accuracy errors accumulate in latter
case.

This patch implements fused multiply-add with the same accuracy
as it is implemented in hardware, using 128-bit intermediate
calculations.

One test case example (raw bits) that this patch fixes:

MADDF.D fd,fs,ft:
  fd = 0x00000ca000000000
  fs = ft = 0x3f40624dd2f1a9fc

Fixes: e24c3bec3e8e ("MIPS: math-emu: Add support for the MIPS R6 MADDF FPU instruction")
Fixes: 83d43305a1df ("MIPS: math-emu: Add support for the MIPS R6 MSUBF FPU instruction")

Signed-off-by: Douglas Leung <douglas.leung@imgtec.com>
Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
Cc: Douglas Leung <douglas.leung@imgtec.com>
Cc: Bo Hu <bohu@google.com>
Cc: James Hogan <james.hogan@imgtec.com>
Cc: Jin Qian <jinqian@google.com>
Cc: Paul Burton <paul.burton@imgtec.com>
Cc: Petar Jovanovic <petar.jovanovic@imgtec.com>
Cc: Raghu Gandham <raghu.gandham@imgtec.com>
Cc: linux-mips@linux-mips.org
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/16891/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/mips/math-emu/dp_maddf.c |  133 +++++++++++++++++++++++++++++-------------
 1 file changed, 94 insertions(+), 39 deletions(-)

--- a/arch/mips/math-emu/dp_maddf.c
+++ b/arch/mips/math-emu/dp_maddf.c
@@ -15,18 +15,44 @@
 #include "ieee754dp.h"
 
 
+/* 128 bits shift right logical with rounding. */
+void srl128(u64 *hptr, u64 *lptr, int count)
+{
+	u64 low;
+
+	if (count >= 128) {
+		*lptr = *hptr != 0 || *lptr != 0;
+		*hptr = 0;
+	} else if (count >= 64) {
+		if (count == 64) {
+			*lptr = *hptr | (*lptr != 0);
+		} else {
+			low = *lptr;
+			*lptr = *hptr >> (count - 64);
+			*lptr |= (*hptr << (128 - count)) != 0 || low != 0;
+		}
+		*hptr = 0;
+	} else {
+		low = *lptr;
+		*lptr = low >> count | *hptr << (64 - count);
+		*lptr |= (low << (64 - count)) != 0;
+		*hptr = *hptr >> count;
+	}
+}
+
 static union ieee754dp _dp_maddf(union ieee754dp z, union ieee754dp x,
 				 union ieee754dp y, enum maddf_flags flags)
 {
 	int re;
 	int rs;
-	u64 rm;
 	unsigned lxm;
 	unsigned hxm;
 	unsigned lym;
 	unsigned hym;
 	u64 lrm;
 	u64 hrm;
+	u64 lzm;
+	u64 hzm;
 	u64 t;
 	u64 at;
 	int s;
@@ -172,7 +198,7 @@ static union ieee754dp _dp_maddf(union i
 	ym <<= 64 - (DP_FBITS + 1);
 
 	/*
-	 * Multiply 64 bits xm, ym to give high 64 bits rm with stickness.
+	 * Multiply 64 bits xm and ym to give 128 bits result in hrm:lrm.
 	 */
 
 	/* 32 * 32 => 64 */
@@ -202,81 +228,110 @@ static union ieee754dp _dp_maddf(union i
 
 	hrm = hrm + (t >> 32);
 
-	rm = hrm | (lrm != 0);
-
-	/*
-	 * Sticky shift down to normal rounding precision.
-	 */
-	if ((s64) rm < 0) {
-		rm = (rm >> (64 - (DP_FBITS + 1 + 3))) |
-		     ((rm << (DP_FBITS + 1 + 3)) != 0);
+	/* Put explicit bit at bit 126 if necessary */
+	if ((int64_t)hrm < 0) {
+		lrm = (hrm << 63) | (lrm >> 1);
+		hrm = hrm >> 1;
 		re++;
-	} else {
-		rm = (rm >> (64 - (DP_FBITS + 1 + 3 + 1))) |
-		     ((rm << (DP_FBITS + 1 + 3 + 1)) != 0);
 	}
-	assert(rm & (DP_HIDDEN_BIT << 3));
 
-	if (zc == IEEE754_CLASS_ZERO)
-		return ieee754dp_format(rs, re, rm);
+	assert(hrm & (1 << 62));
 
-	/* And now the addition */
-	assert(zm & DP_HIDDEN_BIT);
+	if (zc == IEEE754_CLASS_ZERO) {
+		/*
+		 * Move explicit bit from bit 126 to bit 55 since the
+		 * ieee754dp_format code expects the mantissa to be
+		 * 56 bits wide (53 + 3 rounding bits).
+		 */
+		srl128(&hrm, &lrm, (126 - 55));
+		return ieee754dp_format(rs, re, lrm);
+	}
 
-	/*
-	 * Provide guard,round and stick bit space.
-	 */
-	zm <<= 3;
+	/* Move explicit bit from bit 52 to bit 126 */
+	lzm = 0;
+	hzm = zm << 10;
+	assert(hzm & (1 << 62));
 
+	/* Make the exponents the same */
 	if (ze > re) {
 		/*
 		 * Have to shift y fraction right to align.
 		 */
 		s = ze - re;
-		rm = XDPSRS(rm, s);
+		srl128(&hrm, &lrm, s);
 		re += s;
 	} else if (re > ze) {
 		/*
 		 * Have to shift x fraction right to align.
 		 */
 		s = re - ze;
-		zm = XDPSRS(zm, s);
+		srl128(&hzm, &lzm, s);
 		ze += s;
 	}
 	assert(ze == re);
 	assert(ze <= DP_EMAX);
 
+	/* Do the addition */
 	if (zs == rs) {
 		/*
-		 * Generate 28 bit result of adding two 27 bit numbers
-		 * leaving result in xm, xs and xe.
+		 * Generate 128 bit result by adding two 127 bit numbers
+		 * leaving result in hzm:lzm, zs and ze.
 		 */
-		zm = zm + rm;
-
-		if (zm >> (DP_FBITS + 1 + 3)) { /* carry out */
-			zm = XDPSRS1(zm);
+		hzm = hzm + hrm + (lzm > (lzm + lrm));
+		lzm = lzm + lrm;
+		if ((int64_t)hzm < 0) {        /* carry out */
+			srl128(&hzm, &lzm, 1);
 			ze++;
 		}
 	} else {
-		if (zm >= rm) {
-			zm = zm - rm;
+		if (hzm > hrm || (hzm == hrm && lzm >= lrm)) {
+			hzm = hzm - hrm - (lzm < lrm);
+			lzm = lzm - lrm;
 		} else {
-			zm = rm - zm;
+			hzm = hrm - hzm - (lrm < lzm);
+			lzm = lrm - lzm;
 			zs = rs;
 		}
-		if (zm == 0)
+		if (lzm == 0 && hzm == 0)
 			return ieee754dp_zero(ieee754_csr.rm == FPU_CSR_RD);
 
 		/*
-		 * Normalize to rounding precision.
+		 * Put explicit bit at bit 126 if necessary.
 		 */
-		while ((zm >> (DP_FBITS + 3)) == 0) {
-			zm <<= 1;
-			ze--;
+		if (hzm == 0) {
+			/* left shift by 63 or 64 bits */
+			if ((int64_t)lzm < 0) {
+				/* MSB of lzm is the explicit bit */
+				hzm = lzm >> 1;
+				lzm = lzm << 63;
+				ze -= 63;
+			} else {
+				hzm = lzm;
+				lzm = 0;
+				ze -= 64;
+			}
+		}
+
+		t = 0;
+		while ((hzm >> (62 - t)) == 0)
+			t++;
+
+		assert(t <= 62);
+		if (t) {
+			hzm = hzm << t | lzm >> (64 - t);
+			lzm = lzm << t;
+			ze -= t;
 		}
 	}
 
-	return ieee754dp_format(zs, ze, zm);
+	/*
+	 * Move explicit bit from bit 126 to bit 55 since the
+	 * ieee754dp_format code expects the mantissa to be
+	 * 56 bits wide (53 + 3 rounding bits).
+	 */
+	srl128(&hzm, &lzm, (126 - 55));
+
+	return ieee754dp_format(zs, ze, lzm);
 }
 
 union ieee754dp ieee754dp_maddf(union ieee754dp z, union ieee754dp x,

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 4.13 000/109] 4.13.4-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 058/109] scsi: qedi: off by one in qedi_get_cmd_from_tid() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 041/109] powerpc/powernv/npu: Move tlb flush before launching ATSD Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 088/109] media: venus: fix copy/paste error in return_buf_error Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 066/109] scsi: sg: fixup infoleak when using SG_GET_REQUEST_TABLE Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 031/109] crypto: scompress - dont sleep with preemption disabled Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 060/109] scsi: megaraid_sas: mismatch of allocated MFI frame size and length exposed in MFI MPT pass through command Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 039/109] ext4: fix quota inconsistency during orphan cleanup for read-only mounts Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 067/109] scsi: qla2xxx: Update fw_started flags at qpair creation. Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 054/109] scsi: zfcp: fix missing trace records for early returns in TMF eh handlers Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 083/109] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 028/109] MIPS: math-emu: <MADDF|MSUBF>.D: Fix accuracy (64-bit case) Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 049/109] skd: Submit requests to firmware before triggering the doorbell Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 085/109] s390/mm: fix race on mm->context.flush_mm Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 062/109] scsi: megaraid_sas: Check valid aen class range to avoid kernel panic Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 036/109] wcn36xx: Introduce mutual exclusion of fw configuration Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 037/109] ext4: in ext4_seek_{hole,data}, return -ENXIO for negative offsets Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 078/109] tracing: Fix clear of RECORDED_TGID flag when disabling trace event Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 051/109] scsi: zfcp: add handling for FCP_RESID_OVER to the fcp ingress path Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 059/109] scsi: aacraid: Fix command send race condition Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 027/109] MIPS: math-emu: <MADDF|MSUBF>.S: Fix accuracy (32-bit case) Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 052/109] scsi: zfcp: fix capping of unsuccessful GPN_FT SAN response trace records Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 079/109] tracing: Apply trace_clock changes to instance max buffer Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 055/109] scsi: zfcp: fix payload with full FCP_RSP IU in SCSI trace records Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 026/109] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Clean up "maddf_flags" enumeration Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 029/109] docs: disable KASLR when debugging kernel Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 043/109] powerpc: Fix DAR reporting when alignment handler faults Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 046/109] md/bitmap: copy correct data for bitmap super Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 075/109] ftrace: Fix selftest goto location on error Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 081/109] PCI: shpchp: Enable bridge bus mastering if MSI is enabled Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:00 +0200
  [PATCH 4.13 018/109] MIPS: math-emu: <MAX|MAXA|MIN|MINA>.<D|S>: Fix cases of both inputs zero Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 024/109] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Fix some cases of infinite inputs Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 016/109] Input: i8042 - add Gigabyte P57 to the keyboard reset table Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 008/109] Input: xpad - validate USB endpoint type during probe Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 020/109] MIPS: math-emu: <MAXA|MINA>.<D|S>: Fix cases of input values with opposite signs Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 023/109] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Fix NaN propagation Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 009/109] drm/amdgpu: read reg in each iterator of psp_wait_for loop Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 010/109] tty: improve tty_insert_flip_char() fast path Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 014/109] pinctrl: samsung: Fix NULL pointer exception on external interrupts on S3C24xx Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 022/109] MIPS: math-emu: MINA.<D|S>: Fix some cases of infinity and zero inputs Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 011/109] tty: improve tty_insert_flip_char() slow path Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 019/109] MIPS: math-emu: <MAX|MIN>.<D|S>: Fix cases of both inputs negative Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 001/109] orangefs: Dont clear SGID when inheriting ACLs Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
    Re: [PATCH 4.13 001/109] orangefs: Dont clear SGID when inheriting ACLs Mike Marshall <hubcap@omnibond.com> - 2017-09-26 02:10 +0200
  [PATCH 4.13 004/109] IB/{qib, hfi1}: Avoid flow control testing for RDMA write operation Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 006/109] srcu: Provide ordering for CPU not involved in grace period Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  [PATCH 4.13 017/109] MIPS: math-emu: <MAX|MAXA|MIN|MINA>.<D|S>: Fix quiet NaN propagation Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-24 23:10 +0200
  Re: [PATCH 4.13 000/109] 4.13.4-stable review Guenter Roeck <linux@roeck-us.net> - 2017-09-25 03:10 +0200
    Re: [PATCH 4.13 000/109] 4.13.4-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-25 08:40 +0200
  Re: [PATCH 4.13 000/109] 4.13.4-stable review Shuah Khan <shuahkh@osg.samsung.com> - 2017-09-26 01:20 +0200
    Re: [PATCH 4.13 000/109] 4.13.4-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-09-26 09:40 +0200

csiph-web