473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

casting question

Part of the OpenSolaris source contains the following macro[1]:

41 /*
42 * FRC2PCT macro is used to convert 16-bit binary fractions in the range
43 * 0.0 to 1.0 with binary point to the right of the high order bit
44 * (i.e. 1.0 == 0x8000) to percentage value.
45 */
46
47 #define FRC2PCT(pp) (((float)(pp))/0x8000*100)
48

which gets called with 16-bit values of `pp', i.e. from members of a
structure, as in the prstat.c[2] snipper shown below:

482 static void
483 list_update(list_t *list, lwp_info_t *lwp)
484 {
...
513 id->id_pctcpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);

But I recently discovered, by having a local program (which re-uses some
of the prstat Solaris source) crash when it reached that line from
list_update() that it's not always a good idea to blindly cast any
integer value to a float. Changing that macro to an inline function
that uses a `float' intermediate variable and re-arranging the
calculation a bit seems to have fixed the crash here:

static inline float
FRC2PCT(uint16_t pp)
{
float fval;

fval = (100.0 * pp) / 0x800;
return fval;
}

But I'm not sure why this breaks when the cast is used. Is casting an
integer value and then accessing it as a float something that causes UB
or even implementation-defined behavior?

- Giorgos

References:
***********

[1] http://cvs.opensolaris.org/source/xr...rstat/prstat.h
[2] http://cvs.opensolaris.org/source/xr...rstat/prstat.c

May 8 '06 #1
3 2051

"Giorgos Keramidas" <ke******@ceid.upatras.gr> wrote in message
news:86************@gothmog.pc...
Part of the OpenSolaris source contains the following macro[1]:

41 /*
42 * FRC2PCT macro is used to convert 16-bit binary fractions in the range 43 * 0.0 to 1.0 with binary point to the right of the high order bit
44 * (i.e. 1.0 == 0x8000) to percentage value.
45 */
46
47 #define FRC2PCT(pp) (((float)(pp))/0x8000*100)
48

which gets called with 16-bit values of `pp', i.e. from members of a
structure, as in the prstat.c[2] snipper shown below:

482 static void
483 list_update(list_t *list, lwp_info_t *lwp)
484 {
...
513 id->id_pctcpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
But I recently discovered, by having a local program (which re-uses some
of the prstat Solaris source) crash when it reached that line from
list_update() that it's not always a good idea to blindly cast any
integer value to a float. Changing that macro to an inline function
that uses a `float' intermediate variable and re-arranging the
calculation a bit seems to have fixed the crash here:

static inline float
FRC2PCT(uint16_t pp)
{
float fval;

fval = (100.0 * pp) / 0x800;
/* 0x800? Shouldn't that be 0x8000? You lost a zero. */

fval = (100.0 * pp) / 0x8000;

/* You may experience overflow errors if you multiply */
/* a uint16_t by 100 before dividing by 0x8000. */
/* There were two reason for that original cast: 1) was */
/* to increase the range available for the calculation */
/* and 2) was to properly calculate the "binary point." */
/* You haven't done _either_ in your calculation. pp _must_ */
/* be cast to a type with a larger range before the multiplication */
/* by 100 for the calculation to work properly. The calculation */
/* itself _must_ be cast to a float sometime prior to the division for */
/* the binary point to be calculated properly. */

fval = ((float)(100.0 * (uint32_t)pp)) / 0x8000;
return fval;
}

But I'm not sure why this breaks when the cast is used. Is casting an
integer value and then accessing it as a float something that causes UB
or even implementation-defined behavior?

References:
***********

[1] http://cvs.opensolaris.org/source/xr...rstat/prstat.h
[2] http://cvs.opensolaris.org/source/xr...rstat/prstat.c

Sorry, I don't know (or care) why it breaks...
Rod Pemberton
May 8 '06 #2
Giorgos Keramidas wrote:
#define FRC2PCT(pp) (((float)(pp))/0x8000*100)

id->id_pctcpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);

But I recently discovered, by having a local program (which re-uses some
of the prstat Solaris source) crash when it reached that line from
list_update() that it's not always a good idea to blindly cast any
integer value to a float.
Actually there is nothing wrong with that. If the integer cannot be
exactly represented in a float, then a neighbouring float value
will be used. Unless the integer is bigger than FLT_MAX in
which case you will get undefined behaviour. But this is
unlikely to be the case when using 16-bit ints.
Changing that macro to an inline function
that uses a `float' intermediate variable and re-arranging the
calculation a bit seems to have fixed the crash here:

static inline float
FRC2PCT(uint16_t pp)
{
float fval;

fval = (100.0 * pp) / 0x800;
return fval;
}


If you had written:

fval = ((float)pp) / 0x8000 * 100;

then this code would be the same as the macro. If one causes a
crash and the other does not, then you have a bug elsewhere in
your code (likely a memory allocation error or a buffer overflow)
that just happens to manifest itself in this way.

If your code as written works and the code I just suggested does
not, that would be bizarre. You could then try:

fval = ((double)pp) / 0x8000 * 100;

because floats are promoted to doubles in arithmetic expressions
anyway.

May 8 '06 #3
On Mon, 8 May 2006 14:18:03 -0400, "Rod Pemberton" <do*********@bitfoad.cmm> wrote:
"Giorgos Keramidas" <ke******@ceid.upatras.gr> wrote in message
news:86************@gothmog.pc...
Part of the OpenSolaris source contains the following macro[1]:

41 /*
42 * FRC2PCT macro is used to convert 16-bit binary fractions in the range
43 * 0.0 to 1.0 with binary point to the right of the high order bit
44 * (i.e. 1.0 == 0x8000) to percentage value.
45 */
46
47 #define FRC2PCT(pp) (((float)(pp))/0x8000*100)
48

which gets called with 16-bit values of `pp', i.e. from members of a
structure, as in the prstat.c[2] snipper shown below:

482 static void
483 list_update(list_t *list, lwp_info_t *lwp)
484 {
...
513 id->id_pctcpu += FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);

But I recently discovered, by having a local program (which re-uses some
of the prstat Solaris source) crash when it reached that line from
list_update() that it's not always a good idea to blindly cast any
integer value to a float. Changing that macro to an inline function
that uses a `float' intermediate variable and re-arranging the
calculation a bit seems to have fixed the crash here:

static inline float
FRC2PCT(uint16_t pp)
{
float fval;

fval = (100.0 * pp) / 0x800;
/* 0x800? Shouldn't that be 0x8000? You lost a zero. */


Yes, sorry about that. Copy/paste error :(
fval = (100.0 * pp) / 0x8000;

/* You may experience overflow errors if you multiply */
/* a uint16_t by 100 before dividing by 0x8000. */
/* There were two reason for that original cast: 1) was */
/* to increase the range available for the calculation */
/* and 2) was to properly calculate the "binary point." */
/* You haven't done _either_ in your calculation. pp _must_ */
/* be cast to a type with a larger range before the multiplication */
/* by 100 for the calculation to work properly. The calculation */
/* itself _must_ be cast to a float sometime prior to the division for */
/* the binary point to be calculated properly. */

fval = ((float)(100.0 * (uint32_t)pp)) / 0x8000;
All noted.
Sorry, I don't know (or care) why it breaks...


No problem. Thanks for all the help :)

May 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.