| 1 |
/** |
| 2 |
* 80-bit floating point value implementation if the C/D compiler does not support them natively. |
| 3 |
* Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 4 |
* |
| 5 |
* GCC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3, or (at your option) |
| 8 |
* any later version. |
| 9 |
* |
| 10 |
* GCC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with GCC; see the file COPYING3. If not see |
| 17 |
* <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
module dmd.root.longdouble; |
| 21 |
|
| 22 |
import core.stdc.config; |
| 23 |
import core.stdc.stdint; |
| 24 |
|
| 25 |
extern(C++): |
| 26 |
nothrow: |
| 27 |
@nogc: |
| 28 |
pure: |
| 29 |
@trusted: |
| 30 |
|
| 31 |
// Type used by the front-end for compile-time reals |
| 32 |
struct longdouble |
| 33 |
{ |
| 34 |
nothrow: |
| 35 |
@nogc: |
| 36 |
pure: |
| 37 |
extern (D) this(T)(T r) |
| 38 |
{ |
| 39 |
this.set(cast(SetType!T)r); |
| 40 |
} |
| 41 |
|
| 42 |
// No constructor to be able to use this class in a union. |
| 43 |
extern (D) longdouble opAssign(T)(T r) |
| 44 |
if (is (T : longdouble)) |
| 45 |
{ |
| 46 |
this.realvalue = r.realvalue; |
| 47 |
return this; |
| 48 |
} |
| 49 |
|
| 50 |
extern (D) longdouble opAssign(T)(T r) |
| 51 |
if (!is (T : longdouble)) |
| 52 |
{ |
| 53 |
this.set(cast(SetType!T)r); |
| 54 |
return this; |
| 55 |
} |
| 56 |
|
| 57 |
// Arithmetic operators. |
| 58 |
extern (D) longdouble opBinary(string op, T)(T r) const |
| 59 |
if ((op == "+" || op == "-" || op == "*" || op == "/" || op == "%") |
| 60 |
&& is (T : longdouble)) |
| 61 |
{ |
| 62 |
static if (op == "+") |
| 63 |
return this.add(r); |
| 64 |
else static if (op == "-") |
| 65 |
return this.sub(r); |
| 66 |
else static if (op == "*") |
| 67 |
return this.mul(r); |
| 68 |
else static if (op == "/") |
| 69 |
return this.div(r); |
| 70 |
else static if (op == "%") |
| 71 |
return this.mod(r); |
| 72 |
} |
| 73 |
|
| 74 |
extern (D) longdouble opUnary(string op)() const |
| 75 |
if (op == "-") |
| 76 |
{ |
| 77 |
return this.neg(); |
| 78 |
} |
| 79 |
|
| 80 |
extern (D) int opCmp(longdouble r) const |
| 81 |
{ |
| 82 |
return this.cmp(r); |
| 83 |
} |
| 84 |
|
| 85 |
extern (D) int opEquals(longdouble r) const |
| 86 |
{ |
| 87 |
return this.equals(r); |
| 88 |
} |
| 89 |
|
| 90 |
extern (D) bool opCast(T : bool)() const |
| 91 |
{ |
| 92 |
return this.to_bool(); |
| 93 |
} |
| 94 |
|
| 95 |
extern (D) T opCast(T)() const |
| 96 |
{ |
| 97 |
static if (__traits(isUnsigned, T)) |
| 98 |
return cast (T) this.to_uint(); |
| 99 |
else |
| 100 |
return cast(T) this.to_int(); |
| 101 |
} |
| 102 |
|
| 103 |
void set(int8_t d); |
| 104 |
void set(int16_t d); |
| 105 |
void set(int32_t d); |
| 106 |
void set(int64_t d); |
| 107 |
void set(uint8_t d); |
| 108 |
void set(uint16_t d); |
| 109 |
void set(uint32_t d); |
| 110 |
void set(uint64_t d); |
| 111 |
void set(bool d); |
| 112 |
|
| 113 |
int64_t to_int() const; |
| 114 |
uint64_t to_uint() const; |
| 115 |
bool to_bool() const; |
| 116 |
|
| 117 |
longdouble add(const ref longdouble r) const; |
| 118 |
longdouble sub(const ref longdouble r) const; |
| 119 |
longdouble mul(const ref longdouble r) const; |
| 120 |
longdouble div(const ref longdouble r) const; |
| 121 |
longdouble mod(const ref longdouble r) const; |
| 122 |
longdouble neg() const; |
| 123 |
int cmp(const ref longdouble t) const; |
| 124 |
int equals(const ref longdouble t) const; |
| 125 |
|
| 126 |
private: |
| 127 |
// Statically allocate enough space for REAL_VALUE_TYPE. |
| 128 |
enum REALVALUE_SIZE = (2 + (16 + c_long.sizeof) / c_long.sizeof); |
| 129 |
c_long [REALVALUE_SIZE] realvalue; |
| 130 |
} |
| 131 |
|
| 132 |
// Pick the corresponding (u)int64_t type for T, as int64_t may be |
| 133 |
// a special enum that requires casting to explicitly. |
| 134 |
private template SetType(T) |
| 135 |
{ |
| 136 |
static if (__traits(isIntegral, T) && T.sizeof == 8) |
| 137 |
{ |
| 138 |
static if (__traits(isUnsigned, T)) |
| 139 |
alias SetType = uint64_t; |
| 140 |
else |
| 141 |
alias SetType = int64_t; |
| 142 |
} |
| 143 |
else |
| 144 |
alias SetType = T; |
| 145 |
} |