Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
6246483f
Commit
6246483f
authored
Apr 19, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2766 from furunkel/builtin_overflow
Use builtins for overflow math if possible
parents
0f31f160
08c87769
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
+43
-0
include/mruby/numeric.h
include/mruby/numeric.h
+43
-0
No files found.
include/mruby/numeric.h
View file @
6246483f
...
...
@@ -36,6 +36,47 @@ mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
# define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1))
#endif
/* Idea from Potion: https://github.com/perl11/potion (MIT) */
#if (defined(__clang__) && ((__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ >= 4))) \
|| (defined(__GNUC__) && __GNUC__ >= 5)
static
inline
mrb_bool
mrb_int_add_overflow
(
mrb_int
augend
,
mrb_int
addend
,
mrb_int
*
sum
)
{
mrb_bool
of
;
#ifdef MRB_INT64
long
long
val
;
of
=
__builtin_saddll_overflow
(
augend
,
addend
,
&
val
)
||
#else
int
val
;
of
=
__builtin_sadd_overflow
(
augend
,
addend
,
&
val
)
||
#endif
(
val
>
MRB_INT_MAX
)
||
(
val
<
MRB_INT_MIN
);
*
sum
=
(
mrb_int
)
val
;
return
of
;
}
static
inline
mrb_bool
mrb_int_sub_overflow
(
mrb_int
minuend
,
mrb_int
subtrahend
,
mrb_int
*
difference
)
{
mrb_bool
of
;
#ifdef MRB_INT64
long
long
val
;
of
=
__builtin_ssubll_overflow
(
minuend
,
subtrahend
,
&
val
)
||
#else
int
val
;
of
=
__builtin_ssub_overflow
(
minuend
,
subtrahend
,
&
val
)
||
#endif
(
val
>
MRB_INT_MAX
)
||
(
val
<
MRB_INT_MIN
);
*
difference
=
(
mrb_int
)
val
;
return
of
;
}
#else
static
inline
mrb_bool
mrb_int_add_overflow
(
mrb_int
augend
,
mrb_int
addend
,
mrb_int
*
sum
)
{
...
...
@@ -56,6 +97,8 @@ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
return
!!
(((
x
^
z
)
&
(
~
y
^
z
))
&
MRB_INT_OVERFLOW_MASK
);
}
#endif
#undef MRB_INT_OVERFLOW_MASK
#undef mrb_uint
#undef MRB_UINT_MAKE
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment