/ src / optmized / log_data.h
log_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_LOG_DATA_H__
 29  #define __OPTIMIZED_LOG_DATA_H__
 30  
 31  #ifndef LOG_N
 32  #define LOG_N               7
 33  #endif
 34  #define LOG_TABLE_SIZE      (1ULL << N)
 35  #define LOG_MAX_POLYDEGREE  8
 36  
 37  /* We define default poly degree which are having ULP <= 0.5 */
 38  #ifndef LOG_POLY_DEGREE
 39  #if LOG_N == 7
 40  #define LOG_POLY_DEGREE 5
 41  #elif LOG_N == 8
 42  #define LOG_POLY_DEGREE 5
 43  #elif LOG_N == 9
 44  #define LOG_POLY_DEGREE 4
 45  #endif
 46  #endif
 47  
 48  struct log_table {
 49      uint64_t f_inv, head, tail;
 50  };
 51  
 52  static const struct {
 53      double_t ALIGN(16) poly[LOG_POLY_DEGREE];
 54      double_t ALIGN(16) poly1[4];
 55      struct {
 56          double head, tail;
 57      } ln2;
 58      struct log_table ALIGN(16) table[(1<<LOG_N) + 1];
 59      double f_inv[1<<LOG_N];
 60  } log_data = {
 61      .ln2 = {0x1.62e42fefa0000p-1, 0x1.cf79abc9e3b3ap-40 },
 62      .poly = {
 63          -0x1.0000000000000p-1,          /* -1/2 */
 64          0x1.5555555555555p-2,           /* +1/3 */
 65          -0x1.0000000000000p-2,          /* -1/4 */
 66          0x1.999999999999ap-3,           /* +1/5 */
 67  #if LOG_POLY_DEGREE >= 5
 68          -0x1.5555555555555p-3,          /* -1/6 */
 69  #if LOG_POLY_DEGREE >= 6
 70          -0x1.2492492492492p-3,          /* +1/7 */
 71  #if LOG_POLY_DEGREE >= 7
 72          -0x1.0000000000000p-3,          /* -1/8 */
 73  #endif
 74  #endif
 75  #endif
 76      },
 77      .poly1 = {
 78          0x1.55555555554e6p-4,           /* 1/2^2 * 3 */
 79          0x1.9999999bac6d4p-7,           /* 1/2^4 * 5 */
 80          0x1.2492307f1519fp-9,           /* 1/2^6 * 7 */
 81          0x1.c8034c85dfff0p-12           /* 1/2^8 * 9 */
 82      },
 83      .table = {
 84  	    /*
 85  	     * For double-precision, N = 39
 86  	     * head := 2^-N * round_to_nearest_int(2^N * log(1 + j * 2^-7)).
 87  	     * tail := log(1 + j * 2^-7) - head)
 88  	     */
 89  #if LOG_N == 7
 90    #include "data/_log_v3_tbl_128_interleaved.data"
 91  #elif LOG_N == 8
 92    #include "data/_log_v3_tbl_256_interleaved.data"
 93  #else
 94    #error "LOG_N not defined, not sure which table to use"
 95  #endif
 96      },
 97  };
 98  
 99  #define LOG_C1	  log_data.poly[0]
100  #define LOG_C2	  log_data.poly[1]
101  #define LOG_C3	  log_data.poly[2]
102  #define LOG_C4	  log_data.poly[3]
103  #define LOG_C5	  log_data.poly[4]
104  #define LOG_C6	  log_data.poly[5]
105  #define LOG_C7	  log_data.poly[6]
106  #define LOG_C8	  log_data.poly[7]
107  #define LN2_HEAD  log_data.ln2.head
108  #define LN2_TAIL  log_data.ln2.tail
109  #define LOG_TAB   log_data.table
110  
111  typedef union PACKED {
112      struct {
113          unsigned long mantissa:52;
114          unsigned long expo:11;
115          unsigned long sign:1;
116      } f;
117      uint64_t i;
118      double_t d;
119  } flt64_split_t;
120  
121  #endif