1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 23 */ 24 /* 25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 .file "roundl.s" 30 31 #include "libm.h" 32 LIBM_ANSI_PRAGMA_WEAK(roundl,function) 33 #undef fabs 34 35 .section .rodata 36 .align 4 37 .Lhalf: .float 0.5 38 39 ENTRY(roundl) 40 movl %esp,%ecx 41 subl $8,%esp 42 fstcw -8(%ecx) 43 fldt 4(%ecx) 44 movw -8(%ecx),%dx 45 andw $0xf3ff,%dx 46 movw %dx,-4(%ecx) 47 fldcw -4(%ecx) / set RD = to_nearest 48 fld %st(0) 49 frndint / [x],x 50 fstcw -4(%ecx) 51 movw -4(%ecx),%dx 52 andw $0xf3ff,%dx 53 movw -8(%ecx),%ax 54 andw $0x0c00,%ax 55 orw %dx,%ax 56 movw %ax,-8(%ecx) 57 fldcw -8(%ecx) / restore RD 58 fucom / check if x is already an integer 59 fstsw %ax 60 sahf 61 jp 0f 62 je 0f 63 fxch / x,[x] 64 fsub %st(1),%st / x-[x],[x] 65 fabs / |x-[x]|,[x] 66 PIC_SETUP(1) 67 fcoms PIC_L(.Lhalf) 68 PIC_WRAPUP 69 fnstsw %ax 70 sahf 71 jae 2f / if |x-[x]| = 0.5 goto halfway, 72 / most cases will not take branch. 73 0: 74 addl $8,%esp 75 fstp %st(0) 76 ret 77 2: 78 / x = n+0.5, recompute roundl(x) as x+sign(x)*0.5 79 fldt 4(%ecx) / x, 0.5, [x] 80 movw 12(%ecx),%ax / sign+exp of x 81 andw $0x8000,%ax / look at sign bit 82 jnz 3f 83 faddp 84 addl $8,%esp 85 fstp %st(1) 86 ret 87 3: 88 / here, x is negative, so return x-0.5 89 fsubp %st,%st(1) / x-0.5,[x] 90 addl $8,%esp 91 fstp %st(1) 92 ret 93 .align 4 94 SET_SIZE(roundl)