xref: /netbsd-src/lib/libc/arch/sparc/gen/fixunsdfsi.S (revision 4d12bfcd155352508213ace5ccc59ce930ea2974)
1*4d12bfcdSjoerg/*	$NetBSD: fixunsdfsi.S,v 1.6 2013/09/12 15:36:16 joerg Exp $	*/
21394f01bSchristos
30b9f5089Scgd/*
40b9f5089Scgd * Copyright (c) 1992, 1993
50b9f5089Scgd *	The Regents of the University of California.  All rights reserved.
60b9f5089Scgd *
70b9f5089Scgd * This software was developed by the Computer Systems Engineering group
80b9f5089Scgd * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
90b9f5089Scgd * contributed to Berkeley.
100b9f5089Scgd *
110b9f5089Scgd * Redistribution and use in source and binary forms, with or without
120b9f5089Scgd * modification, are permitted provided that the following conditions
130b9f5089Scgd * are met:
140b9f5089Scgd * 1. Redistributions of source code must retain the above copyright
150b9f5089Scgd *    notice, this list of conditions and the following disclaimer.
160b9f5089Scgd * 2. Redistributions in binary form must reproduce the above copyright
170b9f5089Scgd *    notice, this list of conditions and the following disclaimer in the
180b9f5089Scgd *    documentation and/or other materials provided with the distribution.
19eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
200b9f5089Scgd *    may be used to endorse or promote products derived from this software
210b9f5089Scgd *    without specific prior written permission.
220b9f5089Scgd *
230b9f5089Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
240b9f5089Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
250b9f5089Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
260b9f5089Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
270b9f5089Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
280b9f5089Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
290b9f5089Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
300b9f5089Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
310b9f5089Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
320b9f5089Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
330b9f5089Scgd * SUCH DAMAGE.
340b9f5089Scgd *
350b9f5089Scgd * from: Header: fixunsdfsi.s,v 1.3 91/10/08 00:03:15 torek Exp
360b9f5089Scgd */
370b9f5089Scgd
381394f01bSchristos#include <machine/asm.h>
390b9f5089Scgd#if defined(LIBC_SCCS) && !defined(lint)
401394f01bSchristos#if 0
410b9f5089Scgd	.asciz "@(#)fixunsdfsi.s	8.1 (Berkeley) 6/4/93"
421394f01bSchristos#else
43*4d12bfcdSjoerg	RCSID("$NetBSD: fixunsdfsi.S,v 1.6 2013/09/12 15:36:16 joerg Exp $")
441394f01bSchristos#endif
450b9f5089Scgd#endif /* LIBC_SCCS and not lint */
460b9f5089Scgd
470b9f5089Scgd/*
480b9f5089Scgd * Convert double to unsigned integer (for gcc).
490b9f5089Scgd *
500b9f5089Scgd * I have made the output for NaN agree with the Sun compiler, not
510b9f5089Scgd * that it really matters, by using `fbul,a'.
520b9f5089Scgd */
530b9f5089Scgd
540b9f5089Scgd
550b9f5089Scgd	.align	8
560b9f5089ScgdLbig:
570b9f5089Scgd	.word	0x41e00000		! .double 0r2147483648.0e+00
580b9f5089Scgd	.word	0			! (who me, not trust the assembler?)
590b9f5089Scgd
600b9f5089ScgdENTRY(__fixunsdfsi)
610b9f5089Scgd	sub	%sp, 8, %sp
620b9f5089Scgd	std	%o0, [%sp + 64]		! get argument into fpu reg
630b9f5089Scgd	ldd	[%sp + 64], %f0
64*4d12bfcdSjoerg#ifdef __PIC__
65c060b81fSpk	PICCY_SET(Lbig, %g1, %o0)
66c060b81fSpk	ldd	[%g1], %f2
67c060b81fSpk#else
680b9f5089Scgd	sethi	%hi(Lbig), %g1
690b9f5089Scgd	ldd	[%g1 + %lo(Lbig)], %f2
70c060b81fSpk#endif
710b9f5089Scgd	fcmped	%f0, %f2		! d < 2^31, or NaN, or -Inf?
720b9f5089Scgd	nop				! (fpop2 delay)
730b9f5089Scgd	fbul,a	1f			! if so, use fdtoi to convert to int
740b9f5089Scgd	fdtoi	%f0, %f0		!        (this includes negatives!)
750b9f5089Scgd
760b9f5089Scgd	! d does not fit in an int, so subtract 2^31, convert,
770b9f5089Scgd	! and add 2^31 again (sigh).  Just hope the intermediate
780b9f5089Scgd	! fits (if not, the result is undefined anyway).
790b9f5089Scgd
800b9f5089Scgd	fsubd	%f0, %f2, %f0		! d -= 2^31
810b9f5089Scgd	fdtoi	%f0, %f0		! convert to int
820b9f5089Scgd	st	%f0, [%sp + 64]		! move into return reg
830b9f5089Scgd	ld	[%sp + 64], %o0
840b9f5089Scgd	sethi	%hi(0x80000000), %o1
850b9f5089Scgd	add	%o0, %o1, %o0		! add 2^31
860b9f5089Scgd	retl
870b9f5089Scgd	add	%sp, 8, %sp
880b9f5089Scgd
890b9f5089Scgd1:
900b9f5089Scgd	st	%f0, [%sp + 64]		! return result
910b9f5089Scgd	ld	[%sp + 64], %o0
920b9f5089Scgd	retl
930b9f5089Scgd	add	%sp, 8, %sp
94