Skip to content

Commit 4058496

Browse files
author
David S. Miller
committed
[MATH-EMU]: Fix underflow exception reporting.
The underflow exception cases were wrong. This is one weird area of ieee1754 handling in that the underflow behavior changes based upon whether underflow is enabled in the trap enable mask of the FPU control register. As a specific case the Sparc V9 manual gives us the following description: -------------------- If UFM = 0: Underflow occurs if a nonzero result is tiny and a loss of accuracy occurs. Tininess may be detected before or after rounding. Loss of accuracy may be either a denormalization loss or an inexact result. If UFM = 1: Underflow occurs if a nonzero result is tiny. Tininess may be detected before or after rounding. -------------------- What this amounts to in the packing case is if we go subnormal, we set underflow if any of the following are true: 1) rounding sets inexact 2) we ended up rounding back up to normal (this is the case where we set the exponent to 1 and set the fraction to zero), this should set inexact too 3) underflow is set in FPU control register trap-enable mask The initially discovered example was "DBL_MIN / 16.0" which incorrectly generated an underflow. It should not, unless underflow is set in the trap-enable mask of the FPU csr. Another example, "0x0.0000000000001p-1022 / 16.0", should signal both inexact and underflow. The cpu implementations and ieee1754 literature is very clear about this. This is case #2 above. However, if underflow is set in the trap enable mask, only underflow should be set and reported as a trap. That is handled properly by the prioritization logic in arch/sparc{,64}/math-emu/math.c:record_exception(). Based upon a report and test case from Jakub Jelinek. Signed-off-by: David S. Miller <[email protected]>
1 parent 8b224b8 commit 4058496

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

include/asm-sparc/sfp-machine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,10 @@ extern struct task_struct *last_task_used_math;
203203
#define FP_INHIBIT_RESULTS ((last_task_used_math->thread.fsr >> 23) & _fex)
204204
#endif
205205

206+
#ifdef CONFIG_SMP
207+
#define FP_TRAPPING_EXCEPTIONS ((current->thread.fsr >> 23) & 0x1f)
208+
#else
209+
#define FP_TRAPPING_EXCEPTIONS ((last_task_used_math->thread.fsr >> 23) & 0x1f)
210+
#endif
211+
206212
#endif

include/asm-sparc64/sfp-machine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,6 @@
8888

8989
#define FP_INHIBIT_RESULTS ((current_thread_info()->xfsr[0] >> 23) & _fex)
9090

91+
#define FP_TRAPPING_EXCEPTIONS ((current_thread_info()->xfsr[0] >> 23) & 0x1f)
92+
9193
#endif

include/math-emu/op-common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,16 @@ do { \
145145
{ \
146146
X##_e = 1; \
147147
_FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \
148+
FP_SET_EXCEPTION(FP_EX_INEXACT); \
148149
} \
149150
else \
150151
{ \
151152
X##_e = 0; \
152153
_FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \
153-
FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
154154
} \
155+
if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) || \
156+
(FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
157+
FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \
155158
} \
156159
else \
157160
{ \

include/math-emu/soft-fp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,19 @@
9797
#define FP_INHIBIT_RESULTS 0
9898
#endif
9999

100+
#ifndef FP_TRAPPING_EXCEPTIONS
101+
#define FP_TRAPPING_EXCPETIONS 0
102+
#endif
103+
100104
#define FP_SET_EXCEPTION(ex) \
101105
_fex |= (ex)
102106

103107
#define FP_UNSET_EXCEPTION(ex) \
104108
_fex &= ~(ex)
105109

110+
#define FP_CUR_EXCEPTIONS \
111+
(_fex)
112+
106113
#define FP_CLEAR_EXCEPTIONS \
107114
_fex = 0
108115

0 commit comments

Comments
 (0)