| 1 |
#ifndef RBIMPL_ARITHMETIC_LONG_LONG_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_ARITHMETIC_LONG_LONG_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 `long long` and Ruby's. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/value.h" |
| 24 |
#include "ruby/internal/dllexport.h" |
| 25 |
#include "ruby/internal/special_consts.h" |
| 26 |
#include "ruby/backward/2/long_long.h" |
| 27 |
|
| 28 |
#define RB_LL2NUM rb_ll2num_inline /**< @alias{rb_ll2num_inline} */ |
| 29 |
#define RB_ULL2NUM rb_ull2num_inline /**< @alias{rb_ull2num_inline} */ |
| 30 |
#define LL2NUM RB_LL2NUM /**< @old{RB_LL2NUM} */ |
| 31 |
#define ULL2NUM RB_ULL2NUM /**< @old{RB_ULL2NUM} */ |
| 32 |
#define RB_NUM2LL rb_num2ll_inline /**< @alias{rb_num2ll_inline} */ |
| 33 |
#define RB_NUM2ULL rb_num2ull_inline /**< @alias{rb_num2ull_inline} */ |
| 34 |
#define NUM2LL RB_NUM2LL /**< @old{RB_NUM2LL} */ |
| 35 |
#define NUM2ULL RB_NUM2ULL /**< @old{RB_NUM2ULL} */ |
| 36 |
|
| 37 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 38 |
/** |
| 39 |
* Converts a C's `long long` into an instance of ::rb_cInteger. |
| 40 |
* |
| 41 |
* @param[in] num Arbitrary `long long` value. |
| 42 |
* @return An instance of ::rb_cInteger. |
| 43 |
*/ |
| 44 |
VALUE rb_ll2inum(LONG_LONG num); |
| 45 |
|
| 46 |
/** |
| 47 |
* Converts a C's `unsigned long long` into an instance of ::rb_cInteger. |
| 48 |
* |
| 49 |
* @param[in] num Arbitrary `unsigned long long` value. |
| 50 |
* @return An instance of ::rb_cInteger. |
| 51 |
*/ |
| 52 |
VALUE rb_ull2inum(unsigned LONG_LONG num); |
| 53 |
|
| 54 |
/** |
| 55 |
* Converts an instance of ::rb_cNumeric into C's `long long`. |
| 56 |
* |
| 57 |
* @param[in] num Something numeric. |
| 58 |
* @exception rb_eTypeError `num` is not a numeric. |
| 59 |
* @exception rb_eRangeError `num` is out of range of `long long`. |
| 60 |
* @return The passed value converted into C's `long long`. |
| 61 |
*/ |
| 62 |
LONG_LONG rb_num2ll(VALUE num); |
| 63 |
|
| 64 |
/** |
| 65 |
* Converts an instance of ::rb_cNumeric into C's `unsigned long long`. |
| 66 |
* |
| 67 |
* @param[in] num Something numeric. |
| 68 |
* @exception rb_eTypeError `num` is not a numeric. |
| 69 |
* @exception rb_eRangeError `num` is out of range of `unsigned long long`. |
| 70 |
* @return The passed value converted into C's `unsigned long long`. |
| 71 |
*/ |
| 72 |
unsigned LONG_LONG rb_num2ull(VALUE num); |
| 73 |
RBIMPL_SYMBOL_EXPORT_END() |
| 74 |
|
| 75 |
/** |
| 76 |
* Converts a C's `long long` into an instance of ::rb_cInteger. |
| 77 |
* |
| 78 |
* @param[in] n Arbitrary `long long` value. |
| 79 |
* @return An instance of ::rb_cInteger |
| 80 |
*/ |
| 81 |
static inline VALUE |
| 82 |
rb_ll2num_inline(LONG_LONG n) |
| 83 |
{ |
| 84 |
if (FIXABLE(n)) return LONG2FIX((long)n); |
| 85 |
return rb_ll2inum(n); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Converts a C's `unsigned long long` into an instance of ::rb_cInteger. |
| 90 |
* |
| 91 |
* @param[in] n Arbitrary `unsigned long long` value. |
| 92 |
* @return An instance of ::rb_cInteger |
| 93 |
*/ |
| 94 |
static inline VALUE |
| 95 |
rb_ull2num_inline(unsigned LONG_LONG n) |
| 96 |
{ |
| 97 |
if (POSFIXABLE(n)) return LONG2FIX((long)n); |
| 98 |
return rb_ull2inum(n); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Converts an instance of ::rb_cNumeric into C's `long long`. |
| 103 |
* |
| 104 |
* @param[in] x Something numeric. |
| 105 |
* @exception rb_eTypeError `x` is not a numeric. |
| 106 |
* @exception rb_eRangeError `x` is out of range of `long long`. |
| 107 |
* @return The passed value converted into C's `long long`. |
| 108 |
*/ |
| 109 |
static inline LONG_LONG |
| 110 |
rb_num2ll_inline(VALUE x) |
| 111 |
{ |
| 112 |
if (RB_FIXNUM_P(x)) |
| 113 |
return RB_FIX2LONG(x); |
| 114 |
else |
| 115 |
return rb_num2ll(x); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Converts an instance of ::rb_cNumeric into C's `unsigned long long`. |
| 120 |
* |
| 121 |
* @param[in] x Something numeric. |
| 122 |
* @exception rb_eTypeError `x` is not a numeric. |
| 123 |
* @exception rb_eRangeError `x` is out of range of `unsigned long long`. |
| 124 |
* @return The passed value converted into C's `unsigned long long`. |
| 125 |
*/ |
| 126 |
static inline unsigned LONG_LONG |
| 127 |
rb_num2ull_inline(VALUE x) |
| 128 |
{ |
| 129 |
if (RB_FIXNUM_P(x)) |
| 130 |
return RB_FIX2LONG(x); |
| 131 |
else |
| 132 |
return rb_num2ull(x); |
| 133 |
} |
| 134 |
|
| 135 |
#endif /* RBIMPL_ARITHMETIC_LONG_LONG_H */ |