Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
loopy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ben Sepanski
loopy
Commits
e4ffd818
Commit
e4ffd818
authored
5 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Add C Integer semantics experiment
parent
19743eec
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
contrib/c-integer-semantics.py
+145
-0
145 additions, 0 deletions
contrib/c-integer-semantics.py
with
145 additions
and
0 deletions
contrib/c-integer-semantics.py
0 → 100644
+
145
−
0
View file @
e4ffd818
#!/usr/bin/env python
# coding: utf-8
from
os
import
system
import
ctypes
C_SRC
=
"""
#include <stdlib.h>
#include <stdint.h>
int64_t cdiv(int64_t a, int64_t b)
{
return a/b;
}
int64_t cmod(int64_t a, int64_t b)
{
return a%b;
}
#define LOOPY_CALL_WITH_INTEGER_TYPES(MACRO_NAME)
\
MACRO_NAME(short)
\
MACRO_NAME(int)
\
MACRO_NAME(long)
\
MACRO_NAME(int64_t)
#define LOOPY_DEFINE_FLOOR_DIV(TYPE)
\
TYPE loopy_floor_div_##TYPE(TYPE a, TYPE b)
\
{
\
if ((a<0) != (b<0))
\
a = a - (b + (b<0) - (b>=0));
\
return a/b;
\
}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_FLOOR_DIV)
#undef LOOPY_DEFINE_FLOOR_DIV
#define LOOPY_DEFINE_FLOOR_DIV_POS_B(TYPE)
\
TYPE loopy_floor_div_pos_b_##TYPE(TYPE a, TYPE b)
\
{
\
if (a<0)
\
a = a - (b-1);
\
return a/b;
\
}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_FLOOR_DIV_POS_B)
#undef LOOPY_DEFINE_FLOOR_DIV_POS_B
#define LOOPY_DEFINE_MOD_POS_B(TYPE)
\
TYPE loopy_mod_pos_b_##TYPE(TYPE a, TYPE b)
\
{
\
TYPE result = a%b;
\
if (result < 0)
\
result += b;
\
return result;
\
}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_MOD_POS_B)
#undef LOOPY_DEFINE_MOD_POS_B
#define LOOPY_DEFINE_MOD(TYPE)
\
TYPE loopy_mod_##TYPE(TYPE a, TYPE b)
\
{
\
TYPE result = a%b;
\
if (result < 0 && b > 0)
\
result += b;
\
if (result > 0 && b < 0)
\
result = result + b;
\
return result;
\
}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_MOD)
#undef LOOPY_DEFINE_MOD
"""
def
main
():
with
open
(
"
int-experiments.c
"
,
"
w
"
)
as
outf
:
outf
.
write
(
C_SRC
)
system
(
'
gcc -Wall -shared int-experiments.c -o int-experiments.so
'
)
int_exp
=
ctypes
.
CDLL
(
"
int-experiments.so
"
)
for
func
in
[
int_exp
.
cdiv
,
int_exp
.
cmod
,
int_exp
.
loopy_floor_div_int64_t
,
int_exp
.
loopy_floor_div_pos_b_int64_t
,
int_exp
.
loopy_mod_pos_b_int64_t
,
int_exp
.
loopy_mod_int64_t
,
]:
func
.
argtypes
=
[
ctypes
.
c_longlong
,
ctypes
.
c_longlong
]
func
.
restype
=
ctypes
.
c_longlong
cdiv
=
int_exp
.
cdiv
# noqa
cmod
=
int_exp
.
cmod
# noqa
int_floor_div
=
int_exp
.
loopy_floor_div_int64_t
int_floor_div_pos_b
=
int_exp
.
loopy_floor_div_pos_b_int64_t
int_mod_pos_b
=
int_exp
.
loopy_mod_pos_b_int64_t
int_mod
=
int_exp
.
loopy_mod_int64_t
m
=
50
for
a
in
range
(
-
m
,
m
):
for
b
in
range
(
1
,
m
):
cresult
=
int_floor_div_pos_b
(
a
,
b
)
presult
=
a
//
b
assert
cresult
==
presult
if
cresult
!=
presult
:
print
(
a
,
b
,
cresult
,
presult
)
for
a
in
range
(
-
m
,
m
):
for
b
in
range
(
-
m
,
m
):
if
b
==
0
:
continue
cresult
=
int_floor_div
(
a
,
b
)
presult
=
a
//
b
assert
cresult
==
presult
if
cresult
!=
presult
:
print
(
a
,
b
,
cresult
,
presult
)
for
a
in
range
(
-
m
,
m
):
for
b
in
range
(
1
,
m
):
cresult
=
int_mod_pos_b
(
a
,
b
)
presult
=
a
%
b
assert
cresult
==
presult
for
a
in
range
(
-
m
,
m
):
for
b
in
range
(
-
m
,
m
):
if
b
==
0
:
continue
cresult
=
int_mod
(
a
,
b
)
presult
=
a
%
b
assert
cresult
==
presult
if
cresult
!=
presult
:
print
(
a
,
b
,
cresult
,
presult
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment