Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > IF-free conception.

Reply
Thread Tools

IF-free conception.

 
 
James Kuyper
Guest
Posts: n/a
 
      02-16-2013
On 02/15/2013 08:23 PM, Evgeney Knyazhev wrote:
> -----------------------
> You are mistaken. `i2' is the result of an `<' operator,
> hence either zero or one. `!0' is 1, `!1' is zero. `!i2' is
> well-defined.
> ----------------------------------
> well, let's assume
>
> int i=0;
> i=!i;
> // then output: 0xffff


"The result of the logical negation operator ! is 0 if the value of its
operand compares unequal to 0, 1 if the value of its operand compares
equal to 0. The result has type int. The expression !E is equivalent to
(0==E)." (6.5.3.3p5)

Therefore, 0xFFFF is not a legal value for a logical negation
expression. I think that what you're thinking of is the bitwise
complement operator, "~"

i = ~i;

What value 'i' will have after that expression is executed depends upon
whether it's 2's complement, 1's complement, or sign-magnitude, but
regardless of which case applies, it's guaranteed to be negative, which
is not the case for 0xFFFF. Therefore, I assume you're also thinking as
if 'i' had the type 'unsigned int', and UINT_MAX == 0xFFFF.

--
James Kuyper
 
Reply With Quote
 
 
 
 
Evgeney Knyazhev
Guest
Posts: n/a
 
      02-16-2013
Thanks, James, ye're correct. my mind shaded to confuse "!" & "~" operations
 
Reply With Quote
 
 
 
 
Evgeney Knyazhev
Guest
Posts: n/a
 
      02-16-2013
anyway, "i1=!i2" ain't suited because we need inversion ;D
i1=0; // then i2==1
i1=1; // then i2==0
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-16-2013
Evgeney Knyazhev <(E-Mail Removed)> writes:
> -----------------------
> You are mistaken. `i2' is the result of an `<' operator,
> hence either zero or one. `!0' is 1, `!1' is zero. `!i2' is
> well-defined.
> ----------------------------------
> well, let's assume
>
> int i=0;
> i=!i;
> // then output: 0xffff


"!" is the logical "not" operator; it yields 1 if its operand is 0, 0
otherwise.

"~" is the bitwise "not" operator. `~0` is 0xffff only if int is 16
bits.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-16-2013
Evgeney Knyazhev <(E-Mail Removed)> writes:
> Eric, you can download the source & app as well, so it will "heal"
> your questions.


I downloaded your source via the URL buried at the bottom of the
site you mentioned in your first post in this thread. I got a file
called "IF free sorting.7z". Once I figured out how to unpack it,
it included a source file called "IF-free sorting.cpp". (Yes, both file
names had spaces in them.)

The code is (a) Windows-specific, and (b) writtin in C++, not C.

If you want to discuss the ideas represented in the code in the
context of C, preferably portable C, feel free to do so. Otherwise,
I'm suggest this is not the place.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Evgeney Knyazhev
Guest
Posts: n/a
 
      02-16-2013
----------------
Also * can be a more expensive operation than &.
-----------------
Howard, perhaps you're right, but we multiply only "1" & "0", so it could be negligible.
-------------------------
Whether any of these tricks help really depends on the machine and compiler. A
delayed branch or speculative branch hardware with a compiler that can pack
delay slots can implement branches as fast or faster than the non-branch code
and keep the source code readable.
--------------------------
branches are very reason to idle processor for data receiving.
 
Reply With Quote
 
Evgeney Knyazhev
Guest
Posts: n/a
 
      02-16-2013
Keith, yes, i've done it on windows-based ground. but i don't think people would have insuperable problems to fit solution to another grounds.

P.S.

Thanks for reply.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-16-2013
Evgeney Knyazhev <(E-Mail Removed)> writes:

> Eric, thanks for reply. mostly, code, posted in the blog, is schematic
> ;D full code has been in source.


The code is C++ (there's another group for that) and non-standard C++ so
it won't compiler here without much editing. However I did take your
if-avoiding factorial function and timed it compared to

(a) one that was the same but written without all the extra bother of a
pointer argument, automatic arrays, and the subtract one/add one dance
you do; and

(b) one written with a simple conditional; and

(c) one that is written as a plain condition + iterative loop.

My (a) was a little faster than yours (1.3 vs 1.9). My (b) was faster
still (1.1 vs 1.9) and (c) was the fastest of the lot (0.7 vs 1.9).
These are with no optimisation. with more optimisation turned on, the
differences become more dramatic. I.e. the compiler can optimise the
simpler code more easily than it can your complex code.

These are crude tests, with one compiler, on one architecture so they
may not be representative but do you have any data to support writing
these messy alternatives?

--
Ben.
 
Reply With Quote
 
Evgeney Knyazhev
Guest
Posts: n/a
 
      02-16-2013
Ben, factorial was written Just for fun Actually, recursive functions are the Evil. About performance, Just compare two codes:

1st. if(a<b)a++;
2nd. a+=(a<b);
---
Thanks for reply.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-16-2013
On 02/15/2013 09:46 PM, Evgeney Knyazhev wrote:
> Keith, yes, i've done it on windows-based ground. but i don't think
> people would have insuperable problems to fit solution to another
> grounds.


I haven't bothered to go through the trouble that Keith did to locate
your code - but if he felt that the code was sufficiently
Windows-specific to justify commenting on the fact, I suspect it would
require significant conversion to work in other environments.

The other point, which you seem to have ignored, is that the code is
C++. As such, it should be discussed in a C++ forum, not a C forum.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments