[Spice-devel] [PATCH spice-common] codegen: Make the compiler work about better way to write unaligned memory
Frediano Ziglio
fziglio at redhat.com
Mon Sep 11 10:13:04 UTC 2017
Instead of assuming that the system can safely do unaligned access
to memory use packed structures to allow the compiler generate
best code possible.
For instance ARM7 can use unaligned access but not for 64 bit
numbers (currently these accesses are emulated by Linux kernel
with obvious performance consequences).
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
python_modules/demarshal.py | 33 +++++++++++++++++++++++----------
1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/python_modules/demarshal.py b/python_modules/demarshal.py
index 1ea131d..3d94b71 100644
--- a/python_modules/demarshal.py
+++ b/python_modules/demarshal.py
@@ -40,24 +40,37 @@ def write_parser_helpers(writer):
writer = writer.function_helper()
+ writer.writeln("#include <spice/start-packed.h>")
+ for size in [16, 32, 64]:
+ for sign in ["", "u"]:
+ type = "%sint%d" % (sign, size)
+ writer.begin_block("typedef struct")
+ writer.variable_def("%s_t" % type, "v")
+ writer.end_block(newline=False)
+ writer.writeln(" *%s_unaligned_p;" % type)
+ writer.writeln("#include <spice/end-packed.h>")
+ writer.newline()
+
+ for sign in ["", "u"]:
+ type = "%sint8" % sign
+ writer.macro("read_%s" % type, "ptr", "(*((%s_t *)(ptr)))" % type)
+ writer.macro("write_%s" % type, "ptr, val", "*(%s_t *)(ptr) = val" % (type))
+ writer.newline()
+
writer.writeln("#ifdef WORDS_BIGENDIAN")
- for size in [8, 16, 32, 64]:
+ for size in [16, 32, 64]:
for sign in ["", "u"]:
utype = "uint%d" % (size)
type = "%sint%d" % (sign, size)
swap = "SPICE_BYTESWAP%d" % size
- if size == 8:
- writer.macro("read_%s" % type, "ptr", "(*((%s_t *)(ptr)))" % type)
- writer.macro("write_%s" % type, "ptr, val", "*(%s_t *)(ptr) = val" % (type))
- else:
- writer.macro("read_%s" % type, "ptr", "((%s_t)%s(*((%s_t *)(ptr))))" % (type, swap, utype))
- writer.macro("write_%s" % type, "ptr, val", "*(%s_t *)(ptr) = %s((%s_t)val)" % (utype, swap, utype))
+ writer.macro("read_%s" % type, "ptr", "((%s_t)%s(((%s_unaligned_p)(ptr))->v))" % (type, swap, utype))
+ writer.macro("write_%s" % type, "ptr, val", "((%s_unaligned_p)(ptr))->v = %s((%s_t)val)" % (utype, swap, utype))
writer.writeln("#else")
- for size in [8, 16, 32, 64]:
+ for size in [16, 32, 64]:
for sign in ["", "u"]:
type = "%sint%d" % (sign, size)
- writer.macro("read_%s" % type, "ptr", "(*((%s_t *)(ptr)))" % type)
- writer.macro("write_%s" % type, "ptr, val", "(*((%s_t *)(ptr))) = val" % type)
+ writer.macro("read_%s" % type, "ptr", "(((%s_unaligned_p)(ptr))->v)" % type)
+ writer.macro("write_%s" % type, "ptr, val", "(((%s_unaligned_p)(ptr))->v) = val" % type)
writer.writeln("#endif")
for size in [8, 16, 32, 64]:
--
2.13.5
More information about the Spice-devel
mailing list