-
-
Notifications
You must be signed in to change notification settings - Fork 11
WIP: QuadDtype finfo support #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
SwayamInSync
wants to merge
3
commits into
numpy:main
Choose a base branch
from
SwayamInSync:finfo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+375
−94
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#ifndef QUAD_CONSTANTS_HPP | ||
#define QUAD_CONSTANTS_HPP | ||
|
||
#include <sleef.h> | ||
#include <sleefquad.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
// Quad precision constants using sleef_q macro | ||
#define QUAD_PRECISION_ZERO sleef_q(+0x0000000000000LL, 0x0000000000000000ULL, -16383) | ||
#define QUAD_PRECISION_ONE sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 0) | ||
#define QUAD_PRECISION_INF sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 16384) | ||
#define QUAD_PRECISION_NINF sleef_q(-0x1000000000000LL, 0x0000000000000000ULL, 16384) | ||
#define QUAD_PRECISION_NAN sleef_q(+0x1ffffffffffffLL, 0xffffffffffffffffULL, 16384) | ||
|
||
// Additional constants | ||
#define QUAD_PRECISION_MAX_FINITE SLEEF_QUAD_MAX | ||
#define QUAD_PRECISION_MIN_FINITE Sleef_negq1(SLEEF_QUAD_MAX) | ||
#define QUAD_PRECISION_RADIX sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 1) // 2.0 | ||
|
||
#ifdef SLEEF_QUAD_C | ||
static const Sleef_quad SMALLEST_SUBNORMAL_VALUE = SLEEF_QUAD_DENORM_MIN; | ||
#else | ||
static const union { | ||
struct { | ||
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) | ||
uint64_t h, l; | ||
#else | ||
uint64_t l, h; | ||
#endif | ||
} parts; | ||
Sleef_quad value; | ||
} smallest_subnormal_const = {.parts = { | ||
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) | ||
.h = 0x0000000000000000ULL, .l = 0x0000000000000001ULL | ||
#else | ||
.l = 0x0000000000000001ULL, .h = 0x0000000000000000ULL | ||
#endif | ||
}}; | ||
#define SMALLEST_SUBNORMAL_VALUE (smallest_subnormal_const.value) | ||
#endif | ||
|
||
// Integer constants for finfo | ||
#define QUAD_NMANT 112 // mantissa bits (excluding implicit bit) | ||
#define QUAD_MIN_EXP -16382 // minimum exponent for normalized numbers | ||
#define QUAD_MAX_EXP 16384 // maximum exponent | ||
#define QUAD_DECIMAL_DIGITS 33 // decimal digits of precision | ||
|
||
typedef enum ConstantResultType { | ||
CONSTANT_QUAD, // Sleef_quad value | ||
CONSTANT_INT64, // int64_t value | ||
CONSTANT_ERROR // Error occurred | ||
} ConstantResultType; | ||
|
||
typedef struct ConstantResult { | ||
ConstantResultType type; | ||
union { | ||
Sleef_quad quad_value; | ||
int64_t int_value; | ||
} data; | ||
} ConstantResult; | ||
|
||
|
||
static inline ConstantResult get_sleef_constant_by_name(const char* constant_name) { | ||
ConstantResult result; | ||
|
||
if (strcmp(constant_name, "pi") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_PIq; | ||
} | ||
else if (strcmp(constant_name, "e") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_Eq; | ||
} | ||
else if (strcmp(constant_name, "log2e") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_LOG2Eq; | ||
} | ||
else if (strcmp(constant_name, "log10e") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_LOG10Eq; | ||
} | ||
else if (strcmp(constant_name, "ln2") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_LN2q; | ||
} | ||
else if (strcmp(constant_name, "ln10") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_M_LN10q; | ||
} | ||
else if (strcmp(constant_name, "max_value") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_QUAD_MAX; | ||
} | ||
else if (strcmp(constant_name, "epsilon") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_QUAD_EPSILON; | ||
} | ||
else if (strcmp(constant_name, "smallest_normal") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SLEEF_QUAD_MIN; | ||
} | ||
else if (strcmp(constant_name, "smallest_subnormal") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
result.data.quad_value = SMALLEST_SUBNORMAL_VALUE; | ||
} | ||
else if (strcmp(constant_name, "bits") == 0) { | ||
result.type = CONSTANT_INT64; | ||
result.data.int_value = sizeof(Sleef_quad) * 8; | ||
} | ||
else if (strcmp(constant_name, "precision") == 0) { | ||
result.type = CONSTANT_INT64; | ||
// precision = int(-log10(epsilon)) | ||
result.data.int_value = | ||
Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON))); | ||
} | ||
else if (strcmp(constant_name, "resolution") == 0) { | ||
result.type = CONSTANT_QUAD; | ||
// precision = int(-log10(epsilon)) | ||
int64_t precision = | ||
Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON))); | ||
// resolution = 10 ** (-precision) | ||
result.data.quad_value = | ||
Sleef_powq1_u10(Sleef_cast_from_int64q1(10), Sleef_cast_from_int64q1(-precision)); | ||
} | ||
else { | ||
result.type = CONSTANT_ERROR; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
#endif // QUAD_CONSTANTS_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you support unaligned, should use
memcpy
, unfortunately. (Or we change that we promise to not call it for unaligned stuff, which I can get on-board with.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually thinking about it, I am not sure if this is true or not :), we needed copyswap anyway due to byte-swapping. I am actually happy to say that the data must be aligned.