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 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 #include "lint.h"
  30 #include "base_conversion.h"
  31 #include <sys/isa_defs.h>
  32 
  33 #define CSR_DEFAULT 0x1f80
  34 
  35 /* The following should be coded as inline expansion templates.  */
  36 
  37 /*
  38  * Multiplies two normal or subnormal doubles, returns result and exceptions.
  39  */
  40 double
  41 __mul_set(double x, double y, int *pe) {
  42         int csr = CSR_DEFAULT;
  43         double z;
  44 
  45         __asm__ __volatile__(
  46             "ldmxcsr    %4\n"
  47             "mulsd      %2,%3\n"
  48             "stmxcsr    %0\n"
  49             : "=m" (csr), "=C" (z)
  50             : "C" (x), "C" (y), "m" (csr));
  51 
  52         if ((csr & 0x3f) == 0) {
  53                 *pe = 0;
  54         } else {
  55                 /* Result may not be exact. */
  56                 *pe = 1;
  57         }
  58         return (z);
  59 }
  60 
  61 /*
  62  * Divides two normal or subnormal doubles x/y, returns result and exceptions.
  63  */
  64 double
  65 __div_set(double x, double y, int *pe) {
  66         extern void _putmxcsr(), _getmxcsr();
  67         int csr;
  68         double z;
  69 
  70         _putmxcsr(CSR_DEFAULT);
  71         z = x / y;
  72         _getmxcsr(&csr);
  73         if ((csr & 0x3f) == 0) {
  74                 *pe = 0;
  75         } else {
  76                 *pe = 1;
  77         }
  78         return (z);
  79 }
  80 
  81 double
  82 __dabs(double *d)
  83 {
  84         /* should use hardware fabs instruction */
  85         return ((*d < 0.0) ? -*d : *d);
  86 }
  87 
  88 /*
  89  * Returns IEEE mode/status and
  90  * sets up standard environment for base conversion.
  91  */
  92 void
  93 __get_ieee_flags(__ieee_flags_type *b) {
  94         extern void _getmxcsr(), _putmxcsr();
  95 
  96         _getmxcsr(&b->status);
  97 
  98         /* round-to-nearest, all exceptions masked, gradual underflow */
  99         _putmxcsr(CSR_DEFAULT);
 100 }
 101 
 102 /*
 103  * Restores previous IEEE mode/status
 104  */
 105 void
 106 __set_ieee_flags(__ieee_flags_type *b) {
 107         extern void _putmxcsr();
 108 
 109         _putmxcsr(b->status);
 110 }