[Libclc-dev] [PATCH] Add definition for M_PI_F v2

Tom Stellard tom at stellard.net
Tue Mar 18 15:59:44 PDT 2014


On Tue, Mar 11, 2014 at 10:21:17PM +0000, Jeroen Ketema wrote:
> 
> The form of type-punning Tom uses is perfectly legal in OpenCL 1.2: see section 6.2.4.1.
> 
> I don’t see point of using it though. Specifying a float directly with a sufficient number of digits seems a much clearer solution.
> 

Maybe I'm being overly cautious, but with the hexadecimal format,
I know exactly what value I'm getting, but with the floating point
format, I worry that different compilers / hardware might do slightly
different things.

-Tom

> Jeroen
> 
> On 11 Mar 2014, at 18:15, Jan Vesely <jan.vesely at rutgers.edu> wrote:
> 
> > On Tue, 2014-03-11 at 10:50 -0700, Matt Arsenault wrote:
> >> On 03/11/2014 08:38 AM, Tom Stellard wrote:
> >>> v2:
> >>>   - Use a hexadecimal constant.
> >>> ---
> >>>  generic/include/clc/float/definitions.h | 11 +++++++++++
> >>>  1 file changed, 11 insertions(+)
> >>> 
> >>> diff --git a/generic/include/clc/float/definitions.h b/generic/include/clc/float/definitions.h
> >>> index e6ef1d8..a6c947f 100644
> >>> --- a/generic/include/clc/float/definitions.h
> >>> +++ b/generic/include/clc/float/definitions.h
> >>> @@ -9,6 +9,17 @@
> >>>  #define FLT_MIN 	0x1.0p-126f
> >>>  #define FLT_EPSILON 	0x1.0p-23f
> >>> 
> >>> +_CLC_INLINE static float m_pi_f() {
> >>> +  union {
> >>> +    unsigned i;
> >>> +    float f;
> >>> +  } pi;
> >>> +  pi.i = 0x40490fdb;
> >>> +  return pi.f;
> >>> +}
> >>> +
> >>> +#define M_PI_F m_pi_f()
> >>> +
> >>>  #ifdef cl_khr_fp64
> >>> 
> >>>  #define DBL_DIG 	15
> >> I think this is technically undefined behavior that everyone violates. 
> > 
> > type-punning is afaik legal in c99 (implementation defined in c89),
> > "man gcc" includes an example same as this use-case
> > 
> > quick search gives this:
> > http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_283.htm
> > 
> > although I'd ask the same question as Jeroen,
> > 
> > what's wrong with 3.14159274101257f ?
> > 
> > regards,
> > 
> >> If you want the hex constant, you could use the format that comes out of 
> >> the %a format specifier:
> >> 
> >>     float cast = M_PI;
> >>     printf("M_PI  %a\n", M_PI);
> >>     printf("M_PI cast float: %a\n", cast);
> >> 
> >> Gives me:
> >> 
> >> M_PI  0x1.921fb54442d18p+1
> >> M_PI cast float: 0x1.921fb6p+1
> >> 
> >> 
> >> 
> >> 
> >> _______________________________________________
> >> Libclc-dev mailing list
> >> Libclc-dev at pcc.me.uk
> >> http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev
> > 
> > -- 
> > Jan Vesely <jan.vesely at rutgers.edu>
> > _______________________________________________
> > Libclc-dev mailing list
> > Libclc-dev at pcc.me.uk
> > http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev
> 
> 
> _______________________________________________
> Libclc-dev mailing list
> Libclc-dev at pcc.me.uk
> http://www.pcc.me.uk/cgi-bin/mailman/listinfo/libclc-dev




More information about the Libclc-dev mailing list