| 1 |
#ifndef RBIMPL_ARITHMERIC_ST_DATA_T_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_ARITHMERIC_ST_DATA_T_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Arithmetic conversion between C's `st_data_t` and Ruby's. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/arithmetic/fixnum.h" |
| 24 |
#include "ruby/internal/arithmetic/long.h" |
| 25 |
#include "ruby/internal/attr/artificial.h" |
| 26 |
#include "ruby/internal/attr/const.h" |
| 27 |
#include "ruby/internal/attr/constexpr.h" |
| 28 |
#include "ruby/internal/cast.h" |
| 29 |
#include "ruby/internal/value.h" |
| 30 |
#include "ruby/assert.h" |
| 31 |
#include "ruby/st.h" |
| 32 |
|
| 33 |
#define ST2FIX RB_ST2FIX /**< @old{RB_ST2FIX} */ |
| 34 |
/** @cond INTERNAL_MACRO */ |
| 35 |
#define RB_ST2FIX RB_ST2FIX |
| 36 |
/** @endcond */ |
| 37 |
|
| 38 |
RBIMPL_ATTR_CONST_UNLESS_DEBUG() |
| 39 |
RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14) |
| 40 |
RBIMPL_ATTR_ARTIFICIAL() |
| 41 |
/** |
| 42 |
* Converts a C's `st_data_t` into an instance of ::rb_cInteger. |
| 43 |
* |
| 44 |
* @param[in] i The data in question. |
| 45 |
* @return A converted result |
| 46 |
* @warning THIS CONVERSION LOSES DATA! Be warned. |
| 47 |
* @see https://bugs.ruby-lang.org/issues/13877 |
| 48 |
* @see https://bugs.ruby-lang.org/issues/14218 |
| 49 |
* |
| 50 |
* @internal |
| 51 |
* |
| 52 |
* This is needed because of hash functions. Hash functions return |
| 53 |
* `st_data_t`, which could theoretically be bigger than Fixnums. However |
| 54 |
* allocating Bignums for them every time we calculate hash values is just too |
| 55 |
* heavy. To avoid penalty we need to ignore some upper bit(s) and stick to |
| 56 |
* Fixnums. This function is used for that purpose. |
| 57 |
*/ |
| 58 |
static inline VALUE |
| 59 |
RB_ST2FIX(st_data_t i) |
| 60 |
{ |
| 61 |
SIGNED_VALUE x = i; |
| 62 |
|
| 63 |
if (x >= 0) { |
| 64 |
x &= RUBY_FIXNUM_MAX; |
| 65 |
} |
| 66 |
else { |
| 67 |
x |= RUBY_FIXNUM_MIN; |
| 68 |
} |
| 69 |
|
| 70 |
RBIMPL_ASSERT_OR_ASSUME(RB_FIXABLE(x)); |
| 71 |
unsigned long y = RBIMPL_CAST((unsigned long)x); |
| 72 |
return RB_LONG2FIX(y); |
| 73 |
} |
| 74 |
|
| 75 |
#endif /* RBIMPL_ARITHMETIC_ST_DATA_T_H */ |