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
5933651a
Commit
5933651a
authored
May 11, 2016
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3157 from cremno/add-mrb_int_mul_overflow
add function for checked mrb_int multiplication
parents
0be4b896
7453a5df
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
28 deletions
+45
-28
include/mruby/numeric.h
include/mruby/numeric.h
+35
-0
src/numeric.c
src/numeric.c
+4
-12
src/vm.c
src/vm.c
+6
-16
No files found.
include/mruby/numeric.h
View file @
5933651a
...
...
@@ -62,6 +62,12 @@ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
return
__builtin_sub_overflow
(
minuend
,
subtrahend
,
difference
)
||
WBCHK
(
*
difference
);
}
static
inline
mrb_bool
mrb_int_mul_overflow
(
mrb_int
multiplier
,
mrb_int
multiplicand
,
mrb_int
*
product
)
{
return
__builtin_mul_overflow
(
multiplier
,
multiplicand
,
product
)
||
WBCHK
(
*
product
);
}
#undef WBCHK
#else
...
...
@@ -92,6 +98,35 @@ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
return
!!
(((
x
^
z
)
&
(
~
y
^
z
))
&
MRB_INT_OVERFLOW_MASK
);
}
static
inline
mrb_bool
mrb_int_mul_overflow
(
mrb_int
multiplier
,
mrb_int
multiplicand
,
mrb_int
*
product
)
{
#if MRB_INT_BIT == 32
int64_t
n
=
(
int64_t
)
multiplier
*
multiplicand
;
*
product
=
(
mrb_int
)
n
;
return
!
FIXABLE
(
n
);
#else
if
(
multiplier
>
0
)
{
if
(
multiplicand
>
0
)
{
if
(
multiplier
>
MRB_INT_MAX
/
multiplicand
)
return
TRUE
;
}
else
{
if
(
multiplicand
<
MRB_INT_MAX
/
multiplier
)
return
TRUE
;
}
}
else
{
if
(
multiplicand
>
0
)
{
if
(
multiplier
<
MRB_INT_MAX
/
multiplicand
)
return
TRUE
;
}
else
{
if
(
multiplier
!=
0
&&
multiplicand
<
MRB_INT_MAX
/
multiplier
)
return
TRUE
;
}
}
*
product
=
multiplier
*
multiplicand
;
return
FALSE
;
#endif
}
#undef MRB_INT_OVERFLOW_MASK
#undef mrb_uint
#undef MRB_UINT_MAKE
...
...
src/numeric.c
View file @
5933651a
...
...
@@ -541,10 +541,6 @@ int_to_i(mrb_state *mrb, mrb_value num)
return
num
;
}
/*tests if N*N would overflow*/
#define SQRT_INT_MAX ((mrb_int)1<<((MRB_INT_BIT-1-MRB_FIXNUM_SHIFT)/2))
#define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
mrb_value
mrb_fixnum_mul
(
mrb_state
*
mrb
,
mrb_value
x
,
mrb_value
y
)
{
...
...
@@ -552,18 +548,14 @@ mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y)
a
=
mrb_fixnum
(
x
);
if
(
mrb_fixnum_p
(
y
))
{
mrb_float
c
;
mrb_int
b
;
mrb_int
b
,
c
;
if
(
a
==
0
)
return
x
;
b
=
mrb_fixnum
(
y
);
if
(
FIT_SQRT_INT
(
a
)
&&
FIT_SQRT_INT
(
b
))
return
mrb_fixnum_value
(
a
*
b
);
c
=
a
*
b
;
if
((
a
!=
0
&&
c
/
a
!=
b
)
||
!
FIXABLE
(
c
))
{
return
mrb_float_value
(
mrb
,
(
mrb_float
)
a
*
(
mrb_float
)
b
);
if
(
mrb_int_mul_overflow
(
a
,
b
,
&
c
))
{
return
mrb_float_value
(
mrb
,
(
mrb_float
)
a
*
(
mrb_float
)
b
);
}
return
mrb_fixnum_value
(
(
mrb_int
)
c
);
return
mrb_fixnum_value
(
c
);
}
return
mrb_float_value
(
mrb
,
(
mrb_float
)
a
*
mrb_to_flo
(
mrb
,
y
));
}
...
...
src/vm.c
View file @
5933651a
...
...
@@ -1862,25 +1862,15 @@ RETRY_TRY_BLOCK:
switch
(
TYPES2
(
mrb_type
(
regs
[
a
]),
mrb_type
(
regs
[
a
+
1
])))
{
case
TYPES2
(
MRB_TT_FIXNUM
,
MRB_TT_FIXNUM
):
{
mrb_value
z
;
z
=
mrb_fixnum_mul
(
mrb
,
regs
[
a
],
regs
[
a
+
1
]);
mrb_int
x
,
y
,
z
;
switch
(
mrb_type
(
z
))
{
case
MRB_TT_FIXNUM
:
{
SET_INT_VALUE
(
regs
[
a
],
mrb_fixnum
(
z
));
}
break
;
case
MRB_TT_FLOAT
:
{
SET_FLOAT_VALUE
(
mrb
,
regs
[
a
],
mrb_float
(
z
));
}
break
;
default:
/* cannot happen */
x
=
mrb_fixnum
(
regs
[
a
]);
y
=
mrb_fixnum
(
regs
[
a
+
1
]);
if
(
mrb_int_mul_overflow
(
x
,
y
,
&
z
))
{
SET_FLOAT_VALUE
(
mrb
,
regs
[
a
],
(
mrb_float
)
x
*
(
mrb_float
)
y
);
break
;
}
SET_INT_VALUE
(
regs
[
a
],
z
);
}
break
;
case
TYPES2
(
MRB_TT_FIXNUM
,
MRB_TT_FLOAT
):
...
...
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