This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathlong.ts
74 lines (68 loc) · 2.16 KB
/
long.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
module J2ME {
/**
* The closest floating-point representation to this long value.
*/
export function longToNumber(l: number, h: number): number {
return h * Constants.TWO_PWR_32_DBL + ((l >= 0) ? l : Constants.TWO_PWR_32_DBL + l);
}
export function returnLongValue(v: number) {
if (isNaN(v) || !isFinite(v)) {
tempReturn0 = 0;
return 0;
} else if (v <= -Constants.TWO_PWR_63_DBL) {
// min value
tempReturn0 = 0x80000000;
return 0;
} else if (v + 1 >= Constants.TWO_PWR_63_DBL) {
// max value
tempReturn0 = 0x7FFFFFFF;
return 0xFFFFFFFF;
} else if (v < 0) {
var lowBits = returnLongValue(-v);
var highBits = tempReturn0;
if (lowBits === 0 && highBits === 0x80000000) {
return lowBits;
}
// bitwise not
lowBits = (~lowBits) | 0;
highBits = (~highBits) | 0;
// add one
var a48 = highBits >>> 16;
var a32 = highBits & 0xFFFF;
var a16 = lowBits >>> 16;
var a00 = lowBits & 0xFFFF;
var b16 = 1 >>> 16;
var b00 = 1 & 0xFFFF;
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
c00 += a00 + b00;
c16 += c00 >>> 16;
c00 &= 0xFFFF;
c16 += a16 + b16;
c32 += c16 >>> 16;
c16 &= 0xFFFF;
c32 += a32;
c48 += c32 >>> 16;
c32 &= 0xFFFF;
c48 += a48;
c48 &= 0xFFFF;
tempReturn0 = (c48 << 16) | c32;
return (c16 << 16) | c00;
} else {
tempReturn0 = (v / Constants.TWO_PWR_32_DBL) | 0;
return (v % Constants.TWO_PWR_32_DBL) | 0;
}
}
}