64-bit integers

983 views
Skip to first unread message

Jeremy

unread,
Dec 13, 2010, 7:21:57 PM12/13/10
to v8-users
I noticed that Integer::Value() returns a 64-bit integer.
Furthermore, there is a distinct Int32 type which is presumably for 32-
bit integers. But there doesn't seem to be any way to actually put a
64-bit integer into an Integer - Integer::New() takes a 32-bit
integer.

So does V8 support 64-bit integers, or not? Is there some secret way
to get a 64-bit integer into an Integer type? I want to avoid the
precision loss that I would get from putting it into a Number in the
case where |value| > 2^53. Is that possible or should I just allow my
64-bit ints to be promoted to doubles and represent them as a Number?

Thanks,
Jeremy

Vyacheslav Egorov

unread,
Dec 14, 2010, 5:02:20 AM12/14/10
to v8-u...@googlegroups.com
Hi Jeremy,

There is no integer type in JavaScript.

There is Number type which is defined to be double precision
floating-point number.

ECMA-262 defined ToInteger(x) operation in section 9.2: roughly it is
just sign(x) * floor(abs(x)) plus several special cases for NaN, -0,
and infinities.

Integer type in V8 API should be treated as a convenient way of
representing and accessing result of this operation. It does not
extend underlying JavaScript type system, it just wraps value of
Number type.

--
Vyacheslav Egorov

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
>

Jeremy

unread,
Dec 14, 2010, 11:38:26 AM12/14/10
to v8-users
On Dec 14, 2:02 am, Vyacheslav Egorov <vego...@chromium.org> wrote:
> Hi Jeremy,
>
> There is no integer type in JavaScript.
>
> There is Number type which is defined to be double precision
> floating-point number.
>
> ECMA-262 defined ToInteger(x) operation in section 9.2: roughly it is
> just sign(x) * floor(abs(x)) plus several special cases for NaN, -0,
> and infinities.
>
> Integer type in V8 API should be treated as a convenient way of
> representing and accessing result of this operation. It does not
> extend underlying JavaScript type system, it just wraps value of
> Number type.
>

Thanks for your reply. Does v8 internally promote the int32 to a
double during Integer::New()? What about Int32? I tried to look this
up in the source but I can't make heads or tails of it (or maybe I'm
looking in the wrong place).

Jeremy

Vyacheslav Egorov

unread,
Dec 14, 2010, 11:53:35 AM12/14/10
to v8-u...@googlegroups.com
> Does v8 internally promote the int32 to a double during Integer::New()?

Sometimes :-)

Internally numbers in V8 can be represented in two forms:

- as so called SMall Integer or smi (31-bit on ia32 and ARM, 32-bit on
x64; passed as immediate value);
- HeapNumber (double allocated in the heap and passed by reference).

If value you pass into Integer::New fits into smi-representation V8
will store it as a smi otherwise it will allocate a HeapNumber.

> maybe I'm looking in the wrong place

v8::Integer::New is defined in api.cc

You can also check the comment in the beginning of objects.h for
details about object representation.

--
Vyacheslav Egorov

Reply all
Reply to author
Forward
0 new messages