78 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 9a7211bb25 numeric.h: fix integer multiplication overflow check
The previous implementation of mrb_int_mul_overflow performed
the multiplication before checking for overflow. This is undefined
behavior for signed integers and can lead to incorrect results on
some compilers (e.g., MSVC).

The implementation has been changed to perform the overflow checks
before the multiplication.

Co-authored-by: Gemini <gemini@google.com>
2025-08-23 09:43:02 +09:00
Yukihiro "Matz" Matsumoto b99c389ec3 internal.h: aggregate internal functions.
Internal functions can only be called from within the library.
Functions listed in `mruby/internal.h` can be called from:

* core (src/*.c)
* gems (mrbgems/**/*.c)

But not from the application linked with `libmruby`.
2022-04-02 18:25:13 +09:00
Yukihiro "Matz" Matsumoto a869e4e540 numeric.h: remove obsolete functions.
- mrb_flo_to_fixnum()
- mrb_to_flo()

This commit removes weird abbreviation `_flo`.
2022-03-15 19:10:49 +09:00
Yukihiro "Matz" Matsumoto b0d6413f2d numops.c: rename functions for addition and subtraction.
To more consistent names.

* mrb_num_plus() -> mrb_num_add()
* mrb_num_minus() -> mrb_num_sub()

Since no one seems to use those functions, there should be no problem.
We added migration macros for compatibility for safety.
2022-03-12 10:54:11 +09:00
Yukihiro "Matz" Matsumoto b9e1b9b328 numeric.c: merge mrb_as_float implementation to mrb_ensure_float_type.
Since they are basically duplicated functionality. `mrb_as_float` is now
a macro defined using `mrb_ensure_float_type`; #5620
2021-12-31 15:14:37 +09:00
Yukihiro "Matz" Matsumoto 5c804cf68f Remove redundant include headers.
- stdlib.h
- stddef.h
- stdint.h
- stdarg.h
- limits.h
- float.h
2021-07-25 13:07:10 +09:00
Yukihiro "Matz" Matsumoto 7c8a91ff5a numeric.c: restore fmt argument for backward compatibility.
`mrb_float_to_str()` used to take `fmt` argument. We thought no one used
the function, and OK to remove the argument. But at least `mruby-redis`
gem used the function.
2021-06-15 16:19:16 +09:00
Yukihiro "Matz" Matsumoto 8a4bcc58c9 numeric.c: introduce mrb_int_to_cstr() to dump mrb_int.
* refactor `mrb_integer_to_str()`
* refactor `mrb_str_format()`
2021-05-30 08:15:51 +09:00
Yukihiro "Matz" Matsumoto 5c7fe225a6 fp_fmt.c: remove mrb_float_to_cstr().
The function was intended to be a utility function for `mruby-sprintf`.
The functionality was integrated into `sprintf.c`.
2021-05-22 14:16:55 +09:00
Yukihiro "Matz" Matsumoto 328563995f fmt_fp.c: replace with public domain float format routine; ref #5448
The original code can be found in `https://github.com/dhylands/format-float`.

Changes:
- support `double`
- support `#` (alt_form) modifier
- small refactoring
2021-05-22 09:35:27 +09:00
Yukihiro "Matz" Matsumoto beaf365dd4 numeric.h: remove 2 functions from MRB_API
- `mrb_float_to_str()`
- `mrb_float_to_cstr()`

Both functions will be replaced to support new coming `format-float.c`.
2021-05-21 08:13:38 +09:00
Yukihiro "Matz" Matsumoto 5eebbd7df2 Global renaming regarding integer and float.
Consistent number conversion function names:
* `mrb_value` to immediate (C) value
  * `mrb_int()` -> `mrb_as_int()`
  * `mrb_to_flo()` -> `mrb_as_float()`
* `mrb_value` to `mrb_value` (converted)
  * `mrb_to_int()'
  * `mrb_Integer()` - removed
  * `mrb_Float()` -> `mrb_to_float`

Consistent function name (avoid `_flo` suffix):
* `mrb_div_flo()` -> `mrb_div_float`
2021-05-17 15:07:05 +09:00
Yukihiro "Matz" Matsumoto ea7f1953c3 Rename mrb_fixnum_to_str to mrb_integer_to_str. 2021-05-17 13:59:01 +09:00
Yukihiro "Matz" Matsumoto 080fdbf9e8 Rename mrb_flo_to_fixnum to mrb_float_to_integer. 2021-05-17 13:58:54 +09:00
Yukihiro "Matz" Matsumoto e7db158321 numeric.h: reduce conditional compilation branch on MRB_NO_FLOAT. 2021-05-16 18:21:08 +09:00
KOBAYASHI Shuji 48feda0692 Remove the definition of mrb_uint in numeric.h that is no longer needed 2021-01-11 16:20:27 +09:00
Yukihiro "Matz" Matsumoto a6119b0077 Fixed further wrong condition in overflow detection. 2020-11-15 08:08:55 +09:00
Yukihiro "Matz" Matsumoto bb1fb2f43d Fixed wrong condition in mrb_int_mul_overflow(). 2020-11-14 23:34:43 +09:00
Yukihiro "Matz" Matsumoto 2c6a5d3a77 Change the fallback mrb_int_mul_overflow(). 2020-11-14 23:28:05 +09:00
Yukihiro "Matz" Matsumoto dd3ed8d99c Allow full mrb_int operations in overflow detection.
Fix overflow detection in integer operations with `MRB_WORD_BOXING`.
This bug made `1073741824 == 1073741824+0` to be `false` on 32bit
platforms.
2020-11-14 20:40:06 +09:00
Yukihiro "Matz" Matsumoto 3ff2757bf7 Fix mrb_int_mul_overflow() to check either operand being zero. 2020-10-12 18:20:22 +09:00
Yukihiro "Matz" Matsumoto 2b188ed8a1 Reorganize Integer system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
2020-10-12 18:19:54 +09:00
dearblue 80fe9838d2 Integrate Fixnum class into Integer class
* The `Fixnum` constant is now an alias for the `Integer` class.
* Remove `struct mrb_state::fixnum_class` member.
  If necessary, use `struct mrb_state::integer_class` instead.
2020-10-12 16:21:44 +09:00
Yukihiro "Matz" Matsumoto 8a87549315 Rename float configuration option names.
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT`
- `MRB_USE_FLOAT` => `MRB_USE_FLOAT32`

The former is to use `USE_XXX` naming convention. The latter is to make
sure `float` is 32bit float and not floating point number in general.
2020-10-12 16:21:40 +09:00
dearblue d983269908 Add mrb_float_to_cstr() MRB_API function
In order to share the same operation with `mrb_float_to_str()`,
the internal structure is modified.
2020-04-11 22:11:24 +09:00
David Siaw b5299b1c58 fix up documentation for values 2019-08-18 20:12:44 +09:00
dearblue eb86340139 Add constants for floating point number 2019-08-04 18:34:46 +09:00
Yukihiro "Matz" Matsumoto 6f67fddb87 Fix FIXABLE_FLOAT() on MRB_INT64; ref #4566
Normal `TYPED_FIXABLE(f,mrb_float)` does not work on 64bit int from
casting problems. The new approach works well, but assumes two's
complement and IEEE-754 floating point numbers.
2019-07-22 13:23:53 +09:00
Yukihiro "Matz" Matsumoto e941cf06a3 Export mrb_int_value that converts mrb_float to Fixnum.
Or `Float` if `mrb_float` value is too big (or too small) to fit in
`mrb_int`. The `_int_` in `mrb_int_value` means `Integral` module, which
represents integer-like values in mruby.
2019-05-21 21:08:26 +09:00
Yukihiro "Matz" Matsumoto 8b8bf9f3f6 Add new functions for numerical operation; ref 237a57b
New functions:
* mrb_num_plus(mrb, x, y)
* mrb_num_minus(mrb, x, y)
* num_num_mul(mrb, x, y)
2019-05-20 14:59:25 +09:00
Yukihiro "Matz" Matsumoto 237a57bbe8 Make unused functions private.
* mrb_fixnum_plus()
* mrb_fixnum_minus()
* mrb_fixnum_mul()
2019-05-17 11:30:38 +09:00
Yukihiro "Matz" Matsumoto 0c91da257b Remove unused mrb_num_div() function. 2019-05-17 11:27:21 +09:00
Yukihiro "Matz" Matsumoto 283530b19b Use mrb_float instead of double in FIXABLE_FLOAT. 2019-05-17 10:37:49 +09:00
YAMAMOTO Masaya acdc2d1f24 Add MRB_WITHOUT_FLOAT 2017-10-11 17:58:11 +09:00
Tomasz Dąbrowski f0c1074b07 fix: src\numeric.c(1215): warning C4244: 'function': conversion from 'mrb_int' to 'int', possible loss of data 2017-09-27 22:22:05 +02:00
Yukihiro "Matz" Matsumoto fc666bfe20 POSFIXABLE and NEGFIXABLE should take one argument. 2017-07-11 09:02:24 +09:00
Yukihiro "Matz" Matsumoto 46dee5716f Resurrect POSFIXABLE and NEGFIXABLE; ref 9186828 2017-07-10 09:42:48 +09:00
Yukihiro "Matz" Matsumoto 91868286be FIXABLE_FLOAT() fails sometimes in MRB_INT64 environment. 2017-07-06 09:05:46 +09:00
Yukihiro "Matz" Matsumoto 27a5e10454 Avoid using C++ style comments (//). 2017-05-30 11:47:59 +09:00
Yukihiro "Matz" Matsumoto 83fc915213 Add new range check macro FIXABLE_FLOAT(); ref #3652
When MRB_INT64, valid value range of mrb_int is bigger than double,
which only has 53 bits significant precision.
2017-05-26 09:54:10 +09:00
Tomasz Dabrowski bd2b76ea85 Build fix for 32-bit clang 3.8/3.9 with MRB_INT64 2017-02-16 13:39:21 +01:00
cremno 6369374f79 add mrb_int_mul_overflow(); close #3149
- call __builtin_mul_overflow() if it's available
- perform a 64-bit multiplication for 32-bit mrb_int (default)
- otherwise a much slower method is used
2016-05-10 19:10:49 +02:00
cremno 7276e84637 use type-generic checked arithmetic builtins
Version checking is not reliable - especially with Clang.
E.g. Apple's Clang (Xcode) uses different version numbers.
A feature check (__has_builtin) is the recommened way.

Add the MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
macro which may be used in other files.
2016-05-09 16:52:57 +02:00
cremno 39b6a9dafc define macros only if builtins are n/a 2016-05-09 16:34:20 +02:00
Yukihiro "Matz" Matsumoto 065966dae4 common.h are supposed to be included from other header, so call it with quotes; ref #3032 2015-11-28 00:10:38 +09:00
Yukihiro "Matz" Matsumoto 5c405dea3d include changed from by quotes ("") to by brackets (<>); close #3032 2015-11-27 17:48:23 +09:00
cremno d109e5b4e1 remove ifdef as MRB_FIXNUM_SHIFT is always defined 2015-11-06 19:27:39 +01:00
Seba Gamboa a0fe0c40b1 Remove old doxygen tags 2015-10-08 12:29:10 -03:00
Seba Gamboa 40bf7bde78 Sorting documentation grouping 2015-09-21 01:48:42 -03:00
Seba Gamboa c127614638 Setting up doxygen groups 2015-09-20 21:14:07 -03:00