• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 #if !defined(cbigint_h)
19 #define cbigint_h
20 
21 #include <nativehelper/JNIHelp.h>
22 
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <stdint.h>
26 
27 /* IEEE floats consist of: sign bit, exponent field, significand field
28     single:  31 = sign bit, 30..23 = exponent (8 bits), 22..0 = significand (23 bits)
29     double:  63 = sign bit, 62..52 = exponent (11 bits), 51..0 = significand (52 bits)
30     inf                ==    (all exponent bits set) and (all mantissa bits clear)
31     nan                ==    (all exponent bits set) and (at least one mantissa bit set)
32     finite             ==    (at least one exponent bit clear)
33     zero               ==    (all exponent bits clear) and (all mantissa bits clear)
34     denormal           ==    (all exponent bits clear) and (at least one mantissa bit set)
35     positive           ==    sign bit clear
36     negative           ==    sign bit set
37 */
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 #define DOUBLE_LO_OFFSET        0
40 #define DOUBLE_HI_OFFSET        1
41 #define LONG_LO_OFFSET          0
42 #define LONG_HI_OFFSET          1
43 #else
44 #define DOUBLE_LO_OFFSET        1
45 #define DOUBLE_HI_OFFSET        0
46 #define LONG_LO_OFFSET          1
47 #define LONG_HI_OFFSET          0
48 #endif
49 
50 #define DOUBLE_EXPONENT_MASK_HI 0x7FF00000
51 #define DOUBLE_MANTISSA_MASK_HI 0x000FFFFF
52 
53 union U64U32DBL {
54     uint64_t    u64val;
55     uint32_t    u32val[2];
56     int32_t    i32val[2];
57     double  dval;
58 };
59 
60 #define DOUBLE_TO_LONGBITS(dbl) (*(reinterpret_cast<uint64_t*>(&dbl)))
61 #define FLOAT_TO_INTBITS(flt) (*(reinterpret_cast<uint32_t*>(&flt)))
62 #define INTBITS_TO_FLOAT(bits) (*(reinterpret_cast<float*>(&bits)))
63 
64 /* Replace P_FLOAT_HI and P_FLOAT_LOW */
65 /* These macros are used to access the high and low 32-bit parts of a double (64-bit) value. */
66 #define LOW_U32_FROM_DBL_PTR(dblptr) ((reinterpret_cast<U64U32DBL*>(dblptr))->u32val[DOUBLE_LO_OFFSET])
67 #define HIGH_U32_FROM_DBL_PTR(dblptr) ((reinterpret_cast<U64U32DBL*>(dblptr))->u32val[DOUBLE_HI_OFFSET])
68 #define LOW_I32_FROM_DBL_PTR(dblptr) ((reinterpret_cast<U64U32DBL*>(dblptr))->i32val[DOUBLE_LO_OFFSET])
69 #define HIGH_I32_FROM_DBL_PTR(dblptr) ((reinterpret_cast<U64U32DBL*>(dblptr))->i32val[DOUBLE_HI_OFFSET])
70 #define LOW_U32_FROM_DBL(dbl) LOW_U32_FROM_DBL_PTR(&(dbl))
71 #define HIGH_U32_FROM_DBL(dbl) HIGH_U32_FROM_DBL_PTR(&(dbl))
72 #define LOW_U32_FROM_LONG64_PTR(long64ptr) ((reinterpret_cast<U64U32DBL*>(long64ptr))->u32val[LONG_LO_OFFSET])
73 #define HIGH_U32_FROM_LONG64_PTR(long64ptr) ((reinterpret_cast<U64U32DBL*>(long64ptr))->u32val[LONG_HI_OFFSET])
74 #define LOW_I32_FROM_LONG64_PTR(long64ptr) ((reinterpret_cast<U64U32DBL*>(long64ptr))->i32val[LONG_LO_OFFSET])
75 #define HIGH_I32_FROM_LONG64_PTR(long64ptr) ((reinterpret_cast<U64U32DBL*>(long64ptr))->i32val[LONG_HI_OFFSET])
76 #define LOW_U32_FROM_LONG64(long64) LOW_U32_FROM_LONG64_PTR(&(long64))
77 #define HIGH_U32_FROM_LONG64(long64) HIGH_U32_FROM_LONG64_PTR(&(long64))
78 #define LOW_I32_FROM_LONG64(long64) LOW_I32_FROM_LONG64_PTR(&(long64))
79 #define HIGH_I32_FROM_LONG64(long64) HIGH_I32_FROM_LONG64_PTR(&(long64))
80 #define IS_DENORMAL_DBL_PTR(dblptr) (((HIGH_U32_FROM_DBL_PTR(dblptr) & DOUBLE_EXPONENT_MASK_HI) == 0) && ((HIGH_U32_FROM_DBL_PTR(dblptr) & DOUBLE_MANTISSA_MASK_HI) != 0 || (LOW_U32_FROM_DBL_PTR(dblptr) != 0)))
81 #define IS_DENORMAL_DBL(dbl) IS_DENORMAL_DBL_PTR(&(dbl))
82 
83 #define LOW_U32_FROM_VAR(u64)     LOW_U32_FROM_LONG64(u64)
84 #define LOW_U32_FROM_PTR(u64ptr)  LOW_U32_FROM_LONG64_PTR(u64ptr)
85 #define HIGH_U32_FROM_VAR(u64)    HIGH_U32_FROM_LONG64(u64)
86 #define HIGH_U32_FROM_PTR(u64ptr) HIGH_U32_FROM_LONG64_PTR(u64ptr)
87 
88 void multiplyHighPrecision(uint64_t* arg1, int32_t length1, uint64_t* arg2, int32_t length2,
89         uint64_t* result, int32_t length);
90 uint32_t simpleAppendDecimalDigitHighPrecision(uint64_t* arg1, int32_t length, uint64_t digit);
91 jdouble toDoubleHighPrecision(uint64_t* arg, int32_t length);
92 uint64_t doubleMantissa(jdouble z);
93 int32_t compareHighPrecision(uint64_t* arg1, int32_t length1, uint64_t* arg2, int32_t length2);
94 int32_t highestSetBitHighPrecision(uint64_t* arg, int32_t length);
95 void subtractHighPrecision(uint64_t* arg1, int32_t length1, uint64_t* arg2, int32_t length2);
96 int32_t doubleExponent(jdouble z);
97 int32_t addHighPrecision(uint64_t* arg1, int32_t length1, uint64_t* arg2, int32_t length2);
98 int32_t lowestSetBit(uint64_t* y);
99 int32_t timesTenToTheEHighPrecision(uint64_t* result, int32_t length, jint e);
100 int32_t highestSetBit(uint64_t* y);
101 int32_t lowestSetBitHighPrecision(uint64_t* arg, int32_t length);
102 void simpleShiftLeftHighPrecision(uint64_t* arg1, int32_t length, int32_t arg2);
103 uint32_t floatMantissa(jfloat z);
104 int32_t simpleAddHighPrecision(uint64_t* arg1, int32_t length, uint64_t arg2);
105 int32_t floatExponent(jfloat z);
106 
107 #endif                          /* cbigint_h */
108