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