/ src / optmized / exp_data.h
exp_data.h
  1  /*
  2   * Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
  3   *
  4   * Redistribution and use in source and binary forms, with or without modification,
  5   * are permitted provided that the following conditions are met:
  6   * 1. Redistributions of source code must retain the above copyright notice,
  7   *    this list of conditions and the following disclaimer.
  8   * 2. Redistributions in binary form must reproduce the above copyright notice,
  9   *    this list of conditions and the following disclaimer in the documentation
 10   *    and/or other materials provided with the distribution.
 11   * 3. Neither the name of the copyright holder nor the names of its contributors
 12   *    may be used to endorse or promote products derived from this software without
 13   *    specific prior written permission.
 14   *
 15   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 18   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 19   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 20   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 21   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 22   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 24   * POSSIBILITY OF SUCH DAMAGE.
 25   *
 26   */
 27  
 28  #ifndef __OPTIMIZED_EXP_H__
 29  #define __OPTIMIZED_EXP_H__
 30  
 31  
 32  #ifndef EXP_N
 33  #define EXP_N 7
 34  #endif
 35  
 36  #define EXP_TABLE_SIZE (1ULL << EXP_N)
 37  
 38  #if EXP_N == 6                              /* Table size 64 */
 39  #define EXP_POLY_DEGREE 6
 40  
 41  #elif EXP_N == 7                            /* Table size 128 */
 42  #define EXP_POLY_DEGREE 5
 43  
 44  #elif EXP_N == 8                            /* Table size 256 */
 45  #define EXP_POLY_DEGREE 4
 46  
 47  #elif EXP_N == 9                            /* Table size 512 */
 48  #define EXP_POLY_DEGREE 4
 49  
 50  #elif EXP_N == 10                           /* Table size 1024 */
 51  #define EXP_POLY_DEGREE 4
 52  #endif
 53  
 54  #define EXP_MAX_POLYDEGREE 8
 55  
 56  /*
 57   * tblsz_byln2 = (-1) x (log(2) / TABLE_SIZE);
 58   * head = asuint64(tblsz_byln2)
 59   * tail = tblsz_byln2 - head
 60   */
 61  struct exp_table {
 62      double main, head, tail;
 63  };
 64  
 65  static const struct {
 66      double Huge;
 67      double tblsz_byln2;
 68      struct {
 69          double head, tail;
 70      } ln2by_tblsz;
 71      double ALIGN(16) poly[EXP_MAX_POLYDEGREE];
 72      struct exp_table table[EXP_TABLE_SIZE];
 73  } exp_data = {
 74  #if EXP_N == 10
 75      .tblsz_byln2	   =  0x1.71547652b82fep+10,
 76      .ln2by_tblsz       = {-0x1.62e42fefa0000p-11, -0x1.cf79abc9e3b3ap-50},
 77  #elif EXP_N == 9
 78      .tblsz_byln2	   =  0x1.71547652b82fep+9,
 79      .ln2by_tblsz       = {-0x1.62e42fefa0000p-10, -0x1.cf79abc9e3b39p-49}
 80  #elif EXP_N == 8
 81      .tblsz_byln2	   =  0x1.71547652b82fep+8,
 82      .ln2by_tblsz       = {-0x1.62e42fefa0000p-9, -0x1.cf79abc9e3b39p-48},
 83  #elif EXP_N == 7
 84      .tblsz_byln2	   =  0x1.71547652b82fep+7,
 85      .ln2by_tblsz       = {-0x1.62e42fefa0000p-8, -0x1.cf79abc9e3b39p-47},
 86  #elif EXP_N == 6
 87      .tblsz_byln2	   =  0x1.71547652b82fep+6,
 88      .ln2by_tblsz       = {-0x1.62e42fefa0000p-7, -0x1.cf79abc9e3b39p-46},
 89  #else
 90  #error "N not defined"
 91  #endif
 92      /*
 93       * Polynomial constants, 1/x! (reciprocal of factorial(x))
 94       * To make better use of cache line, we dont store 0! and 1!
 95       */
 96      .poly = {	/* skip for 0! and 1! */
 97          0x1.0000000000000p-1,	/* 1/2! = 1/2    */
 98          0x1.5555555555555p-3,	/* 1/3! = 1/6    */
 99          0x1.5555555555555p-5,	/* 1/4! = 1/24   */
100          0x1.1111111111111p-7 ,	/* 1/5! = 1/120  */
101  #if EXP_POLY_DEGREE >= 5
102          0x1.6c16c16c16c17p-10,	/* 1/6! = 1/720  */
103  #if EXP_POLY_DEGREE >= 6
104          0x1.a01a01a01a01ap-13,	/* 1/7! = 1/5040 */
105  #if EXP_POLY_DEGREE >= 7
106          0x1.a01a01a01a01ap-16,	/* 1/8! = 1/40320*/
107  #if EXP_POLY_DEGREE >= 8
108          0x1.71de3a556c734p-19,	/* 1/9! = 1/322880*/
109  #endif
110  #endif
111  #endif
112  #endif
113      },
114      .Huge		   = 0x1.8p+52,
115  
116      .table = {
117  #if EXP_N == 6
118  #include "data/_exp_tbl_64_interleaved.data"
119  
120  #elif EXP_N == 7
121  #include "data/_exp_tbl_128_interleaved.data"
122  
123  #endif
124      },
125  };
126  
127  /* C1 is 1 as 1! = 1 and 1/1! = 1 */
128  #define EXP_C2	exp_data.poly[0]
129  #define EXP_C3	exp_data.poly[1]
130  #define EXP_C4	exp_data.poly[2]
131  #define EXP_C5	exp_data.poly[3]
132  #define EXP_C6	exp_data.poly[4]
133  #define EXP_C7	exp_data.poly[5]
134  #define EXP_C8	exp_data.poly[6]
135  #define EXP_HUGE	exp_data.Huge
136  
137  #define EXP_TBLSZ_BY_LN2	exp_data.tblsz_byln2
138  #define EXP_LN2_BY_TBLSZ_HEAD	exp_data.ln2by_tblsz.head
139  #define EXP_LN2_BY_TBLSZ_TAIL	exp_data.ln2by_tblsz.tail
140  #define EXP_TABLE_DATA		exp_data.table
141  
142  
143  #endif	/* OPTIMIZIED_EXP_H */