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 #include "libm_synonyms.h"
  34 #undef fabs
  35 
  36         .section .rodata
  37         .align  4
  38 .Lhalf: .float  0.5
  39 
  40         ENTRY(roundl)
  41         movl    %esp,%ecx
  42         subl    $8,%esp
  43         fstcw   -8(%ecx)
  44         fldt    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         addl    $8,%esp
  76         fstp    %st(0)
  77         ret
  78 2:
  79     / x = n+0.5, recompute roundl(x) as x+sign(x)*0.5
  80         fldt    4(%ecx)                 / x, 0.5, [x]
  81         movw    12(%ecx),%ax            / sign+exp of x
  82         andw    $0x8000,%ax             / look at sign bit
  83         jnz     3f
  84         faddp
  85         addl    $8,%esp
  86         fstp    %st(1)
  87         ret
  88 3:
  89         / here, x is negative, so return x-0.5
  90         fsubp   %st,%st(1)              / x-0.5,[x]
  91         addl    $8,%esp
  92         fstp    %st(1)
  93         ret
  94         .align  4
  95         SET_SIZE(roundl)