📄 s_log1p.s
字号:
.file "log1p.s" // Copyright (C) 2000, 2001, Intel Corporation// All rights reserved.// // Contributed 2/2/2000 by John Harrison, Ted Kubaska, Bob Norin, Shane Story,// and Ping Tak Peter Tang of the Computational Software Lab, Intel Corporation.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are// met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.//// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in the// documentation and/or other materials provided with the distribution.//// * The name of Intel Corporation may not be used to endorse or promote// products derived from this software without specific prior written// permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // Intel Corporation is the author of this code, and requests that all// problem reports or change requests be submitted to it directly at // http://developer.intel.com/opensource.//// History//==============================================================// 2/02/00 Initial version// 4/04/00 Unwind support added// 8/15/00 Bundle added after call to __libm_error_support to properly// set [the previously overwritten] GR_Parameter_RESULT.//// *********************************************************************//// Function: log1p(x) = ln(x+1), for double precision x values//// *********************************************************************//// Accuracy: Very accurate for double precision values//// *********************************************************************//// Resources Used://// Floating-Point Registers: f8 (Input and Return Value)// f9,f33-f55,f99 //// General Purpose Registers:// r32-r53// r54-r57 (Used to pass arguments to error handling routine)//// Predicate Registers: p6-p15//// *********************************************************************//// IEEE Special Conditions://// Denormal fault raised on denormal inputs// Overflow exceptions cannot occur // Underflow exceptions raised when appropriate for log1p // (Error Handling Routine called for underflow)// Inexact raised when appropriate by algorithm//// log1p(inf) = inf// log1p(-inf) = QNaN // log1p(+/-0) = +/-0 // log1p(-1) = -inf // log1p(SNaN) = QNaN// log1p(QNaN) = QNaN// log1p(EM_special Values) = QNaN//// *********************************************************************//// Computation is based on the following kernel.//// ker_log_64( in_FR : X,// in_FR : E,// in_FR : Em1,// in_GR : Expo_Range,// out_FR : Y_hi,// out_FR : Y_lo,// out_FR : Scale,// out_PR : Safe )// // Overview//// The method consists of three cases.//// If |X+Em1| < 2^(-80) use case log1p_small;// elseif |X+Em1| < 2^(-7) use case log_near1;// else use case log_regular;//// Case log1p_small://// log( 1 + (X+Em1) ) can be approximated by (X+Em1).//// Case log_near1://// log( 1 + (X+Em1) ) can be approximated by a simple polynomial// in W = X+Em1. This polynomial resembles the truncated Taylor// series W - W^/2 + W^3/3 - ...// // Case log_regular://// Here we use a table lookup method. The basic idea is that in// order to compute log(Arg) for an argument Arg in [1,2), we // construct a value G such that G*Arg is close to 1 and that// log(1/G) is obtainable easily from a table of values calculated// beforehand. Thus//// log(Arg) = log(1/G) + log(G*Arg)// = log(1/G) + log(1 + (G*Arg - 1))//// Because |G*Arg - 1| is small, the second term on the right hand// side can be approximated by a short polynomial. We elaborate// this method in four steps.//// Step 0: Initialization//// We need to calculate log( E + X ). Obtain N, S_hi, S_lo such that//// E + X = 2^N * ( S_hi + S_lo ) exactly//// where S_hi in [1,2) and S_lo is a correction to S_hi in the sense// that |S_lo| <= ulp(S_hi).//// Step 1: Argument Reduction//// Based on S_hi, obtain G_1, G_2, G_3 from a table and calculate//// G := G_1 * G_2 * G_3// r := (G * S_hi - 1) + G * S_lo//// These G_j's have the property that the product is exactly // representable and that |r| < 2^(-12) as a result.//// Step 2: Approximation////// log(1 + r) is approximated by a short polynomial poly(r).//// Step 3: Reconstruction////// Finally, log( E + X ) is given by//// log( E + X ) = log( 2^N * (S_hi + S_lo) )// ~=~ N*log(2) + log(1/G) + log(1 + r)// ~=~ N*log(2) + log(1/G) + poly(r).//// **** Algorithm ****//// Case log1p_small://// Although log(1 + (X+Em1)) is basically X+Em1, we would like to // preserve the inexactness nature as well as consistent behavior// under different rounding modes. Note that this case can only be// taken if E is set to be 1.0. In this case, Em1 is zero, and that// X can be very tiny and thus the final result can possibly underflow.// Thus, we compare X against a threshold that is dependent on the// input Expo_Range. If |X| is smaller than this threshold, we set// SAFE to be FALSE. //// The result is returned as Y_hi, Y_lo, and in the case of SAFE // is FALSE, an additional value Scale is also returned. //// W := X + Em1// Threshold := Threshold_Table( Expo_Range )// Tiny := Tiny_Table( Expo_Range )//// If ( |W| > Threshold ) then// Y_hi := W// Y_lo := -W*W// Else// Y_hi := W// Y_lo := -Tiny// Scale := 2^(-100)// Safe := FALSE// EndIf////// One may think that Y_lo should be -W*W/2; however, it does not matter// as Y_lo will be rounded off completely except for the correct effect in // directed rounding. Clearly -W*W is simplier to compute. Moreover,// because of the difference in exponent value, Y_hi + Y_lo or // Y_hi + Scale*Y_lo is always inexact.//// Case log_near1://// Here we compute a simple polynomial. To exploit parallelism, we split// the polynomial into two portions.// // W := X + Em1// Wsq := W * W// W4 := Wsq*Wsq// W6 := W4*Wsq// Y_hi := W + Wsq*(P_1 + W*(P_2 + W*(P_3 + W*P_4))// Y_lo := W6*(P_5 + W*(P_6 + W*(P_7 + W*P_8)))// set lsb(Y_lo) to be 1//// Case log_regular://// We present the algorithm in four steps.//// Step 0. Initialization// ----------------------//// Z := X + E// N := unbaised exponent of Z// S_hi := 2^(-N) * Z// S_lo := 2^(-N) * { (max(X,E)-Z) + min(X,E) }//// Note that S_lo is always 0 for the case E = 0.//// Step 1. Argument Reduction// --------------------------//// Let//// Z = 2^N * S_hi = 2^N * 1.d_1 d_2 d_3 ... d_63//// We obtain G_1, G_2, G_3 by the following steps.////// Define X_0 := 1.d_1 d_2 ... d_14. This is extracted// from S_hi.//// Define A_1 := 1.d_1 d_2 d_3 d_4. This is X_0 truncated// to lsb = 2^(-4).//// Define index_1 := [ d_1 d_2 d_3 d_4 ].//// Fetch Z_1 := (1/A_1) rounded UP in fixed point with// fixed point lsb = 2^(-15).// Z_1 looks like z_0.z_1 z_2 ... z_15// Note that the fetching is done using index_1.// A_1 is actually not needed in the implementation// and is used here only to explain how is the value// Z_1 defined.//// Fetch G_1 := (1/A_1) truncated to 21 sig. bits.// floating pt. Again, fetching is done using index_1. A_1// explains how G_1 is defined.//// Calculate X_1 := X_0 * Z_1 truncated to lsb = 2^(-14)// = 1.0 0 0 0 d_5 ... d_14// This is accomplised by integer multiplication.// It is proved that X_1 indeed always begin// with 1.0000 in fixed point.////// Define A_2 := 1.0 0 0 0 d_5 d_6 d_7 d_8. This is X_1 // truncated to lsb = 2^(-8). Similar to A_1,// A_2 is not needed in actual implementation. It// helps explain how some of the values are defined.//// Define index_2 := [ d_5 d_6 d_7 d_8 ].//// Fetch Z_2 := (1/A_2) rounded UP in fixed point with// fixed point lsb = 2^(-15). Fetch done using index_2.// Z_2 looks like z_0.z_1 z_2 ... z_15//// Fetch G_2 := (1/A_2) truncated to 21 sig. bits.// floating pt.//// Calculate X_2 := X_1 * Z_2 truncated to lsb = 2^(-14)// = 1.0 0 0 0 0 0 0 0 d_9 d_10 ... d_14// This is accomplised by integer multiplication.// It is proved that X_2 indeed always begin// with 1.00000000 in fixed point.////// Define A_3 := 1.0 0 0 0 0 0 0 0 d_9 d_10 d_11 d_12 d_13 1.// This is 2^(-14) + X_2 truncated to lsb = 2^(-13).//// Define index_3 := [ d_9 d_10 d_11 d_12 d_13 ].//// Fetch G_3 := (1/A_3) truncated to 21 sig. bits.// floating pt. Fetch is done using index_3.//// Compute G := G_1 * G_2 * G_3. //// This is done exactly since each of G_j only has 21 sig. bits.//// Compute //// r := (G*S_hi - 1) + G*S_lo using 2 FMA operations.//// thus, r approximates G*(S_hi+S_lo) - 1 to within a couple of // rounding errors.////// Step 2. Approximation// ---------------------//// This step computes an approximation to log( 1 + r ) where r is the// reduced argument just obtained. It is proved that |r| <= 1.9*2^(-13);// thus log(1+r) can be approximated by a short polynomial://// log(1+r) ~=~ poly = r + Q1 r^2 + ... + Q4 r^5////// Step 3. Reconstruction// ----------------------//// This step computes the desired result of log(X+E)://// log(X+E) = log( 2^N * (S_hi + S_lo) )// = N*log(2) + log( S_hi + S_lo )// = N*log(2) + log(1/G) +// log(1 + C*(S_hi+S_lo) - 1 )//// log(2), log(1/G_j) are stored as pairs of (single,double) numbers:// log2_hi, log2_lo, log1byGj_hi, log1byGj_lo. The high parts are// single-precision numbers and the low parts are double precision// numbers. These have the property that//// N*log2_hi + SUM ( log1byGj_hi )//// is computable exactly in double-extended precision (64 sig. bits).// Finally//// Y_hi := N*log2_hi + SUM ( log1byGj_hi )// Y_lo := poly_hi + [ poly_lo + // ( SUM ( log1byGj_lo ) + N*log2_lo ) ]// set lsb(Y_lo) to be 1//#include "libm_support.h"#ifdef _LIBC.rodata#else.data#endif// P_7, P_6, P_5, P_4, P_3, P_2, and P_1 .align 64Constants_P:ASM_TYPE_DIRECTIVE(Constants_P,@object)data4 0xEFD62B15,0xE3936754,0x00003FFB,0x00000000data4 0xA5E56381,0x8003B271,0x0000BFFC,0x00000000data4 0x73282DB0,0x9249248C,0x00003FFC,0x00000000data4 0x47305052,0xAAAAAA9F,0x0000BFFC,0x00000000data4 0xCCD17FC9,0xCCCCCCCC,0x00003FFC,0x00000000data4 0x00067ED5,0x80000000,0x0000BFFD,0x00000000data4 0xAAAAAAAA,0xAAAAAAAA,0x00003FFD,0x00000000data4 0xFFFFFFFE,0xFFFFFFFF,0x0000BFFD,0x00000000ASM_SIZE_DIRECTIVE(Constants_P) // log2_hi, log2_lo, Q_4, Q_3, Q_2, and Q_1 .align 64Constants_Q:ASM_TYPE_DIRECTIVE(Constants_Q,@object)data4 0x00000000,0xB1721800,0x00003FFE,0x00000000 data4 0x4361C4C6,0x82E30865,0x0000BFE2,0x00000000data4 0x328833CB,0xCCCCCAF2,0x00003FFC,0x00000000data4 0xA9D4BAFB,0x80000077,0x0000BFFD,0x00000000data4 0xAAABE3D2,0xAAAAAAAA,0x00003FFD,0x00000000data4 0xFFFFDAB7,0xFFFFFFFF,0x0000BFFD,0x00000000ASM_SIZE_DIRECTIVE(Constants_Q) // Z1 - 16 bit fixed, G1 and H1 - IEEE single .align 64Constants_Z_G_H_h1:ASM_TYPE_DIRECTIVE(Constants_Z_G_H_h1,@object)data4 0x00008000,0x3F800000,0x00000000,0x00000000,0x00000000,0x00000000data4 0x00007879,0x3F70F0F0,0x3D785196,0x00000000,0x617D741C,0x3DA163A6data4 0x000071C8,0x3F638E38,0x3DF13843,0x00000000,0xCBD3D5BB,0x3E2C55E6data4 0x00006BCB,0x3F579430,0x3E2FF9A0,0x00000000,0xD86EA5E7,0xBE3EB0BFdata4 0x00006667,0x3F4CCCC8,0x3E647FD6,0x00000000,0x86B12760,0x3E2E6A8Cdata4 0x00006187,0x3F430C30,0x3E8B3AE7,0x00000000,0x5C0739BA,0x3E47574Cdata4 0x00005D18,0x3F3A2E88,0x3EA30C68,0x00000000,0x13E8AF2F,0x3E20E30Fdata4 0x0000590C,0x3F321640,0x3EB9CEC8,0x00000000,0xF2C630BD,0xBE42885Bdata4 0x00005556,0x3F2AAAA8,0x3ECF9927,0x00000000,0x97E577C6,0x3E497F34data4 0x000051EC,0x3F23D708,0x3EE47FC5,0x00000000,0xA6B0A5AB,0x3E3E6A6Edata4 0x00004EC5,0x3F1D89D8,0x3EF8947D,0x00000000,0xD328D9BE,0xBDF43E3Cdata4 0x00004BDB,0x3F17B420,0x3F05F3A1,0x00000000,0x0ADB090A,0x3E4094C3data4 0x00004925,0x3F124920,0x3F0F4303,0x00000000,0xFC1FE510,0xBE28FBB2data4 0x0000469F,0x3F0D3DC8,0x3F183EBF,0x00000000,0x10FDE3FA,0x3E3A7895data4 0x00004445,0x3F088888,0x3F20EC80,0x00000000,0x7CC8C98F,0x3E508CE5data4 0x00004211,0x3F042108,0x3F29516A,0x00000000,0xA223106C,0xBE534874ASM_SIZE_DIRECTIVE(Constants_Z_G_H_h1) // Z2 - 16 bit fixed, G2 and H2 - IEEE single .align 64 Constants_Z_G_H_h2:ASM_TYPE_DIRECTIVE(Constants_Z_G_H_h2,@object)data4 0x00008000,0x3F800000,0x00000000,0x00000000,0x00000000,0x00000000data4 0x00007F81,0x3F7F00F8,0x3B7F875D,0x00000000,0x22C42273,0x3DB5A116data4 0x00007F02,0x3F7E03F8,0x3BFF015B,0x00000000,0x21F86ED3,0x3DE620CFdata4 0x00007E85,0x3F7D08E0,0x3C3EE393,0x00000000,0x484F34ED,0xBDAFA07Edata4 0x00007E08,0x3F7C0FC0,0x3C7E0586,0x00000000,0x3860BCF6,0xBDFE07F0data4 0x00007D8D,0x3F7B1880,0x3C9E75D2,0x00000000,0xA78093D6,0x3DEA370Fdata4 0x00007D12,0x3F7A2328,0x3CBDC97A,0x00000000,0x72A753D0,0x3DFF5791data4 0x00007C98,0x3F792FB0,0x3CDCFE47,0x00000000,0xA7EF896B,0x3DFEBE6Cdata4 0x00007C20,0x3F783E08,0x3CFC15D0,0x00000000,0x409ECB43,0x3E0CF156data4 0x00007BA8,0x3F774E38,0x3D0D874D,0x00000000,0xFFEF71DF,0xBE0B6F97data4 0x00007B31,0x3F766038,0x3D1CF49B,0x00000000,0x5D59EEE8,0xBE080483data4 0x00007ABB,0x3F757400,0x3D2C531D,0x00000000,0xA9192A74,0x3E1F91E9data4 0x00007A45,0x3F748988,0x3D3BA322,0x00000000,0xBF72A8CD,0xBE139A06data4 0x000079D1,0x3F73A0D0,0x3D4AE46F,0x00000000,0xF8FBA6CF,0x3E1D9202data4 0x0000795D,0x3F72B9D0,0x3D5A1756,0x00000000,0xBA796223,0xBE1DCCC4data4 0x000078EB,0x3F71D488,0x3D693B9D,0x00000000,0xB6B7C239,0xBE049391ASM_SIZE_DIRECTIVE(Constants_Z_G_H_h2) // G3 and H3 - IEEE single and h3 -IEEE double .align 64 Constants_Z_G_H_h3:ASM_TYPE_DIRECTIVE(Constants_Z_G_H_h3,@object)data4 0x3F7FFC00,0x38800100,0x562224CD,0x3D355595data4 0x3F7FF400,0x39400480,0x06136FF6,0x3D8200A2data4 0x3F7FEC00,0x39A00640,0xE8DE9AF0,0x3DA4D68Ddata4 0x3F7FE400,0x39E00C41,0xB10238DC,0xBD8B4291data4 0x3F7FDC00,0x3A100A21,0x3B1952CA,0xBD89CCB8data4 0x3F7FD400,0x3A300F22,0x1DC46826,0xBDB10707data4 0x3F7FCC08,0x3A4FF51C,0xF43307DB,0x3DB6FCB9data4 0x3F7FC408,0x3A6FFC1D,0x62DC7872,0xBD9B7C47data4 0x3F7FBC10,0x3A87F20B,0x3F89154A,0xBDC3725Edata4 0x3F7FB410,0x3A97F68B,0x62B9D392,0xBD93519Ddata4 0x3F7FAC18,0x3AA7EB86,0x0F21BD9D,0x3DC18441data4 0x3F7FA420,0x3AB7E101,0x2245E0A6,0xBDA64B95data4 0x3F7F9C20,0x3AC7E701,0xAABB34B8,0x3DB4B0ECdata4 0x3F7F9428,0x3AD7DD7B,0x6DC40A7E,0x3D992337data4 0x3F7F8C30,0x3AE7D474,0x4F2083D3,0x3DC6E17Bdata4 0x3F7F8438,0x3AF7CBED,0x811D4394,0x3DAE314Bdata4 0x3F7F7C40,0x3B03E1F3,0xB08F2DB1,0xBDD46F21data4 0x3F7F7448,0x3B0BDE2F,0x6D34522B,0xBDDC30A4data4 0x3F7F6C50,0x3B13DAAA,0xB1F473DB,0x3DCB0070data4 0x3F7F6458,0x3B1BD766,0x6AD282FD,0xBDD65DDCdata4 0x3F7F5C68,0x3B23CC5C,0xF153761A,0xBDCDAB83data4 0x3F7F5470,0x3B2BC997,0x341D0F8F,0xBDDADA40data4 0x3F7F4C78,0x3B33C711,0xEBC394E8,0x3DCD1BD7data4 0x3F7F4488,0x3B3BBCC6,0x52E3E695,0xBDC3532Bdata4 0x3F7F3C90,0x3B43BAC0,0xE846B3DE,0xBDA3961Edata4 0x3F7F34A0,0x3B4BB0F4,0x785778D4,0xBDDADF06data4 0x3F7F2CA8,0x3B53AF6D,0xE55CE212,0x3DCC3ED1data4 0x3F7F24B8,0x3B5BA620,0x9E382C15,0xBDBA3103data4 0x3F7F1CC8,0x3B639D12,0x5C5AF197,0x3D635A0Bdata4 0x3F7F14D8,0x3B6B9444,0x71D34EFC,0xBDDCCB19data4 0x3F7F0CE0,0x3B7393BC,0x52CD7ADA,0x3DC74502data4 0x3F7F04F0,0x3B7B8B6D,0x7D7F2A42,0xBDB68F17ASM_SIZE_DIRECTIVE(Constants_Z_G_H_h3) // // Exponent Thresholds and Tiny Thresholds// for 8, 11, 15, and 17 bit exponents// // Expo_Range Value// // 0 (8 bits) 2^(-126)// 1 (11 bits) 2^(-1022)// 2 (15 bits) 2^(-16382)// 3 (17 bits) 2^(-16382)// // Tiny_Table// ----------// Expo_Range Value// // 0 (8 bits) 2^(-16382)// 1 (11 bits) 2^(-16382)// 2 (15 bits) 2^(-16382)// 3 (17 bits) 2^(-16382)// .align 64 Constants_Threshold:ASM_TYPE_DIRECTIVE(Constants_Threshold,@object)data4 0x00000000,0x80000000,0x00003F81,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000data4 0x00000000,0x80000000,0x00003C01,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000data4 0x00000000,0x80000000,0x00000001,0x00000000ASM_SIZE_DIRECTIVE(Constants_Threshold).align 64Constants_1_by_LN10:ASM_TYPE_DIRECTIVE(Constants_1_by_LN10,@object)data4 0x37287195,0xDE5BD8A9,0x00003FFD,0x00000000data4 0xACCF70C8,0xD56EAABE,0x00003FBD,0x00000000ASM_SIZE_DIRECTIVE(Constants_1_by_LN10)FR_Input_X = f8 FR_Neg_One = f9FR_E = f33FR_Em1 = f34FR_Y_hi = f34 // Shared with Em1FR_Y_lo = f35FR_Scale = f36FR_X_Prime = f37 FR_Z = f38 FR_S_hi = f38 // Shared with Z FR_W = f39FR_G = f40FR_wsq = f40 // Shared with G FR_H = f41FR_w4 = f41// Shared with H FR_h = f42FR_w6 = f42 // Shared with h FR_G_tmp = f43FR_poly_lo = f43// Shared with G_tmp FR_P8 = f43 // Shared with G_tmp FR_H_tmp = f44FR_poly_hi = f44 // Shared with H_tmpFR_P7 = f44 // Shared with H_tmpFR_h_tmp = f45 FR_rsq = f45 // Shared with h_tmpFR_P6 = f45// Shared with h_tmpFR_abs_W = f46FR_r = f46
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -