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 2005 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29         .file "rndintl.s"
  30 
  31 #include "libm.h"
  32 
  33 #undef fabs
  34 
  35         ENTRY(aintl)
  36         movq    %rsp,%rax
  37         subq    $16,%rsp
  38         fstcw   -8(%rax)
  39         fldt    8(%rax)
  40         movw    -8(%rax),%cx
  41         orw     $0x0c00,%cx
  42         movw    %cx,-4(%rax)
  43         fldcw   -4(%rax)                / set RD = to_zero
  44         frndint
  45         fstcw   -4(%rax)
  46         movw    -4(%rax),%dx
  47         andw    $0xf3ff,%dx
  48         movw    -8(%rax),%cx
  49         andw    $0x0c00,%cx
  50         orw     %dx,%cx
  51         movw    %cx,-8(%rax)
  52         fldcw   -8(%rax)                / restore RD
  53         addq    $16,%rsp
  54         ret
  55         .align  16
  56         SET_SIZE(aintl)
  57 
  58         ENTRY(irintl)
  59         movq    %rsp,%rcx
  60         subq    $16,%rsp
  61         fldt    8(%rcx)                 / load x
  62         fistpl  -8(%rcx)                / [x]
  63         fwait
  64         movslq  -8(%rcx),%rax
  65         addq    $16,%rsp
  66         ret
  67         .align  16
  68         SET_SIZE(irintl)
  69 
  70         .data
  71         .align  16
  72 half:   .float  0.5
  73 
  74         ENTRY(anintl)
  75 .Lanintl:
  76         movq    %rsp,%rcx
  77         subq    $16,%rsp
  78         fstcw   -8(%rcx)
  79         fldt    8(%rcx)
  80         movw    -8(%rcx),%dx
  81         andw    $0xf3ff,%dx
  82         movw    %dx,-4(%rcx)
  83         fldcw   -4(%rcx)                / set RD = to_nearest
  84         fld     %st(0)
  85         frndint                         / [x],x
  86         fstcw   -4(%rcx)
  87         movw    -4(%rcx),%dx
  88         andw    $0xf3ff,%dx
  89         movw    -8(%rcx),%ax
  90         andw    $0x0c00,%ax
  91         orw     %dx,%ax
  92         movw    %ax,-8(%rcx)
  93         fldcw   -8(%rcx)                / restore RD
  94         fucomi  %st(1),%st              / check if x is already an integer
  95         jp      .L0
  96         je      .L0
  97         fxch                            / x,[x]
  98         fsub    %st(1),%st              / x-[x],[x]
  99         fabs                            / |x-[x]|,[x]
 100         PIC_SETUP(1)
 101         flds    PIC_L(half)
 102         fcomip  %st(1),%st              / compare 0.5 with |x-[x]|
 103         PIC_WRAPUP
 104         je      .halfway                / if 0.5 = |x-[x]| goto halfway, 
 105                                         / most cases will not take branch.
 106 .L0:
 107         addq    $16,%rsp
 108         fstp    %st(0)
 109         ret
 110 .halfway:
 111         / x = n+0.5, recompute anint(x) as x+sign(x)*0.5
 112         fldt    8(%rcx)                 / x, 0.5, [x]
 113         movw    16(%rcx),%ax            / sign+exp part of x
 114         andw    $0x8000,%ax             / look at sign bit
 115         jnz     .x_neg
 116         faddp
 117         addq    $16,%rsp
 118         fstp    %st(1)
 119         ret
 120 .x_neg:
 121         / here, x is negative, so return x-0.5
 122         fsubp   %st,%st(1)              / x-0.5,[x]
 123         addq    $16,%rsp
 124         fstp    %st(1)
 125         ret
 126         .align  16
 127         SET_SIZE(anintl)
 128 
 129         ENTRY(nintl)
 130         pushq   %rbp
 131         movq    %rsp,%rbp
 132         subq    $16,%rsp
 133         pushq   24(%rbp)
 134         pushq   16(%rbp)
 135         call    .Lanintl                /// LOCAL
 136         fistpl  -8(%rbp)
 137         fwait
 138         movslq  -8(%rbp),%rax
 139         leave
 140         ret
 141         .align  16
 142         SET_SIZE(nintl)