This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC Patch]: Implement remainder() as built-in function [PR fortran/24518]


Hello!

The motivation for this RFC patch is comment #4 to PR fortran/24518,
where MOD and MODULO fortran calls are proposed to be implemented
using __builtin_fmod() and __builtin_remainder().

Currently there are some obstacles in the implementation of fortran
front-end support:

1) missing __builtin_remainder() optab description (solved by attached patch)

2) __builtin_fmod(), __builtin_drem() and __builtin_remainder()
expansion currently depends on flag_unsafe_math_optimizations, but IMO
there is no reason for that.

Attached patch expands these three builtin functions in all cases.
Errno handling is implemented in the same way as sqrt?f2 support is
handled. The return of intrinsic x87 function is checked for NaN, and
in case NaN is detected, library function is called.

Following testcase:
--cut here--
double test(double a, double b)
{
 return remainder(a, b);
}
--cut here--

compiles to:
test:
       pushl   %ebp
       movl    %esp, %ebp
       subl    $8, %esp
       fldl    8(%ebp)
       fldl    16(%ebp)
       fld     %st(1)
       fld     %st(1)
       fxch    %st(1)
.L2:
       fprem1
       fnstsw  %ax
       sahf
       jp      .L2
       fstp    %st(1)
       fstpl   -8(%ebp)
       fldl    -8(%ebp)
       fucom   %st(0)
       fnstsw  %ax
       sahf
       jp      .L11
       jne     .L11
       fstp    %st(1)
       fstp    %st(1)
       leave
       ret
       .p2align 4,,7
.L11:
       fstp    %st(0)
       fstpl   16(%ebp)
       fstpl   8(%ebp)
       leave
       jmp     remainder

When fno-math-errno is used, testcase compiles to:

test:
       pushl   %ebp
       movl    %esp, %ebp
       subl    $8, %esp
       fldl    8(%ebp)
       fldl    16(%ebp)
       fxch    %st(1)
.L2:
       fprem1
       fnstsw  %ax
       sahf
       jp      .L2
       fstp    %st(1)
       fstpl   -8(%ebp)
       fldl    -8(%ebp)
       leave
       ret

(fstpl/fldl pair at the end is in fact XFmode->DFmode fixup).

and using -ffast-math:

test:
       pushl   %ebp
       movl    %esp, %ebp
       fldl    8(%ebp)
       fldl    16(%ebp)
       fxch    %st(1)
.L2:
       fprem1
       fnstsw  %ax
       sahf
       jp      .L2
       fstp    %st(1)
       popl    %ebp
       ret


2006-10-23 Uros Bizjak <uros@kss-loka.si>


	* optabs.h (enum optab_index): Add new OTI_remainder.
	(remainder_optab): Define corresponding macro.
	* optabs.c (init_optabs): Initialize remainder_optab.
	* genopinit.c (optabs): Implement remainder_optab using
	remainder?f3 patterns.
	* builtins.c (expand_builtin_mathfn_2): Handle
	BUILT_IN_REMAINDER{,F,L} using remainder_optab.
	(expand_builtin): Expand BUILT_IN_REMAINDER{,F,L} using
	expand_builtin_mathfn_2.

	(expand_builtin) [BUILT_IN_FMOD], [BUILT_IN_DREM]: Do not
	depend on flag_unsafe_math_optimizations.

	* config/i386/i386.md ("remaindersf3", "remainderdf3")
	("remainderxf3"): New expanders to implement remainderf,
	remainder and remainderl built-ins as inline x87 intrinsics.

	("fpremxf4", "fprem1xf4"): Do not depend on
	flag_unsafe_math_optimizations.
	("fmodsf3", "fmoddf3", "fmodxf3"): Do not depend on
	flag_unsafe_math_optimizations.  Use truncxf?f expander
	instead of truncxf?f_i387_noop.
	("dremsf3", "dremdf3", "dremxf3"): Do not depend on
	flag_unsafe_math_optimizations. Use truncxf?f expander
	instead of truncxf?f_i387_noop.

Uros.

Attachment: i386-remainder.diff
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]