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 #undef fabs
  34 
  35         .section .rodata
  36         .align  4
  37 .Lhalf: .float  0.5
  38 
  39         ENTRY(lround)
  40         movl    %esp,%ecx
  41         subl    $8,%esp
  42         fstcw   -8(%ecx)
  43         fldl    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         fstp    %st(0)
  75 1:
  76         fistpl  -8(%ecx)
  77         fwait
  78         movl    -8(%ecx),%eax
  79         addl    $8,%esp
  80         ret
  81 2:
  82     / x = n+0.5, recompute lround(x) as x+sign(x)*0.5
  83         fldl    4(%ecx)                 / x, 0.5, [x]
  84         movl    8(%ecx),%eax            / high part of x
  85         andl    $0x80000000,%eax
  86         jnz     3f
  87         faddp
  88         fstp    %st(1)
  89         jmp     1b
  90 3:
  91         / here, x is negative, so return x-0.5
  92         fsubp   %st,%st(1)              / x-0.5,[x]
  93         fstp    %st(1)
  94         jmp     1b
  95         .align  4
  96         SET_SIZE(lround)