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         extern void _putmxcsr(), _getmxcsr();
  43         int csr;
  44         volatile double z;
  45 
  46         _putmxcsr(CSR_DEFAULT);
  47         /*
  48          * NOTE:  By declaring 'z' volatile above, we intend for the
  49          * compiler to not move this after the _getmxcsr() call, whose results
  50          * are only useful if they immediately follow the multiply.
  51          *
  52          * From the C11 spec:
  53          *
  54          * 134 - A volatile declaration may be used to describe an
  55          * object corresponding to a memory-mapped input/output port
  56          * or an object accessed by an asynchronously interrupting
  57          * function. Actions on objects so declared shall by an
  58          * implementation or reordered except as permitted by the
  59          * rules for evaluating expressions."
  60          */
  61         z = x * y;
  62         _getmxcsr(&csr);
  63         if ((csr & 0x3f) == 0) {
  64                 *pe = 0;
  65         } else {
  66                 /* Result may not be exact. */
  67                 *pe = 1;
  68         }
  69         return (z);
  70 }
  71 
  72 /*
  73  * Divides two normal or subnormal doubles x/y, returns result and exceptions.
  74  */
  75 double
  76 __div_set(double x, double y, int *pe) {
  77         extern void _putmxcsr(), _getmxcsr();
  78         int csr;
  79         volatile double z;
  80 
  81         _putmxcsr(CSR_DEFAULT);
  82         /* NOTE:  See __mul_set() above, same principle applies here. */
  83         z = x / y;
  84         _getmxcsr(&csr);
  85         if ((csr & 0x3f) == 0) {
  86                 *pe = 0;
  87         } else {
  88                 *pe = 1;
  89         }
  90         return (z);
  91 }
  92 
  93 double
  94 __dabs(double *d)
  95 {
  96         /* should use hardware fabs instruction */
  97         return ((*d < 0.0) ? -*d : *d);
  98 }
  99 
 100 /*
 101  * Returns IEEE mode/status and
 102  * sets up standard environment for base conversion.
 103  */
 104 void
 105 __get_ieee_flags(__ieee_flags_type *b) {
 106         extern void _getmxcsr(), _putmxcsr();
 107 
 108         _getmxcsr(&b->status);
 109 
 110         /* round-to-nearest, all exceptions masked, gradual underflow */
 111         _putmxcsr(CSR_DEFAULT);
 112 }
 113 
 114 /*
 115  * Restores previous IEEE mode/status
 116  */
 117 void
 118 __set_ieee_flags(__ieee_flags_type *b) {
 119         extern void _putmxcsr();
 120 
 121         _putmxcsr(b->status);
 122 }