[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

ram

6/11/2014 8:21:00 PM

Newsgroups: comp.lang.c,comp.lang.javascript

I have read this in a World-Wide Web encyclopedia:

For example, given the following C code:

int f(int i) {
return i + 1;
}

Emscripten would output the following JS code:

function f(i) {
i = i|0;
return (i + 1)|0;
}

Do you think that the »|0« is necessary to express the
C semantics in JavaScript, or could the speed of the
generated code be improved by omitting it?

Newsgroups: comp.lang.c,comp.lang.javascript

67 Answers

Hertz Donut

1/9/2008 9:51:00 PM

0


"William Flax" <krtq73aa@prodigy.net> wrote in message
news:Uyahj.4867$pr6.4427@nlpi070.nbdc.sbc.com...
> See Below:
>
> "Hertz Donut" <somewhere@outthere.net> wrote in message
> news:T4idneNqvoPQrRjanZ2dnUVZ_qGknZ2d@hawaiiantel.net...
>>
>> "William Flax" <krtq73aa@prodigy.net> wrote in message
>> news:0U9hj.3702$El5.2816@newssvr22.news.prodigy.net...
>>
>> Are you going to whine and out all the way to election day?
>>
>> A recount? It might move Ron Paul to 9%.
>>
>> Get a life.
>>
>> Better yet, get a *CLUE*.
>>
>> Honu
>>
> You are a funny, fellow. If you add even the reported Paul votes in Iowa
> and New Hampshire, he out polled the combined Giuliani votes in Iowa and
> New
> Hampshire, and the combined Fred Thompson votes in Iowa and New Hampshire.
> This further points up the dirty tactics of Fox News, who had both
> Giuliani
> and Thompson at the debate from which Paul was excluded, last Sunday.
>
> Now, on balance, I think Fred Thompson is a decent man--as indeed is
> Giuliani, personally. Yet neither has shown the in depth understanding of
> economics, or the Constitution, that Ron Paul has. Thompson is fairly
> Conservative, Giuliani fairly Liberal. But the point is that Fox has
> worked
> to undermine Paul, while treating the others with respect.
>
>

8% is 8%!

You, being the good little shill you are, will endlessly "compare and
contrast" his non-performance against his more worthy opponents, and blame
"Fox News" and other things for the simple fact that he failed to inspire
anyone to vote for him.

Honu



PseuDoeCyAnts

1/10/2008 12:59:00 AM

0

on Wed 09 Jan 2008 01:10:43p
"William Flax" <krtq73aa@prodigy.net> posted
in news:Uyahj.4867$pr6.4427@nlpi070.nbdc.sbc.com:

> See Below:
>
> "Hertz Donut" <somewhere@outthere.net> wrote in message
> news:T4idneNqvoPQrRjanZ2dnUVZ_qGknZ2d@hawaiiantel.net...
>>
>> "William Flax" <krtq73aa@prodigy.net> wrote in message
>> news:0U9hj.3702$El5.2816@newssvr22.news.prodigy.net...
>>
>> Are you going to whine and out all the way to election day?
>>
>> A recount? It might move Ron Paul to 9%.
>>
>> Get a life.
>>
>> Better yet, get a *CLUE*.
>>
>> Honu
>>
> You are a funny, fellow. If you add even the reported Paul
> votes in Iowa and New Hampshire, he out polled the combined
> Giuliani votes in Iowa and New Hampshire, and the combined Fred
> Thompson votes in Iowa and New Hampshire. This further points up
> the dirty tactics of Fox News, who had both Giuliani and
> Thompson at the debate from which Paul was excluded, last
> Sunday.
>
> Now, on balance, I think Fred Thompson is a decent man--as
> indeed is Giuliani, personally. Yet neither has shown the in
> depth understanding of economics, or the Constitution, that Ron
> Paul has. Thompson is fairly Conservative, Giuliani fairly
> Liberal. But the point is that Fox has worked to undermine
> Paul, while treating the others with respect.
>
Oh my, oh my...

Here's your big clue for the day:

New Hampsire Republican Delegates:
22 Pledged
8 Unpledged

If the current GOP sanctions hold though,
only half of these delegates will be seated
at the RNC 2008 Convention.

Currently the actual total for New Hampshire is

11 Pledged
4 Unpledged

Iowa Republican Delegates:
45 Pledged
12 Unpledged

Presently, under sanctions the total delegates
from New Hampshire and Iowa combined is 72.

1,191 delegate votes are needed to win.

In the states in play on Super Tuesday, February 5, 2008,
there are 1,051 total delegates pledged and unpledged.

New York's total on February 5, 2008,
is 101 delegates, all pledged.

Currently,
Ron Paul has 4 delegates, and Rudy Giuliani has 1.
Care to wager who will have more delegates
after Super Tuesday?

ROFLMAO

Oh yeah, and Thompson, a decent man?
You're talking about a guy who abused
his former Tenn. Senate seat to gain access as a paid lobbyist
to the office of the Senate Majority leader, Bill Frist (R-TN).

What a guy...

stuff_stuff

1/10/2008 1:33:00 AM

0

On Wed, 9 Jan 2008 16:10:43 -0500, "William Flax"
<krtq73aa@prodigy.net> wrote:

>You are a funny, fellow. If you add even the reported Paul votes in Iowa
>and New Hampshire, he out polled the combined Giuliani votes in Iowa and New
>Hampshire,


LOL. Because Guliania didn't actively run in either. LOL.

Evertjan.

6/11/2014 8:49:00 PM

0

ram@zedat.fu-berlin.de (Stefan Ram) wrote on 11 jun 2014 in
comp.lang.javascript:

> Newsgroups: comp.lang.c,comp.lang.javascript

Response for comp.lang.javascript

> I have read this in a World-Wide Web encyclopedia:
>
> For example, given the following C code:
>
> int f(int i) {
> return i + 1;
>}

I have no idea what that means.

> Emscripten

What is that?

> would output the following JS code:
>
> function f(i) {
> i = i|0;
> return (i + 1)|0;
>}

> Do you think that the »|0« is necessary to express the
> C semantics in JavaScript,

What is "C semantics"?
Why would you want to do that and not just write javascript?
What other types of i would you want to catch?
btw, i could be a string, a floating point, would you allow that?


var i = 'Hello';
alert(i|0); // 0
alert(i||0); // Hello

alert('yes'|'maybe'); // 0
alert('yes'||'maybe'); // yes
alert(''||'maybe'); // maybe


> or could the speed of the
> generated code be improved by omitting it?

var i = 'Hello';
alert(f(i));

function f(i) {
return i++;
};

This will return NaN,
[which is NOT an error!]
and then you deal with that.

However, better be sure of your code
and the effective subtypes
of the variable values you are using.

Javascript has no typed variables.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

ram

6/11/2014 9:01:00 PM

0

Newsgroups: comp.lang.c,comp.lang.javascript

Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>type of i. i|0 probably forces it to an integer, and the

Yes, but if it was compiled from a correct C program, then
every call within that program should already have an
integer as its argument - and authors of other clients in
JavaScript can be instructed to use only integer arguments.

When they then call the function with non-integer arguments
its their fault. (Or they can write a |0 wrapper instead
of requiring each and every function to do |0 and thereby
possibly slowing down execution and enlarging code size.)

Newsgroups: comp.lang.c,comp.lang.javascript

Ike Naar

6/11/2014 9:09:00 PM

0

On 2014-06-11, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> When they then call the function with non-integer arguments
> its their fault. (Or they can write a |0 wrapper instead
> of requiring each and every function to do |0 and thereby
> possibly slowing down execution and enlarging code size.)

With a decent optimizing compiler, do you think that |0 would
slow down execution or enlarge code size?

James Kuyper

6/11/2014 9:38:00 PM

0

On 06/11/2014 05:09 PM, Ike Naar wrote:
> On 2014-06-11, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>> When they then call the function with non-integer arguments
>> its their fault. (Or they can write a |0 wrapper instead
>> of requiring each and every function to do |0 and thereby
>> possibly slowing down execution and enlarging code size.)
>
> With a decent optimizing compiler, do you think that |0 would
> slow down execution or enlarge code size?

I'm no JS experts, but so far I haven't seen any responses from those
who are, so I'll throw in my guesses.

It seems reasonable to me that it would - in javascript, |0 doesn't just
cause the integer value to be unchanged (something a smart compiler
could drop) - it also (and first) causes conversion to a 32-bit integer
type, if necessary. The compiler can't be expected to know that such a
conversion will not, in fact, be necessary. This is a substantial
difference from the original C code, where such conversions would occur,
if necessary, in the calling code, where the compiler can be sure. A JS
compiler would have to generate code to check the type of the argument,
and code to perform the conversion. The checking part, at least, will
have to be executed even though the conversion code will not.

Christoph M. Becker

6/11/2014 10:00:00 PM

0

Stefan Ram wrote:

> I have read this in a World-Wide Web encyclopedia:
>
> For example, given the following C code:
>
> int f(int i) {
> return i + 1;
> }
>
> Emscripten would output the following JS code:
>
> function f(i) {
> i = i|0;
> return (i + 1)|0;
> }
>
> Do you think that the »|0« is necessary to express the
> C semantics in JavaScript, or could the speed of the
> generated code be improved by omitting it?

At least the C semantics are better matched; consider arbitrary input to
the function, or an overflow occuring in the calculation.

I'm not sure about the performance. Actually, this idiom is quite
common, so it might cause performance improvements in some ECMAScript
implementations. At least it is used in asm.js to enforce the type of i
and the return value to be an integer type.

[xpost & fup2 comp.lang.javascript]

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

6/11/2014 10:21:00 PM

0

[F'up2 comp.lang.javascript]

Stefan Ram wrote:

> I have read this in a World-Wide Web encyclopedia:
>
> For example, given the following C code:
>
> int f(int i) {
> return i + 1;
> }
>
> Emscripten would output the following JS code:
>
> function f(i) {
> i = i|0;
> return (i + 1)|0;
> }
>
> Do you think that the »|0« is necessary to express the
> C semantics in JavaScript, or could the speed of the
> generated code be improved by omitting it?

First of all, this does _not_ express the C semantics in JavaScript, and
there is no other way. â??intâ? is a *generic* type in C/C++; IIUC, the result
could be a 32-bit integer when compiled for a 32-bit platform or a 64-bit
integer when compiled for a 64-bit platform.

<http://en.wikibooks.org/wiki/C_Programming/Reference_Tables#Table_of_Data...
<http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4...

By contrast, using the binary bitwise OR operator, as with all ECMAScript
binary bitwise operators, *always* creates an IEEE-754 double-precision
*floating-point* value representing a *32-bit* integer value. Also, not
only the result will be such a value, but also the operands are converted
to such values internally, before the operation is performed.

<http://ecma-international.org/ecma-262/5.1/#sec...


Conversion to an integer value of the ECMAScript Number type (i.e., where
the fractional part of the mantissa is zero), which appears to be the goal
here, can be better achieved with the Math.floor() and Math.ceil()
functions, e.g.:

if (typeof Number.prototype.toInteger != "undefined")
{
Number.prototype.toInteger = function () {
return (this < 0 ? Math.ceil(this) : Math.floor(this));
};
}

/**
* Frobnicates this value
*
* @return {int} i
* The value to be frobnicated
* @return {int}
* The frobnicated value
*/
function f (i)
{
return (+i).toInteger() + 42;
}

[JSX:array.js features another converter, for
jsx.array.BigArray.prototype.slice() & friends¹, that for practical reasons
more closely matches the Specification (ToInt32), but does not convert to
32-bit floating-point integer.]

Of course, this still does not remotely implement the C semantics. One
aspect of it is that code where you pass a non-integer would not compile.
Since ECMAScript uses dynamic type-checking, it is not possible to prevent
compilation. But at the very least passing unsuitable values should cause
an exception to be thrown, so that it becomes unnecessary to handle them,
e.g.:

function f (i)
{
if (i % 1 != 0)
{
/* JSX:object.js provides jsx.InvalidArgumentError instead */
throw new TypeError('f: Invalid argument for "i": ' + i + ':'
+ typeof i + '[' + _getClass(i) + ']'
+ (i != null ? ' by ' + (_getFunctionName(i.constructor) || '?')
: '')
+ '; expected integer');
}

return i + 42;
}

_________
¹ supports arrays with up to 2⁵³â??1 numerically indexed elements²
² Because 2⁵³+1 is indistinguishable from 2⁵³ due to precision limits,
so that element overflow could not be detected, I had to reduce
jsx.array.BigArray.MAX_LENGTH to Math.pow(2, 53) - 1 recently.
(This is also the reason why standard Array instances can hold only
up to 2³²_â??1_ elements, so that the largest possible index is
Math.pow(2, 32) _- 2_.)
--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Andreas Bergmaier

6/11/2014 10:47:00 PM

0

Stefan Ram schrieb:

> Emscripten would output the following JS code:
>
> function f(i) {
> i = i|0;
> return (i + 1)|0;
> }
>
> Do you think that the »|0« is necessary to express the
> C semantics in JavaScript, or could the speed of the
> generated code be improved by omitting it?

Emscripten does output asm.js[1], and the |0 is necessary to be valid
asm.js code. It does annotate the type of the expression - the result
will always be an integer. This does allow certain *optimisations* in
the compiled code, omitting it would actually make it slower.
I would't expect huge contributions to code size, assuming the script is
properly compressed.

Regards,
Bergi

[1]: http://...