| 1 |
#ifndef RBIMPL_RBIGNUM_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_RBIGNUM_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 Routines to manipulate struct RBignum. |
| 22 |
* @note The struct RBignum itself is opaque. |
| 23 |
*/ |
| 24 |
#include "ruby/internal/dllexport.h" |
| 25 |
#include "ruby/internal/value.h" |
| 26 |
#include "ruby/internal/value_type.h" |
| 27 |
#include "ruby/internal/stdbool.h" |
| 28 |
|
| 29 |
#define RBIGNUM_SIGN rb_big_sign /**< @alias{rb_big_sign} */ |
| 30 |
|
| 31 |
/** @cond INTERNAL_MACRO */ |
| 32 |
#define RBIGNUM_POSITIVE_P RBIGNUM_POSITIVE_P |
| 33 |
#define RBIGNUM_NEGATIVE_P RBIGNUM_NEGATIVE_P |
| 34 |
/** @endcond */ |
| 35 |
|
| 36 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 37 |
/** |
| 38 |
* The "sign" of a bignum. |
| 39 |
* |
| 40 |
* @param[in] num An object of RBignum. |
| 41 |
* @retval 1 It is greater than or equal to zero. |
| 42 |
* @retval 0 It is less than zero. |
| 43 |
* |
| 44 |
* @internal |
| 45 |
* |
| 46 |
* Implementation wise, unlike fixnums (which are 2's complement), bignums are |
| 47 |
* signed magnitude system. Theoretically it could be possible to have |
| 48 |
* negative zero instances. But in reality there is no way to create such |
| 49 |
* thing. Nobody ever needed that kind of insanity. |
| 50 |
*/ |
| 51 |
int rb_big_sign(VALUE num); |
| 52 |
RBIMPL_SYMBOL_EXPORT_END() |
| 53 |
|
| 54 |
/** |
| 55 |
* Checks if the bignum is positive. |
| 56 |
* @param[in] b An object of RBignum. |
| 57 |
* @retval false `b` is less than zero. |
| 58 |
* @retval true Otherwise. |
| 59 |
*/ |
| 60 |
static inline bool |
| 61 |
RBIGNUM_POSITIVE_P(VALUE b) |
| 62 |
{ |
| 63 |
RBIMPL_ASSERT_TYPE(b, RUBY_T_BIGNUM); |
| 64 |
return RBIGNUM_SIGN(b); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Checks if the bignum is negative. |
| 69 |
* @param[in] b An object of RBignum. |
| 70 |
* @retval true `b` is less than zero. |
| 71 |
* @retval false Otherwise. |
| 72 |
*/ |
| 73 |
static inline bool |
| 74 |
RBIGNUM_NEGATIVE_P(VALUE b) |
| 75 |
{ |
| 76 |
RBIMPL_ASSERT_TYPE(b, RUBY_T_BIGNUM); |
| 77 |
return ! RBIGNUM_POSITIVE_P(b); |
| 78 |
} |
| 79 |
|
| 80 |
#endif /* RBIMPL_RBIGNUM_H */ |