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 "lround.s" 30 31 #include "libm.h" 32 LIBM_ANSI_PRAGMA_WEAK(lround,function) 33 #include "libm_synonyms.h" 34 #undef fabs 35 36 .section .rodata 37 .align 4 38 .Lhalf: .float 0.5 39 40 ENTRY(lround) 41 movl %esp,%ecx 42 subl $8,%esp 43 fstcw -8(%ecx) 44 fldl 4(%ecx) 45 movw -8(%ecx),%dx 46 andw $0xf3ff,%dx 47 movw %dx,-4(%ecx) 48 fldcw -4(%ecx) / set RD = to_nearest 49 fld %st(0) 50 frndint / [x],x 51 fstcw -4(%ecx) 52 movw -4(%ecx),%dx 53 andw $0xf3ff,%dx 54 movw -8(%ecx),%ax 55 andw $0x0c00,%ax 56 orw %dx,%ax 57 movw %ax,-8(%ecx) 58 fldcw -8(%ecx) / restore RD 59 fucom / check if x is already an integer 60 fstsw %ax 61 sahf 62 jp 0f 63 je 0f 64 fxch / x,[x] 65 fsub %st(1),%st / x-[x],[x] 66 fabs / |x-[x]|,[x] 67 PIC_SETUP(1) 68 fcoms PIC_L(.Lhalf) 69 PIC_WRAPUP 70 fnstsw %ax 71 sahf 72 jae 2f / if |x-[x]| = 0.5 goto halfway, 73 / most cases will not take branch. 74 0: 75 fstp %st(0) 76 1: 77 fistpl -8(%ecx) 78 fwait 79 movl -8(%ecx),%eax 80 addl $8,%esp 81 ret 82 2: 83 / x = n+0.5, recompute lround(x) as x+sign(x)*0.5 84 fldl 4(%ecx) / x, 0.5, [x] 85 movl 8(%ecx),%eax / high part of x 86 andl $0x80000000,%eax 87 jnz 3f 88 faddp 89 fstp %st(1) 90 jmp 1b 91 3: 92 / here, x is negative, so return x-0.5 93 fsubp %st,%st(1) / x-0.5,[x] 94 fstp %st(1) 95 jmp 1b 96 .align 4 97 SET_SIZE(lround)