| 1 |
#ifndef RBIMPL_INTERN_BIGNUM_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_BIGNUM_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 Public APIs related to so-called rb_cBignum. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/config.h" |
| 24 |
|
| 25 |
#ifdef STDC_HEADERS |
| 26 |
# include <stddef.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "ruby/internal/attr/nonnull.h" |
| 30 |
#include "ruby/internal/dllexport.h" |
| 31 |
#include "ruby/internal/value.h" |
| 32 |
#include "ruby/backward/2/long_long.h" |
| 33 |
|
| 34 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 35 |
|
| 36 |
/* bignum.c */ |
| 37 |
|
| 38 |
/** |
| 39 |
* Allocates a bignum object. |
| 40 |
* |
| 41 |
* @param[in] len Length of the bignum's backend storage, in words. |
| 42 |
* @param[in] sign Sign of the bignum. |
| 43 |
* @return An allocated new bignum instance. |
| 44 |
* @note This only allocates an object, doesn't fill its value in. |
| 45 |
* |
| 46 |
* @internal |
| 47 |
* |
| 48 |
* @shyouhei finds it hard to use from extension libraries. `len` is per |
| 49 |
* `BDIGIT` but its definition is hidden. |
| 50 |
*/ |
| 51 |
VALUE rb_big_new(size_t len, int sign); |
| 52 |
|
| 53 |
/** |
| 54 |
* Queries if the passed bignum instance is a "bigzero". What is a bigzero? |
| 55 |
* Well, bignums are for very big integers, but can also represent tiny ones |
| 56 |
* like -1, 0, 1. Bigzero are instances of bignums whose values are zero. |
| 57 |
* Knowing if a bignum is bigzero can be handy on occasions, like for instance |
| 58 |
* detecting division by zero situation. |
| 59 |
* |
| 60 |
* @param[in] x A bignum. |
| 61 |
* @retval 1 It is a bigzero. |
| 62 |
* @retval 0 Otherwise. |
| 63 |
*/ |
| 64 |
int rb_bigzero_p(VALUE x); |
| 65 |
|
| 66 |
/** |
| 67 |
* Duplicates the given bignum. |
| 68 |
* |
| 69 |
* @param[in] num A bignum. |
| 70 |
* @return An allocated bignum, who is equivalent to `num`. |
| 71 |
*/ |
| 72 |
VALUE rb_big_clone(VALUE num); |
| 73 |
|
| 74 |
/** |
| 75 |
* Destructively modify the passed bignum into 2's complement representation. |
| 76 |
* |
| 77 |
* @note By default bignums are in signed magnitude system. |
| 78 |
* |
| 79 |
* @param[out] num A bignum to modify. |
| 80 |
*/ |
| 81 |
void rb_big_2comp(VALUE num); |
| 82 |
|
| 83 |
/** |
| 84 |
* Normalises the passed bignum. It for instance returns a fixnum of the same |
| 85 |
* value if fixnum can represent that number. |
| 86 |
* |
| 87 |
* @param[out] x Target bignum (can be destructively modified). |
| 88 |
* @return An integer of the identical value (can be `x` itself). |
| 89 |
*/ |
| 90 |
VALUE rb_big_norm(VALUE x); |
| 91 |
|
| 92 |
/** |
| 93 |
* Destructively resizes the backend storage of the passed bignum. |
| 94 |
* |
| 95 |
* @param[out] big A bignum. |
| 96 |
* @param[in] len New length of `big`'s backend, in words. |
| 97 |
*/ |
| 98 |
void rb_big_resize(VALUE big, size_t len); |
| 99 |
|
| 100 |
RBIMPL_ATTR_NONNULL(()) |
| 101 |
/** |
| 102 |
* Parses C's string to convert into a Ruby's integer. It understands prefixes |
| 103 |
* (e.g. `0x`) and underscores. |
| 104 |
* |
| 105 |
* @param[in] str Stringised representation of the return value. |
| 106 |
* @param[in] base Base of conversion. Must be `-36..36` inclusive, |
| 107 |
* except `1`. `2..36` means the conversion is done |
| 108 |
* according to it, with unmatched prefix understood |
| 109 |
* as a part of the result. `-36..-2` means the |
| 110 |
* conversion honours prefix when present, or use |
| 111 |
* `-base` when absent. `0` is equivalent to `-10`. |
| 112 |
* `-1` mandates a prefix. `1` is an error. |
| 113 |
* @param[in] badcheck Whether to raise ::rb_eArgError on failure. If |
| 114 |
* `0` is passed here this function can return |
| 115 |
* `INT2FIX(0)` for parse errors. |
| 116 |
* @exception rb_eArgError Failed to parse (and `badcheck` is truthy). |
| 117 |
* @return An instance of ::rb_cInteger, which is a numeric interpretation |
| 118 |
* of what is written in `str`. |
| 119 |
* |
| 120 |
* @internal |
| 121 |
* |
| 122 |
* Not sure if it intentionally accepts `base == -1` or is just buggy. Nobody |
| 123 |
* practically uses negative bases these days. |
| 124 |
*/ |
| 125 |
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck); |
| 126 |
|
| 127 |
/** |
| 128 |
* Identical to rb_cstr2inum(), except it takes Ruby's strings instead of C's. |
| 129 |
* |
| 130 |
* @param[in] str Stringised representation of the return |
| 131 |
* value. |
| 132 |
* @param[in] base Base of conversion. Must be `-36..36` |
| 133 |
* inclusive, except `1`. `2..36` means the |
| 134 |
* conversion is done according to it, with |
| 135 |
* unmatched prefix understood as a part of the |
| 136 |
* result. `-36..-2` means the conversion |
| 137 |
* honours prefix when present, or use `-base` |
| 138 |
* when absent. `0` is equivalent to `-10`. |
| 139 |
* `-1` mandates a prefix. `1` is an error. |
| 140 |
* @param[in] badcheck Whether to raise ::rb_eArgError on failure. |
| 141 |
* If `0` is passed here this function can |
| 142 |
* return `INT2FIX(0)` for parse errors. |
| 143 |
* @exception rb_eArgError Failed to parse (and `badcheck` is truthy). |
| 144 |
* @exception rb_eTypeError `str` is not a string. |
| 145 |
* @exception rb_eEncCompatError `str` is not ASCII compatible. |
| 146 |
* @return An instance of ::rb_cInteger, which is a numeric interpretation |
| 147 |
* of what is written in `str`. |
| 148 |
*/ |
| 149 |
VALUE rb_str_to_inum(VALUE str, int base, int badcheck); |
| 150 |
|
| 151 |
RBIMPL_ATTR_NONNULL(()) |
| 152 |
/** |
| 153 |
* Identical to rb_cstr_to_inum(), except the second argument controls the base |
| 154 |
* and badcheck at once. It basically doesn't raise for parse errors, unless |
| 155 |
* the base is zero. |
| 156 |
* |
| 157 |
* This is an older API. New codes might prefer rb_cstr_to_inum(). |
| 158 |
* |
| 159 |
* @param[in] str Stringised representation of the return value. |
| 160 |
* @param[in] base Base of conversion. Must be `-36..36` inclusive, |
| 161 |
* except `1`. `2..36` means the conversion is done |
| 162 |
* according to it, with unmatched prefix understood |
| 163 |
* as a part of the result. `-36..-2` means the |
| 164 |
* conversion honours prefix when present, or use |
| 165 |
* `-base` when absent. `0` is equivalent to `-10`. |
| 166 |
* `-1` mandates a prefix. `1` is an error. |
| 167 |
* @exception rb_eArgError Failed to parse (and `base` is zero). |
| 168 |
* @return An instance of ::rb_cInteger, which is a numeric interpretation |
| 169 |
* of what is written in `str`. |
| 170 |
*/ |
| 171 |
VALUE rb_cstr2inum(const char *str, int base); |
| 172 |
|
| 173 |
/** |
| 174 |
* Identical to rb_str_to_inum(), except the second argument controls the base |
| 175 |
* and badcheck at once. It can also be seen as a routine identical to |
| 176 |
* rb_cstr2inum(), except it takes Ruby's strings instead of C's. |
| 177 |
* |
| 178 |
* This is an older API. New codes might prefer rb_cstr_to_inum(). |
| 179 |
* |
| 180 |
* @param[in] str Stringised representation of the return |
| 181 |
* value. |
| 182 |
* @param[in] base Base of conversion. Must be `-36..36` |
| 183 |
* inclusive, except `1`. `2..36` means the |
| 184 |
* conversion is done according to it, with |
| 185 |
* unmatched prefix understood as a part of the |
| 186 |
* result. `-36..-2` means the conversion |
| 187 |
* honours prefix when present, or use `-base` |
| 188 |
* when absent. `0` is equivalent to `-10`. |
| 189 |
* `-1` mandates a prefix. `1` is an error. |
| 190 |
* @exception rb_eArgError Failed to parse (and `base` is zero). |
| 191 |
* @exception rb_eTypeError `str` is not a string. |
| 192 |
* @exception rb_eEncCompatError `str` is not ASCII compatible. |
| 193 |
* @return An instance of ::rb_cInteger, which is a numeric interpretation |
| 194 |
* of what is written in `str`. |
| 195 |
*/ |
| 196 |
VALUE rb_str2inum(VALUE str, int base); |
| 197 |
|
| 198 |
/** |
| 199 |
* Generates a place-value representation of the passed integer. |
| 200 |
* |
| 201 |
* @param[in] x An integer to stringify. |
| 202 |
* @param[in] base `2` to `36` inclusive for each radix. |
| 203 |
* @exception rb_eArgError `base` is out of range. |
| 204 |
* @exception rb_eRangeError `x` is too big, cannot represent in string. |
| 205 |
* @return An instance of ::rb_cString which represents `x`. |
| 206 |
*/ |
| 207 |
VALUE rb_big2str(VALUE x, int base); |
| 208 |
|
| 209 |
/** |
| 210 |
* Converts a bignum into C's `long`. |
| 211 |
* |
| 212 |
* @param[in] x A bignum. |
| 213 |
* @exception rb_eRangeError `x` is out of range of `long`. |
| 214 |
* @return The passed value converted into C's `long`. |
| 215 |
*/ |
| 216 |
long rb_big2long(VALUE x); |
| 217 |
|
| 218 |
/** @alias{rb_big2long} */ |
| 219 |
#define rb_big2int(x) rb_big2long(x) |
| 220 |
|
| 221 |
/** |
| 222 |
* Converts a bignum into C's `unsigned long`. |
| 223 |
* |
| 224 |
* @param[in] x A bignum. |
| 225 |
* @exception rb_eRangeError `x` is out of range of `unsigned long`. |
| 226 |
* @return The passed value converted into C's `unsigned long`. |
| 227 |
* |
| 228 |
* @internal |
| 229 |
* |
| 230 |
* This function can generate a very large positive integer for a negative |
| 231 |
* input. For instance applying Ruby's -4,611,686,018,427,387,905 to this |
| 232 |
* function yields C's 13,835,058,055,282,163,711 on my machine. This is how |
| 233 |
* it has been. Cannot change any longer. |
| 234 |
*/ |
| 235 |
unsigned long rb_big2ulong(VALUE x); |
| 236 |
|
| 237 |
/** @alias{rb_big2long} */ |
| 238 |
#define rb_big2uint(x) rb_big2ulong(x) |
| 239 |
|
| 240 |
#if HAVE_LONG_LONG |
| 241 |
/** |
| 242 |
* Converts a bignum into C's `long long`. |
| 243 |
* |
| 244 |
* @param[in] x A bignum. |
| 245 |
* @exception rb_eRangeError `x` is out of range of `long long`. |
| 246 |
* @return The passed value converted into C's `long long`. |
| 247 |
*/ |
| 248 |
LONG_LONG rb_big2ll(VALUE); |
| 249 |
|
| 250 |
/** |
| 251 |
* Converts a bignum into C's `unsigned long long`. |
| 252 |
* |
| 253 |
* @param[in] x A bignum. |
| 254 |
* @exception rb_eRangeError `x` is out of range of `unsigned long long`. |
| 255 |
* @return The passed value converted into C's `unsigned long long`. |
| 256 |
* |
| 257 |
* @internal |
| 258 |
* |
| 259 |
* This function can generate a very large positive integer for a negative |
| 260 |
* input. For instance applying Ruby's -4,611,686,018,427,387,905 to this |
| 261 |
* function yields C's 13,835,058,055,282,163,711 on my machine. This is how |
| 262 |
* it has been. Cannot change any longer. |
| 263 |
*/ |
| 264 |
unsigned LONG_LONG rb_big2ull(VALUE); |
| 265 |
|
| 266 |
#endif /* HAVE_LONG_LONG */ |
| 267 |
|
| 268 |
RBIMPL_ATTR_NONNULL(()) |
| 269 |
/** |
| 270 |
* Converts a bignum into a series of its parts. |
| 271 |
* |
| 272 |
* @param[in] val An integer. |
| 273 |
* @param[out] buf Return buffer. |
| 274 |
* @param[in] num_longs Number of words of `buf`. |
| 275 |
* @exception rb_eTypeError `val` doesn't respond to `#to_int`. |
| 276 |
* @post `buf` is filled with `val`'s 2's complement representation, in |
| 277 |
* the host CPU's native byte order, from least significant word |
| 278 |
* towards the most significant one, for `num_longs` words. |
| 279 |
* @note The "pack" terminology comes from `Array#pack`. |
| 280 |
*/ |
| 281 |
void rb_big_pack(VALUE val, unsigned long *buf, long num_longs); |
| 282 |
|
| 283 |
RBIMPL_ATTR_NONNULL(()) |
| 284 |
/** |
| 285 |
* Constructs a (possibly very big) bignum from a series of integers. `buf[0]` |
| 286 |
* would be the return value's least significant word; `buf[num_longs-1]` would |
| 287 |
* be that of most significant. |
| 288 |
* |
| 289 |
* @param[in] buf A series of integers. |
| 290 |
* @param[in] num_longs Number of words of `buf`. |
| 291 |
* @exception rb_eArgError Result would be too big. |
| 292 |
* @return An instance of ::rb_cInteger which is an "unpack"-ed value of |
| 293 |
* the parameters. |
| 294 |
* @note The "unpack" terminology comes from `String#pack`. |
| 295 |
*/ |
| 296 |
VALUE rb_big_unpack(unsigned long *buf, long num_longs); |
| 297 |
|
| 298 |
/* pack.c */ |
| 299 |
|
| 300 |
RBIMPL_ATTR_NONNULL(()) |
| 301 |
/** |
| 302 |
* Encodes a Unicode codepoint into its UTF-8 representation. |
| 303 |
* |
| 304 |
* @param[out] buf Return buffer, must at least be 6 bytes width. |
| 305 |
* @param[in] uv An Unicode codepoint. |
| 306 |
* @exception rb_eRangeError `uv` is out of Unicode. |
| 307 |
* @return Number of bytes written to `buf` |
| 308 |
* @post `buf` holds a UTF-8 representation of `uv`. |
| 309 |
*/ |
| 310 |
int rb_uv_to_utf8(char buf[6], unsigned long uv); |
| 311 |
|
| 312 |
/* bignum.c */ |
| 313 |
|
| 314 |
/** |
| 315 |
* Converts a C's `double` into a bignum. |
| 316 |
* |
| 317 |
* @param[in] d A value to convert. |
| 318 |
* @exception rb_eFloatDomainError `d` is Inf/NaN. |
| 319 |
* @return An instance of ::rb_cInteger whose value is approximately `d`. |
| 320 |
* |
| 321 |
* @internal |
| 322 |
* |
| 323 |
* @shyouhei is not sure if the result is guaranteed to be the nearest integer |
| 324 |
* of `d`. |
| 325 |
*/ |
| 326 |
VALUE rb_dbl2big(double d); |
| 327 |
|
| 328 |
/** |
| 329 |
* Converts a bignum into C's `double`. |
| 330 |
* |
| 331 |
* @param[in] x A bignum. |
| 332 |
* @return The passed value converted into C's `double`. |
| 333 |
* |
| 334 |
* @internal |
| 335 |
* |
| 336 |
* @shyouhei is not sure if the result is guaranteed to be `x`'s nearest value |
| 337 |
* that a `double` can represent. |
| 338 |
*/ |
| 339 |
double rb_big2dbl(VALUE x); |
| 340 |
|
| 341 |
/** |
| 342 |
* Compares the passed two bignums. |
| 343 |
* |
| 344 |
* @param[in] lhs Comparison LHS. |
| 345 |
* @param[in] rhs Comparison RHS. |
| 346 |
* @retval -1 `rhs` is bigger than `lhs`. |
| 347 |
* @retval 0 They are identical. |
| 348 |
* @retval 1 `lhs` is bigger than `rhs`. |
| 349 |
* @see rb_num_coerce_cmp() |
| 350 |
*/ |
| 351 |
VALUE rb_big_cmp(VALUE lhs, VALUE rhs); |
| 352 |
|
| 353 |
/** |
| 354 |
* Equality, in terms of `==`. This checks if the _value_ is the same, not the |
| 355 |
* identity. For instance `1 == 1.0` must hold. |
| 356 |
* |
| 357 |
* @param[in] lhs Comparison LHS. |
| 358 |
* @param[in] rhs Comparison RHS. |
| 359 |
* @retval RUBY_Qtrue They are the same. |
| 360 |
* @retval RUBY_Qfalse They are different. |
| 361 |
*/ |
| 362 |
VALUE rb_big_eq(VALUE lhs, VALUE rhs); |
| 363 |
|
| 364 |
/** |
| 365 |
* Equality, in terms of `eql?`. Unlike rb_big_eq() it does not convert |
| 366 |
* ::rb_cFloat etc. This function returns ::RUBY_Qtrue if and only if both |
| 367 |
* parameters are bignums, which represent the identical numerical value. |
| 368 |
* |
| 369 |
* @param[in] lhs Comparison LHS. |
| 370 |
* @param[in] rhs Comparison RHS. |
| 371 |
* @retval RUBY_Qtrue They are identical. |
| 372 |
* @retval RUBY_Qfalse They are distinct. |
| 373 |
*/ |
| 374 |
VALUE rb_big_eql(VALUE lhs, VALUE rhs); |
| 375 |
|
| 376 |
/** |
| 377 |
* Performs addition of the passed two objects. |
| 378 |
* |
| 379 |
* @param[in] x A bignum. |
| 380 |
* @param[in] y Arbitrary ruby object. |
| 381 |
* @return What `x + y` evaluates to. |
| 382 |
* @see rb_num_coerce_bin() |
| 383 |
*/ |
| 384 |
VALUE rb_big_plus(VALUE x, VALUE y); |
| 385 |
|
| 386 |
/** |
| 387 |
* Performs subtraction of the passed two objects. |
| 388 |
* |
| 389 |
* @param[in] x A bignum. |
| 390 |
* @param[in] y Arbitrary ruby object. |
| 391 |
* @return What `x - y` evaluates to. |
| 392 |
* @see rb_num_coerce_bin() |
| 393 |
*/ |
| 394 |
VALUE rb_big_minus(VALUE x, VALUE y); |
| 395 |
|
| 396 |
/** |
| 397 |
* Performs multiplication of the passed two objects. |
| 398 |
* |
| 399 |
* @param[in] x A bignum. |
| 400 |
* @param[in] y Arbitrary ruby object. |
| 401 |
* @return What `x * y` evaluates to. |
| 402 |
* @see rb_num_coerce_bin() |
| 403 |
*/ |
| 404 |
VALUE rb_big_mul(VALUE x, VALUE y); |
| 405 |
|
| 406 |
/** |
| 407 |
* Performs division of the passed two objects. |
| 408 |
* |
| 409 |
* @param[in] x A bignum. |
| 410 |
* @param[in] y Arbitrary ruby object. |
| 411 |
* @return What `x / y` evaluates to. |
| 412 |
* @see rb_num_coerce_bin() |
| 413 |
*/ |
| 414 |
VALUE rb_big_div(VALUE x, VALUE y); |
| 415 |
|
| 416 |
/** |
| 417 |
* Performs "integer division". This is different from rb_big_div(). |
| 418 |
* |
| 419 |
* @param[in] x A bignum. |
| 420 |
* @param[in] y Arbitrary ruby object. |
| 421 |
* @return What `x.div y` evaluates to. |
| 422 |
* @see rb_num_coerce_bin() |
| 423 |
*/ |
| 424 |
VALUE rb_big_idiv(VALUE x, VALUE y); |
| 425 |
|
| 426 |
/** |
| 427 |
* Performs modulo of the passed two objects. |
| 428 |
* |
| 429 |
* @param[in] x A bignum. |
| 430 |
* @param[in] y Arbitrary ruby object. |
| 431 |
* @return What `x % y` evaluates to. |
| 432 |
* @see rb_num_coerce_bin() |
| 433 |
* |
| 434 |
* @internal |
| 435 |
* |
| 436 |
* There also is `rb_big_remainder()` internally, which is different from this |
| 437 |
* one. |
| 438 |
*/ |
| 439 |
VALUE rb_big_modulo(VALUE x, VALUE y); |
| 440 |
|
| 441 |
/** |
| 442 |
* Performs "divmod" operation. The operation in bignum's context is that it |
| 443 |
* calculates rb_big_idiv() and rb_big_modulo() at once. |
| 444 |
* |
| 445 |
* @param[in] x A bignum. |
| 446 |
* @param[in] y Arbitrary ruby object. |
| 447 |
* @return What `x.divmod y` evaluates to. |
| 448 |
* @see rb_num_coerce_bin() |
| 449 |
*/ |
| 450 |
VALUE rb_big_divmod(VALUE x, VALUE y); |
| 451 |
|
| 452 |
/** |
| 453 |
* Raises `x` to the powerof `y`. |
| 454 |
* |
| 455 |
* @param[in] x A bignum. |
| 456 |
* @param[in] y Arbitrary ruby object. |
| 457 |
* @return What `x ** y` evaluates to. |
| 458 |
* @see rb_num_coerce_bin() |
| 459 |
* @note This can return an instance of ::rb_cFloat, even when both `x` |
| 460 |
* and `y` are bignums. Or an instance of ::rb_cRational, when for |
| 461 |
* instance `y` is negative. |
| 462 |
*/ |
| 463 |
VALUE rb_big_pow(VALUE x, VALUE y); |
| 464 |
|
| 465 |
/** |
| 466 |
* Performs bitwise and of the passed two objects. |
| 467 |
* |
| 468 |
* @param[in] x A bignum. |
| 469 |
* @param[in] y Arbitrary ruby object. |
| 470 |
* @return What `x & y` evaluates to. |
| 471 |
* @see rb_num_coerce_bit() |
| 472 |
*/ |
| 473 |
VALUE rb_big_and(VALUE x, VALUE y); |
| 474 |
|
| 475 |
/** |
| 476 |
* Performs bitwise or of the passed two objects. |
| 477 |
* |
| 478 |
* @param[in] x A bignum. |
| 479 |
* @param[in] y Arbitrary ruby object. |
| 480 |
* @return What `x | y` evaluates to. |
| 481 |
* @see rb_num_coerce_bit() |
| 482 |
*/ |
| 483 |
VALUE rb_big_or(VALUE x, VALUE y); |
| 484 |
|
| 485 |
/** |
| 486 |
* Performs exclusive or of the passed two objects. |
| 487 |
* |
| 488 |
* @param[in] x A bignum. |
| 489 |
* @param[in] y Arbitrary ruby object. |
| 490 |
* @return What `x ^ y` evaluates to. |
| 491 |
* @see rb_num_coerce_bit() |
| 492 |
*/ |
| 493 |
VALUE rb_big_xor(VALUE x, VALUE y); |
| 494 |
|
| 495 |
/** |
| 496 |
* Performs shift left. |
| 497 |
* |
| 498 |
* @param[in] x A bignum. |
| 499 |
* @param[in] y Shift amount. |
| 500 |
* @exception rb_eTypeError `y` is not an integer. |
| 501 |
* @exception rb_eArgError `y` is too big. |
| 502 |
* @return `x` shifted left to `y` bits. |
| 503 |
* @note `y` can be negative. Shifts right then. |
| 504 |
*/ |
| 505 |
VALUE rb_big_lshift(VALUE x, VALUE y); |
| 506 |
|
| 507 |
/** |
| 508 |
* Performs shift right. |
| 509 |
* |
| 510 |
* @param[in] x A bignum. |
| 511 |
* @param[in] y Shift amount. |
| 512 |
* @exception rb_eTypeError `y` is not an integer. |
| 513 |
* @return `x` shifted right to `y` bits. |
| 514 |
* @note This is arithmetic. Because bignums are not bitfields there is |
| 515 |
* no shift right logical operator. |
| 516 |
*/ |
| 517 |
VALUE rb_big_rshift(VALUE x, VALUE y); |
| 518 |
|
| 519 |
/** |
| 520 |
* @name Flags for rb_integer_pack()/rb_integer_unpack() |
| 521 |
* @{ |
| 522 |
*/ |
| 523 |
|
| 524 |
/** Stores/interprets the most significant word as the first word. */ |
| 525 |
#define INTEGER_PACK_MSWORD_FIRST 0x01 |
| 526 |
|
| 527 |
/** Stores/interprets the least significant word as the first word. */ |
| 528 |
#define INTEGER_PACK_LSWORD_FIRST 0x02 |
| 529 |
|
| 530 |
/** |
| 531 |
* Stores/interprets the most significant byte in a word as the first byte in |
| 532 |
* the word. |
| 533 |
*/ |
| 534 |
#define INTEGER_PACK_MSBYTE_FIRST 0x10 |
| 535 |
|
| 536 |
/** |
| 537 |
* Stores/interprets the least significant byte in a word as the first byte in |
| 538 |
* the word. |
| 539 |
*/ |
| 540 |
#define INTEGER_PACK_LSBYTE_FIRST 0x20 |
| 541 |
|
| 542 |
/** |
| 543 |
* Means either #INTEGER_PACK_MSBYTE_FIRST or #INTEGER_PACK_LSBYTE_FIRST, |
| 544 |
* depending on the host processor's endian. |
| 545 |
*/ |
| 546 |
#define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40 |
| 547 |
|
| 548 |
/** Uses 2's complement representation. */ |
| 549 |
#define INTEGER_PACK_2COMP 0x80 |
| 550 |
|
| 551 |
/** Uses "generic" implementation (handy on test). */ |
| 552 |
#define INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION 0x400 |
| 553 |
|
| 554 |
/** |
| 555 |
* Always generates a bignum object even if the integer can be representable |
| 556 |
* using fixnum scheme (unpack only) |
| 557 |
*/ |
| 558 |
#define INTEGER_PACK_FORCE_BIGNUM 0x100 |
| 559 |
|
| 560 |
/** |
| 561 |
* Interprets the input as a signed negative number (unpack only). If not |
| 562 |
* specified returns a positive number. |
| 563 |
*/ |
| 564 |
#define INTEGER_PACK_NEGATIVE 0x200 |
| 565 |
|
| 566 |
/** Little endian combination. */ |
| 567 |
#define INTEGER_PACK_LITTLE_ENDIAN \ |
| 568 |
(INTEGER_PACK_LSWORD_FIRST | \ |
| 569 |
INTEGER_PACK_LSBYTE_FIRST) |
| 570 |
|
| 571 |
/** Big endian combination */ |
| 572 |
#define INTEGER_PACK_BIG_ENDIAN \ |
| 573 |
(INTEGER_PACK_MSWORD_FIRST | \ |
| 574 |
INTEGER_PACK_MSBYTE_FIRST) |
| 575 |
|
| 576 |
/** @} */ |
| 577 |
|
| 578 |
RBIMPL_ATTR_NONNULL(()) |
| 579 |
/** |
| 580 |
* Exports an integer into a buffer. This function fills the buffer specified |
| 581 |
* by `words` and `numwords` as `val` in the format specified by `wordsize`, |
| 582 |
* `nails` and `flags`. |
| 583 |
* |
| 584 |
* @param[in] val Integer or integer-like object which has |
| 585 |
* `#to_int` method. |
| 586 |
* @param[out] words Return buffer. |
| 587 |
* @param[in] numwords Number of words of `words`. |
| 588 |
* @param[in] wordsize Number of bytes per word. |
| 589 |
* @param[in] nails Number of padding bits in a word. Most |
| 590 |
* significant nails bits of each word are filled |
| 591 |
* by zero. |
| 592 |
* @param[in] flags Bitwise or of constants whose name starts |
| 593 |
* "INTEGER_PACK_". |
| 594 |
* @exception rb_eTypeError `val` doesn't respond to `#to_int`. |
| 595 |
* |
| 596 |
* Possible flags are: |
| 597 |
* |
| 598 |
* - #INTEGER_PACK_MSWORD_FIRST: |
| 599 |
* Stores the most significant word as the first word. |
| 600 |
* |
| 601 |
* - #INTEGER_PACK_LSWORD_FIRST: |
| 602 |
* Stores the least significant word as the first word. |
| 603 |
* |
| 604 |
* - #INTEGER_PACK_MSBYTE_FIRST: |
| 605 |
* Stores the most significant byte in a word as the first byte in the |
| 606 |
* word. |
| 607 |
* |
| 608 |
* - #INTEGER_PACK_LSBYTE_FIRST: |
| 609 |
* Stores the least significant byte in a word as the first byte in the |
| 610 |
* word. |
| 611 |
* |
| 612 |
* - #INTEGER_PACK_NATIVE_BYTE_ORDER: |
| 613 |
* Either #INTEGER_PACK_MSBYTE_FIRST or #INTEGER_PACK_LSBYTE_FIRST |
| 614 |
* corresponding to the host's endian. |
| 615 |
* |
| 616 |
* - #INTEGER_PACK_2COMP: |
| 617 |
* Uses 2's complement representation. |
| 618 |
* |
| 619 |
* - #INTEGER_PACK_LITTLE_ENDIAN: Shorthand of |
| 620 |
* `INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_LSBYTE_FIRST`. |
| 621 |
* |
| 622 |
* - #INTEGER_PACK_BIG_ENDIAN: Shorthand of |
| 623 |
* `INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_MSBYTE_FIRST`. |
| 624 |
* |
| 625 |
* - #INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION: |
| 626 |
* Uses generic implementation (for test and debug). |
| 627 |
* |
| 628 |
* This function fills the buffer specified by `words` as `val`'s 2's |
| 629 |
* complement representation if #INTEGER_PACK_2COMP is specified in `flags`. |
| 630 |
* Otherwise it fills `words` as `abs(val)` and signedness is returned via the |
| 631 |
* return value. |
| 632 |
* |
| 633 |
* @return The signedness and overflow condition. The overflow condition |
| 634 |
* depends on #INTEGER_PACK_2COMP. |
| 635 |
* |
| 636 |
* When #INTEGER_PACK_2COMP is not specified: |
| 637 |
* |
| 638 |
* - `-2` : |
| 639 |
* Negative overflow. `val <= -2**(numwords*(wordsize*CHAR_BIT-nails))` |
| 640 |
* |
| 641 |
* - `-1` : |
| 642 |
* Negative without overflow. |
| 643 |
* `-2**(numwords*(wordsize*CHAR_BIT-nails)) < val < 0` |
| 644 |
* |
| 645 |
* - `0` : zero. `val == 0` |
| 646 |
* |
| 647 |
* - `1` : |
| 648 |
* Positive without overflow. |
| 649 |
* `0 < val < 2**(numwords*(wordsize*CHAR_BIT-nails))` |
| 650 |
* |
| 651 |
* - `2` : |
| 652 |
* Positive overflow. `2**(numwords*(wordsize*CHAR_BIT-nails)) <= val` |
| 653 |
* |
| 654 |
* When #INTEGER_PACK_2COMP is specified: |
| 655 |
* |
| 656 |
* - `-2` : |
| 657 |
* Negative overflow. `val < -2**(numwords*(wordsize*CHAR_BIT-nails))` |
| 658 |
* |
| 659 |
* - `-1` : |
| 660 |
* Negative without overflow. |
| 661 |
* `-2**(numwords*(wordsize*CHAR_BIT-nails)) <= val < 0` |
| 662 |
* |
| 663 |
* - `0` : zero. `val == 0` |
| 664 |
* |
| 665 |
* - `1` : |
| 666 |
* Positive without overflow. |
| 667 |
* `0 < val < 2**(numwords*(wordsize*CHAR_BIT-nails))` |
| 668 |
* |
| 669 |
* - `2` : |
| 670 |
* Positive overflow. `2**(numwords*(wordsize*CHAR_BIT-nails)) <= val` |
| 671 |
* |
| 672 |
* The value, `-2**(numwords*(wordsize*CHAR_BIT-nails))`, is representable in |
| 673 |
* 2's complement representation but not representable in absolute value. So |
| 674 |
* `-1` is returned for the value if #INTEGER_PACK_2COMP is specified but |
| 675 |
* returns `-2` if #INTEGER_PACK_2COMP is not specified. |
| 676 |
* |
| 677 |
* The least significant words are filled in the buffer when overflow occur. |
| 678 |
*/ |
| 679 |
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags); |
| 680 |
|
| 681 |
RBIMPL_ATTR_NONNULL(()) |
| 682 |
/** |
| 683 |
* Import an integer from a buffer. |
| 684 |
* |
| 685 |
* @param[in] words Buffer to import. |
| 686 |
* @param[in] numwords Number of words of `words`. |
| 687 |
* @param[in] wordsize Number of bytes per word. |
| 688 |
* @param[in] nails Number of padding bits in a word. Most |
| 689 |
* significant nails bits of each word are ignored. |
| 690 |
* @param[in] flags Bitwise or of constants whose name starts |
| 691 |
* "INTEGER_PACK_". |
| 692 |
* @exception rb_eArgError `numwords * wordsize` too big. |
| 693 |
* |
| 694 |
* Possible flags are: |
| 695 |
* |
| 696 |
* - #INTEGER_PACK_MSWORD_FIRST: |
| 697 |
* Interpret the first word as the most significant word. |
| 698 |
* |
| 699 |
* - #INTEGER_PACK_LSWORD_FIRST: |
| 700 |
* Interpret the first word as the least significant word. |
| 701 |
* |
| 702 |
* - #INTEGER_PACK_MSBYTE_FIRST: |
| 703 |
* Interpret the first byte in a word as the most significant byte in the |
| 704 |
* word. |
| 705 |
* |
| 706 |
* - #INTEGER_PACK_LSBYTE_FIRST: |
| 707 |
* Interpret the first byte in a word as the least significant byte in |
| 708 |
* the word. |
| 709 |
* |
| 710 |
* - #INTEGER_PACK_NATIVE_BYTE_ORDER: |
| 711 |
* Either #INTEGER_PACK_MSBYTE_FIRST or #INTEGER_PACK_LSBYTE_FIRST |
| 712 |
* corresponding to the host's endian. |
| 713 |
* |
| 714 |
* - #INTEGER_PACK_2COMP: |
| 715 |
* Uses 2's complement representation. |
| 716 |
* |
| 717 |
* - #INTEGER_PACK_LITTLE_ENDIAN: Shorthand of |
| 718 |
* `INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_LSBYTE_FIRST` |
| 719 |
* |
| 720 |
* - #INTEGER_PACK_BIG_ENDIAN: Shorthand of |
| 721 |
* `INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_MSBYTE_FIRST` |
| 722 |
* |
| 723 |
* - #INTEGER_PACK_FORCE_BIGNUM: |
| 724 |
* Returns a bignum even if its value is representable as a fixnum. |
| 725 |
* |
| 726 |
* - #INTEGER_PACK_NEGATIVE: |
| 727 |
* Returns a non-positive value. (Returns a non-negative value if not |
| 728 |
* specified.) |
| 729 |
* |
| 730 |
* - #INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION: |
| 731 |
* Uses generic implementation (for test and debug). |
| 732 |
* |
| 733 |
* @return An instance of ::rb_cInteger whose value is the interpreted |
| 734 |
* `words`. The range of the result value depends on |
| 735 |
* #INTEGER_PACK_2COMP and #INTEGER_PACK_NEGATIVE. |
| 736 |
* |
| 737 |
* When #INTEGER_PACK_2COMP is not set: |
| 738 |
* |
| 739 |
* - `0 <= val < 2**(numwords*(wordsize*CHAR_BIT-nails))` if |
| 740 |
* `!INTEGER_PACK_NEGATIVE` |
| 741 |
* |
| 742 |
* - `-2**(numwords*(wordsize*CHAR_BIT-nails)) < val <= 0` if |
| 743 |
* `INTEGER_PACK_NEGATIVE` |
| 744 |
* |
| 745 |
* When #INTEGER_PACK_2COMP is set: |
| 746 |
* |
| 747 |
* - `-2**(numwords*(wordsize*CHAR_BIT-nails)-1)` `<= val <=` |
| 748 |
* `2**(numwords*(wordsize*CHAR_BIT-nails)-1)-1` if |
| 749 |
* `!INTEGER_PACK_NEGATIVE` |
| 750 |
* |
| 751 |
* - `-2**(numwords*(wordsize*CHAR_BIT-nails)) <= val <= -1` if |
| 752 |
* `INTEGER_PACK_NEGATIVE` |
| 753 |
* |
| 754 |
* Passing #INTEGER_PACK_2COMP without #INTEGER_PACK_NEGATIVE means sign |
| 755 |
* extension. #INTEGER_PACK_2COMP with #INTEGER_PACK_NEGATIVE means assuming |
| 756 |
* the higher bits are `1`. |
| 757 |
* |
| 758 |
* Note that this function returns 0 when `numwords` is zero and |
| 759 |
* #INTEGER_PACK_2COMP is set but #INTEGER_PACK_NEGATIVE is not set. |
| 760 |
*/ |
| 761 |
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags); |
| 762 |
|
| 763 |
/** |
| 764 |
* Calculates the number of bytes needed to represent the absolute value of the |
| 765 |
* passed integer. |
| 766 |
* |
| 767 |
* @param[in] val Integer or integer-like object which has |
| 768 |
* `#to_int` method. |
| 769 |
* @param[out] nlz_bits_ret Number of leading zero bits in the most |
| 770 |
* significant byte is returned if not `NULL`. |
| 771 |
* @exception rb_eTypeError `val` doesn't respond to `#to_int`. |
| 772 |
* @return `((val_numbits * CHAR_BIT + CHAR_BIT - 1) / CHAR_BIT)`, where |
| 773 |
* val_numbits is the number of bits of `abs(val)`. |
| 774 |
* @post If `nlz_bits_ret` is not `NULL`, |
| 775 |
* `(return_value * CHAR_BIT - val_numbits)` is stored in |
| 776 |
* `*nlz_bits_ret`. In this case, |
| 777 |
* `0 <= *nlz_bits_ret < CHAR_BIT`. |
| 778 |
* |
| 779 |
* This function should not overflow. |
| 780 |
*/ |
| 781 |
size_t rb_absint_size(VALUE val, int *nlz_bits_ret); |
| 782 |
|
| 783 |
/** |
| 784 |
* Calculates the number of words needed represent the absolute value of the |
| 785 |
* passed integer. Unlike rb_absint_size() this function can overflow. It |
| 786 |
* returns `(size_t)-1` then. |
| 787 |
* |
| 788 |
* @param[in] val Integer or integer-like object which has |
| 789 |
* `#to_int` method. |
| 790 |
* @param[in] word_numbits Number of bits per word. |
| 791 |
* @param[out] nlz_bits_ret Number of leading zero bits in the most |
| 792 |
* significant word is returned if not `NULL`. |
| 793 |
* @exception rb_eTypeError `val` doesn't respond to `#to_int`. |
| 794 |
* @retval (size_t)-1 Overflowed. |
| 795 |
* @retval otherwise |
| 796 |
* `((val_numbits * CHAR_BIT + word_numbits - 1) / word_numbits)`, |
| 797 |
* where val_numbits is the number of bits of `abs(val)`. |
| 798 |
* @post If `nlz_bits_ret` is not `NULL` and there is no overflow, |
| 799 |
* `(return_value * word_numbits - val_numbits)` is stored in |
| 800 |
* `*nlz_bits_ret`. In this case, |
| 801 |
* `0 <= *nlz_bits_ret < word_numbits.` |
| 802 |
* |
| 803 |
*/ |
| 804 |
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret); |
| 805 |
|
| 806 |
/** |
| 807 |
* Tests `abs(val)` consists only of a bit or not. |
| 808 |
* |
| 809 |
* @param[in] val Integer or integer-like object which has |
| 810 |
* `#to_int` method. |
| 811 |
* @exception rb_eTypeError `val` doesn't respond to `#to_int`. |
| 812 |
* @retval 1 `abs(val) == 1 << n` for some `n >= 0`. |
| 813 |
* @retval 0 Otherwise. |
| 814 |
* |
| 815 |
* rb_absint_singlebit_p() can be used to determine required buffer size for |
| 816 |
* rb_integer_pack() used with #INTEGER_PACK_2COMP (two's complement). |
| 817 |
* |
| 818 |
* Following example calculates number of bits required to represent val in |
| 819 |
* two's complement number, without sign bit. |
| 820 |
* |
| 821 |
* ```CXX |
| 822 |
* size_t size; |
| 823 |
* int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val); |
| 824 |
* size = rb_absint_numwords(val, 1, NULL) |
| 825 |
* if (size == (size_t)-1) ...overflow... |
| 826 |
* if (neg && rb_absint_singlebit_p(val)) |
| 827 |
* size--; |
| 828 |
* ``` |
| 829 |
* |
| 830 |
* Following example calculates number of bytes required to represent val in |
| 831 |
* two's complement number, with sign bit. |
| 832 |
* |
| 833 |
* ```CXX |
| 834 |
* size_t size; |
| 835 |
* int neg = FIXNUM_P(val) ? FIX2LONG(val) < 0 : BIGNUM_NEGATIVE_P(val); |
| 836 |
* int nlz_bits; |
| 837 |
* size = rb_absint_size(val, &nlz_bits); |
| 838 |
* if (nlz_bits == 0 && !(neg && rb_absint_singlebit_p(val))) |
| 839 |
* size++; |
| 840 |
* ``` |
| 841 |
*/ |
| 842 |
int rb_absint_singlebit_p(VALUE val); |
| 843 |
|
| 844 |
RBIMPL_SYMBOL_EXPORT_END() |
| 845 |
|
| 846 |
#endif /* RBIMPL_INTERN_BIGNUM_H */ |