xref: /openbsd-src/lib/libc/arch/sparc64/gen/fixunsdfsi.S (revision 50caa7fd37738b8da47f5e44620323327a24b664)
1/*	$OpenBSD: fixunsdfsi.S,v 1.5 2023/01/13 17:52:08 miod Exp $	*/
2/*	$NetBSD: fixunsdfsi.S,v 1.3 2000/07/25 04:26:12 mycroft Exp $	*/
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <machine/asm.h>
37#define _LOCORE
38#include <machine/frame.h>
39
40/*
41 * Convert double to unsigned integer (for gcc).
42 *
43 * I have made the output for NaN agree with the Sun compiler, not
44 * that it really matters, by using `fbul,a'.
45 */
46
47	.section .rodata
48	.align	8
49Lbigd:
50	.word	0x43e00000		! .double 2^63
51	.word	0			! (who me, not trust the assembler?)
52Lbigf:
53	.word	0x5f000000		! .float 2^63
54
55/*
56 *unsigned long __dtoul(double):
57 *	double -> unsigned long
58 */
59	.text
60ENTRY(__dtoul)
61	sub	%sp, 16, %sp
62	std	%f2, [%sp + 64 + BIAS + 8]
63#ifdef __PIC__
64	PIC_PROLOGUE(%o5, %o4)
65	set	Lbigd, %o4
66	ldx	[%o5 + %o4], %g1
67	ldd	[%g1], %f2
68#else
69	sethi	%hi(Lbigd), %g1
70	ldd	[%g1 + %lo(Lbigd)], %f2
71#endif
72	fcmped	%f0, %f2		! d < 2^63, or NaN, or -Inf?
73	nop				! (fpop2 delay)
74	fbul,a	1f			! if so, use fdtoi to convert to int
75	 fdtox	%f0, %f0		!        (this includes negatives!)
76
77	! d does not fit in an int, so subtract 2^63, convert,
78	! and add 2^63 again (sigh).  Just hope the intermediate
79	! fits (if not, the result is undefined anyway).
80
81	fsubd	%f0, %f2, %f0		! d -= 2^63
82	fdtox	%f0, %f0		! convert to int
83	std	%f0, [%sp + 64 + BIAS]	! move into return reg
84	ldx	[%sp + 64 + BIAS], %o0
85	sethi	%hi(0x80000000), %o1
86	sllx	%o1, 32, %o1
87	add	%o0, %o1, %o0		! add 2^63
88	ldd	[%sp + 64 + BIAS + 8], %f2
89	retl
90	 add	%sp, 16, %sp
91
921:
93	std	%f0, [%sp + 64 + BIAS]	! return result
94	ldx	[%sp + 64 + BIAS], %o0
95	ldd	[%sp + 64 + BIAS + 8], %f2
96	retl
97	 add	%sp, 16, %sp
98
99/*
100 *unsigned long __ftoul(float):
101 *	float -> unsigned long
102 */
103ENTRY(__ftoul)
104	sub	%sp, 16, %sp
105	st	%f1, [%sp + 64 + BIAS + 8]
106	st	%f2, [%sp + 64 + BIAS + 12]
107#ifdef __PIC__
108	PIC_PROLOGUE(%o5, %o4)
109	set	Lbigf, %o4
110	ldx	[%o5 + %o4], %g1
111	ld	[%g1], %f2
112#else
113	sethi	%hi(Lbigf), %g1
114	ld	[%g1 + %lo(Lbigf)], %f2
115#endif
116	fcmpes	%f0, %f2		! d < 2^63, or NaN, or -Inf?
117	nop				! (fpop2 delay)
118	fbul,a	1f			! if so, use fdtoi to convert to int
119	 fstox	%f0, %f0		!        (this includes negatives!)
120
121	! d does not fit in an int, so subtract 2^63, convert,
122	! and add 2^63 again (sigh).  Just hope the intermediate
123	! fits (if not, the result is undefined anyway).
124
125	fsubs	%f0, %f2, %f0		! d -= 2^63
126	fstox	%f0, %f0		! convert to int
127	std	%f0, [%sp + 64 + BIAS]	! move into return reg
128	ldx	[%sp + 64 + BIAS], %o0
129	sethi	%hi(0x80000000), %o1
130	sllx	%o1, 32, %o1
131	add	%o0, %o1, %o0		! add 2^63
132	ld	[%sp + 64 + BIAS + 8], %f1
133	ld	[%sp + 64 + BIAS + 12], %f2
134	retl
135	 add	%sp, 16, %sp
136
1371:
138	std	%f0, [%sp + 64 + BIAS]	! return result
139	ldx	[%sp + 64 + BIAS], %o0
140	ld	[%sp + 64 + BIAS + 8], %f1
141	ld	[%sp + 64 + BIAS + 12], %f2
142	retl
143	 add	%sp, 16, %sp
144