4 // Copyright (c) 2000 - 2003, Intel Corporation
5 // All rights reserved.
7 // Contributed 2000 by the Intel Numerics Group, Intel Corporation
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
20 // * The name of Intel Corporation may not be used to endorse or promote
21 // products derived from this software without specific prior written
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
28 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // Intel Corporation is the author of this code, and requests that all
37 // problem reports or change requests be submitted to it directly at
38 // http://www.intel.com/software/products/opensource/libraries/num.htm.
41 //==============================================================
42 // 02/03/00 Initial version
43 // 05/26/00 Fix bug when x a double-extended denormal;
44 // if x=0 call error routine, per C9X
45 // 08/15/00 Bundle added after call to __libm_error_support to properly
46 // set [the previously overwritten] GR_Parameter_RESULT.
47 // 01/20/01 Fixed result for x=0, corrected error tag value.
48 // 05/20/02 Cleaned up namespace and sf0 syntax
49 // 01/20/03 Improved performance
52 //==============================================================
53 // int ilogb( double x );
55 // Overview of operation
56 //==============================================================
57 // The ilogb function extracts the exponent of x as an integer
58 // and returns it in r8
60 // ilogb is similar to logb but differs in the following ways:
62 // ilogb: returns INT_MAX
64 // Nan returns FP_LOGBNAN (which is either INT_MAX or INT_MIN)
65 // ilogb: returns INT_MAX (7fffffff)
66 // logb: returns QNAN (quietized SNAN)
67 // 0 returns FP_ILOGB0 (which is either INT_MIN or -INT_MAX)
68 // ilogb: returns -INT_MAX (80000001)
69 // logb: returns -inf, raises the divide-by-zero exception,
70 // and calls libm_error_support to set domain error
73 //==============================================================
74 // general registers used:
76 // r36 -> r39 used as parameters to error path
78 // predicate registers used:
80 // floating-point registers used:
98 GR_Parameter_RESULT = r38
99 GR_Parameter_TAG = r39
106 GLOBAL_LIBM_ENTRY(ilogb)
109 // TrueExp_x = exp(f8) - 0xffff
112 getf.exp rSignexp_x = f8
113 fclass.m p8,p0 = f8, 0x0b // Test for x unorm
114 mov rExpBias = 0xffff // Exponent bias
118 fnorm.s1 fNorm_x = f8
119 mov rExpMask = 0x1ffff // Exponent mask
123 // Form signexp of 2^64 in case need to scale denormal
125 mov rExp_2to64 = 0x1003f
126 fclass.m p6,p9 = f8, 0x1e3 // Test x natval, nan, inf
127 (p8) br.cond.spnt ILOGB_DENORM // Branch if x unorm
132 // Return here from ILOGB_DENORM
134 and rExp_x = rSignexp_x, rExpMask // Get biased exponent
135 fclass.m p7,p10 = f8, 0x07 // Test x zero
140 movl rIntMax = 0x000000007fffffff // Form INT_MAX
144 .pred.rel "mutex",p6,p9
146 (p9) sub r8 = rExp_x, rExpBias // Get true exponent for normal path
147 (p6) fma.s0 fTmp = f8, f8, f0 // Dummy to set Invalid flag
148 (p6) mov r8 = rIntMax // If nan, inf, return INT_MAX
152 (p7) br.cond.spnt ILOGB_ZERO // Branch if x zero
153 (p10) br.ret.sptk b0 // Exit if x not zero
159 // Form 2^64 in case need to scale denormal
160 // Check to see if double-extended denormal
162 setf.exp f2to64 = rExp_2to64
163 fclass.m p8,p0 = fNorm_x, 0x0b
170 fcmp.eq.s0 p7,p0 = f8, f0 // Dummy op to set denormal flag
175 // If double-extended denormal add 64 to exponent bias for scaling
176 // If double-extended denormal form x * 2^64 which is normal
178 (p8) add rExpBias = 64, rExpBias
179 (p8) fmpy.s1 fNorm_x = fNorm_x, f2to64
184 // Logic is the same as normal path but use normalized input
186 getf.exp rSignexp_x = fNorm_x
188 br.cond.sptk ILOGB_COMMON // Return to main path
194 // Return INT_MIN, call error support
197 alloc r32=ar.pfs,1,3,4,0
198 movl rTrialResult = 0x0000000080000000
201 mov GR_Parameter_TAG = 157 // Error code
203 br.cond.sptk __libm_error_region // Call error support
207 GLOBAL_LIBM_END(ilogb)
210 LOCAL_LIBM_ENTRY(__libm_error_region)
214 add GR_Parameter_Y=-32,sp // Parameter 2 value
216 .save ar.pfs,GR_SAVE_PFS
217 mov GR_SAVE_PFS=ar.pfs // Save ar.pfs
221 add sp=-64,sp // Create new stack
223 mov GR_SAVE_GP=gp // Save gp
227 stfd [GR_Parameter_Y] = f0,16 // STORE Parameter 2 on stack
228 add GR_Parameter_X = 16,sp // Parameter 1 address
230 mov GR_SAVE_B0=b0 // Save b0
235 stfd [GR_Parameter_X] = f8 // STORE Parameter 1 on stack
236 add GR_Parameter_RESULT = 0,GR_Parameter_Y // Parameter 3 address
240 stfd [GR_Parameter_Y] = f9 // Store Parameter 3 on stack
241 add GR_Parameter_Y = -16,GR_Parameter_Y
242 br.call.sptk b0=__libm_error_support# // Call error handling function
246 add GR_Parameter_RESULT = 48,sp
252 mov r8 = rTrialResult
254 add sp = 64,sp // Restore stack pointer
255 mov b0 = GR_SAVE_B0 // Restore return address
259 mov gp = GR_SAVE_GP // Restore gp
260 mov ar.pfs = GR_SAVE_PFS // Restore ar.pfs
264 LOCAL_LIBM_END(__libm_error_region)
267 .type __libm_error_support#,@function
268 .global __libm_error_support#