473,587 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with NaN handling in IL conditionals

I would appreciate it if someone could help me understand NaN handling
with respect to conditionals in IL code. I am playing with a small IL
interpreter and having a little problem with it. Following is a small
piece of C# code that gets compiled by VS2005 to simulate my problem.
static void Main(string[] args)
{
Double db1 = 1.0;
Double db2 = Double.NaN;
if (db1 <= db2)
db2 = 1.0;
}

This is very simple code. The assignment after the conditional should
never happen because the conditional should evaluate as false because of
the NaN. Following is the IL code generated.
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThrea dAttribute::.ct or() =
( 01 00 00 00 )
// Code size 40 (0x28)
.maxstack 2
.locals init ([0] float64 db1,
[1] float64 db2,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.r8 1.
IL_000a: stloc.0
IL_000b: ldc.r8 (00 00 00 00 00 00 F8 FF)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: cgt.un
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0027
IL_001d: ldc.r8 1.
IL_0026: stloc.1
IL_0027: ret
} // end of method CABlock::Main
The IL code loads the two numbers onto the stack and does a cgt.un. This
should and does return a 0 (false) because of the NaN. The specification
is clear on this. The problem I am having is with the brtrue.s
instruction. The logic here seems to be reversed. If false is the result
of the cgt.un then the btrue will not branch which it should. This has
got me greatly confused. I have tried reading the CLR specification to
see what it does because it magically handles this properly. Somehow the
logic needs to get reversed but it is eluding me. The literal
interpretation of the above IL code only fails if there is a NaN
included. Without NaNs i have no problems at all.

Any help understanding the reversal of condition logic would be appreciated.

Leon Lambert
Jan 5 '06 #1
4 2014
Leon Lambert wrote:
I would appreciate it if someone could help me understand NaN handling
with respect to conditionals in IL code. I am playing with a small IL
interpreter and having a little problem with it. Following is a small
piece of C# code that gets compiled by VS2005 to simulate my problem.
<snip>
The IL code loads the two numbers onto the stack and does a cgt.un. This
should and does return a 0 (false) because of the NaN. The specification
is clear on this. The problem I am having is with the brtrue.s
instruction. The logic here seems to be reversed.


Yes. It's doing:

if (db1 > db2)
{
// Branch!
}
else
{
db=1.0;
}

Note that cgt.un is a "greater than" even though your C# code specifies
"less than or equal to". Basically, it's reversing the logic to get the
"less than or equal to" behaviour because there's only "less than" or
"greater than" in IL (I believe!).

Apologies if you understood all this already and the problem is
slightly different...

Jon

Jan 5 '06 #2
Hmmmm... your assumption that cgt.un return 0 is wrong.
It's actually 1.

cgt.un returns 1 if value1 is greater than value2 or if value1 is
'unordered'.
Now, value2 is the value at the top of the stack, so it's the value 1.0.
value1 is Nan that means unordered.
This results in a value 1 returned, right?

Willy.

"Leon Lambert" <la******@inil. com> wrote in message
news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
I would appreciate it if someone could help me understand NaN handling with
respect to conditionals in IL code. I am playing with a small IL
interpreter and having a little problem with it. Following is a small piece
of C# code that gets compiled by VS2005 to simulate my problem.
static void Main(string[] args)
{
Double db1 = 1.0;
Double db2 = Double.NaN;
if (db1 <= db2)
db2 = 1.0;
}

This is very simple code. The assignment after the conditional should
never happen because the conditional should evaluate as false because of
the NaN. Following is the IL code generated.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThrea dAttribute::.ct or() = (
01 00 00 00 )
// Code size 40 (0x28)
.maxstack 2
.locals init ([0] float64 db1,
[1] float64 db2,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.r8 1.
IL_000a: stloc.0
IL_000b: ldc.r8 (00 00 00 00 00 00 F8 FF)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: cgt.un
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0027
IL_001d: ldc.r8 1.
IL_0026: stloc.1
IL_0027: ret
} // end of method CABlock::Main
The IL code loads the two numbers onto the stack and does a cgt.un. This
should and does return a 0 (false) because of the NaN. The specification
is clear on this. The problem I am having is with the brtrue.s
instruction. The logic here seems to be reversed. If false is the result
of the cgt.un then the btrue will not branch which it should. This has got
me greatly confused. I have tried reading the CLR specification to see
what it does because it magically handles this properly. Somehow the logic
needs to get reversed but it is eluding me. The literal interpretation of
the above IL code only fails if there is a NaN included. Without NaNs i
have no problems at all.

Any help understanding the reversal of condition logic would be
appreciated.

Leon Lambert

Jan 5 '06 #3
The following is in the spec about cgt.un.

"Descriptio n:
The cgt instruction compares value1 and value2. If value1 is strictly
greater than value2, then 1 (of type int32) is pushed on the stack.
Otherwise 0 (of type int32) is pushed on the stack

For floating-point numbers, cgt returns 0 if the numbers are unordered
(that is, if one or both of the arguments are NaN).

As per IEC 60559:1989 spec, infinite values are ordered with respect to
normal numbers (e.g +infinity > 5.0 > -infinity)."

Doesn't this mean it returns 0 if either number is a Nan?

Leon Lambert

Willy Denoyette [MVP] wrote:
Hmmmm... your assumption that cgt.un return 0 is wrong.
It's actually 1.

cgt.un returns 1 if value1 is greater than value2 or if value1 is
'unordered'.
Now, value2 is the value at the top of the stack, so it's the value 1.0.
value1 is Nan that means unordered.
This results in a value 1 returned, right?

Willy.

"Leon Lambert" <la******@inil. com> wrote in message
news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
I would appreciate it if someone could help me understand NaN handling with
respect to conditionals in IL code. I am playing with a small IL
interpreter and having a little problem with it. Following is a small piece
of C# code that gets compiled by VS2005 to simulate my problem.
static void Main(string[] args)
{
Double db1 = 1.0;
Double db2 = Double.NaN;
if (db1 <= db2)
db2 = 1.0;
}

This is very simple code. The assignment after the conditional should
never happen because the conditional should evaluate as false because of
the NaN. Following is the IL code generated.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThrea dAttribute::.ct or() = (
01 00 00 00 )
// Code size 40 (0x28)
.maxstack 2
.locals init ([0] float64 db1,
[1] float64 db2,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.r8 1.
IL_000a: stloc.0
IL_000b: ldc.r8 (00 00 00 00 00 00 F8 FF)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: cgt.un
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0027
IL_001d: ldc.r8 1.
IL_0026: stloc.1
IL_0027: ret
} // end of method CABlock::Main
The IL code loads the two numbers onto the stack and does a cgt.un. This
should and does return a 0 (false) because of the NaN. The specification
is clear on this. The problem I am having is with the brtrue.s
instruction . The logic here seems to be reversed. If false is the result
of the cgt.un then the btrue will not branch which it should. This has got
me greatly confused. I have tried reading the CLR specification to see
what it does because it magically handles this properly. Somehow the logic
needs to get reversed but it is eluding me. The literal interpretation of
the above IL code only fails if there is a NaN included. Without NaNs i
have no problems at all.

Any help understanding the reversal of condition logic would be
appreciated .

Leon Lambert


Jan 6 '06 #4
Never mind i am an idiot. Been looking at spec for cgt instead of
cgt.un. Sometimes it pays to get some sleep Lol

Leon Lambert

Leon Lambert wrote:
The following is in the spec about cgt.un.

"Descriptio n:
The cgt instruction compares value1 and value2. If value1 is strictly
greater than value2, then 1 (of type int32) is pushed on the stack.
Otherwise 0 (of type int32) is pushed on the stack

For floating-point numbers, cgt returns 0 if the numbers are unordered
(that is, if one or both of the arguments are NaN).

As per IEC 60559:1989 spec, infinite values are ordered with respect to
normal numbers (e.g +infinity > 5.0 > -infinity)."

Doesn't this mean it returns 0 if either number is a Nan?

Leon Lambert

Willy Denoyette [MVP] wrote:
Hmmmm... your assumption that cgt.un return 0 is wrong.
It's actually 1.

cgt.un returns 1 if value1 is greater than value2 or if value1 is
'unordered'.
Now, value2 is the value at the top of the stack, so it's the value
1.0. value1 is Nan that means unordered.
This results in a value 1 returned, right?

Willy.

"Leon Lambert" <la******@inil. com> wrote in message
news:e0******** ********@TK2MSF TNGP10.phx.gbl. ..
I would appreciate it if someone could help me understand NaN
handling with respect to conditionals in IL code. I am playing with a
small IL interpreter and having a little problem with it. Following
is a small piece of C# code that gets compiled by VS2005 to simulate
my problem.
static void Main(string[] args)
{
Double db1 = 1.0;
Double db2 = Double.NaN;
if (db1 <= db2)
db2 = 1.0;
}

This is very simple code. The assignment after the conditional should
never happen because the conditional should evaluate as false because
of the NaN. Following is the IL code generated.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThrea dAttribute::.ct or() =
( 01 00 00 00 )
// Code size 40 (0x28)
.maxstack 2
.locals init ([0] float64 db1,
[1] float64 db2,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.r8 1.
IL_000a: stloc.0
IL_000b: ldc.r8 (00 00 00 00 00 00 F8 FF)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: cgt.un
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0027
IL_001d: ldc.r8 1.
IL_0026: stloc.1
IL_0027: ret
} // end of method CABlock::Main
The IL code loads the two numbers onto the stack and does a cgt.un.
This should and does return a 0 (false) because of the NaN. The
specification is clear on this. The problem I am having is with the
brtrue.s instruction. The logic here seems to be reversed. If false
is the result of the cgt.un then the btrue will not branch which it
should. This has got me greatly confused. I have tried reading the
CLR specification to see what it does because it magically handles
this properly. Somehow the logic needs to get reversed but it is
eluding me. The literal interpretation of the above IL code only
fails if there is a NaN included. Without NaNs i have no problems at
all.

Any help understanding the reversal of condition logic would be
appreciated.

Leon Lambert


Jan 6 '06 #5

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

Similar topics

1
2376
by: FHuang | last post by:
Ok, I'm having some trouble with conditionals, for some reason PHP is screwing them up. First off, here is the code: <?php $username = "fred"; $userdata = "./$username.txt"; $fp = fopen("$userdata", "r", 1); $origpass = fread($fp, 5); if($passcount = 1) {
15
2576
by: Joshua Ginsberg | last post by:
Is there any plan to include inline conditionals in Python? For example: def isNegative(x): return x < 0 ? True : False Thanks! -jag --
3
1434
by: steven | last post by:
Is it possible to combine conditionals to call out data? I have a list of records that include a date. I can call out all dates after the date after tomorrow: IIf(Date()+1)<,...,... And I can call dates before next week: IIf(< Date()+7,...,... But I'd like to isolate those that fall between the day after tomorrow and next week. I was...
13
1324
by: Michael B Allen | last post by:
This is a highly opinionated question but I'd like to know what people think in this matter. When initializing / creating a group of objects together I usually consolidate the error handling like the following: if (foo_init(&f) == -1 || (b = bar_new()) == NULL || some_function(a, b, c) == -1) { ERR(errno);
6
1945
by: Phillip N Rounds | last post by:
I have an application which is heavily graphics intensive, all the graphics being custom. Scattered throughout by app, I have MyView->OnDraw( this->GetDC() ); Apparently, each call to this->GetDC() creates a GDI object and, 16,000 or so calls to OnDraw() results in the Application hanging because it can no longer create any new GDI...
11
1788
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType = (Convert.ToString(drwData) == "True") ? "ID" : "PID"; XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ? "Highlight" : "Selection");
5
2153
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort, which doesn't work. The c code shows what was used to generate a.out, seen in the shell script. The value returned was varied from 0 to 1 for testing.
16
2757
by: Gowtham | last post by:
Hi, I had some C code written which initialized a global variable as: FILE *yyerfp = stdout; This used to work fine in older versions of gcc. Now, when I tried to compile this code (with gcc 3.2.3), I got errors like:
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.