#255: revive AT&T syntax, unfortunately cannot be combined with LTO for Clang

This commit is contained in:
Mamy Ratsimbazafy 2023-09-09 11:27:06 +02:00
parent 3ed57d3690
commit 121334be79
No known key found for this signature in database
GPG Key ID: 6227262F49BE273A
4 changed files with 2347 additions and 1123 deletions

View File

@ -7,6 +7,7 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import ./ct_types import ./ct_types
import ../isa/macro_assembler_x86
# ############################################################ # ############################################################
# #
@ -45,20 +46,47 @@ func ccopy_fallback[T](ctl: CTBool[T], x: var T, y: T) {.inline.}=
# x86 and x86-64 # x86 and x86-64
# ------------------------------------------------------------ # ------------------------------------------------------------
template mux_x86_impl() {.dirty.} = when UseAsmSyntaxIntel:
static: doAssert(X86) template mux_x86_impl() {.dirty.} =
static: doAssert(GCC_Compatible) static: doAssert(X86)
static: doAssert(GCC_Compatible)
var muxed = x var muxed = x
asm """ asm """
test %[ctl], %[ctl] test %[ctl], %[ctl]
cmovz %[muxed], %[y] cmovz %[muxed], %[y]
: [muxed] "+r" (`muxed`) : [muxed] "+r" (`muxed`)
: [ctl] "r" (`ctl`), [y] "r" (`y`) : [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc" : "cc"
""" """
muxed muxed
else:
template mux_x86_impl() {.dirty.} =
static: doAssert(X86)
static: doAssert(GCC_Compatible)
when sizeof(T) == 8:
var muxed = x
asm """
testq %[ctl], %[ctl]
cmovzq %[y], %[muxed]
: [muxed] "+r" (`muxed`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
muxed
elif sizeof(T) == 4:
var muxed = x
asm """
testl %[ctl], %[ctl]
cmovzl %[y], %[muxed]
: [muxed] "+r" (`muxed`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
muxed
else:
{.error: "Unsupported word size".}
func mux_x86[T](ctl: CTBool[T], x, y: T): T {.inline.}= func mux_x86[T](ctl: CTBool[T], x, y: T): T {.inline.}=
## Multiplexer / selector ## Multiplexer / selector
@ -74,29 +102,73 @@ func mux_x86[T: CTBool](ctl: CTBool, x, y: T): T {.inline.}=
## So equivalent to ctl? x: y ## So equivalent to ctl? x: y
mux_x86_impl() mux_x86_impl()
func ccopy_x86[T](ctl: CTBool[T], x: var T, y: T) {.inline.}= when UseAsmSyntaxIntel:
## Conditional copy func ccopy_x86[T](ctl: CTBool[T], x: var T, y: T) {.inline.}=
## Copy ``y`` into ``x`` if ``ctl`` is true ## Conditional copy
static: doAssert(X86) ## Copy ``y`` into ``x`` if ``ctl`` is true
static: doAssert(GCC_Compatible) static: doAssert(X86)
static: doAssert(GCC_Compatible)
when defined(cpp): when defined(cpp):
asm """ asm """
test %[ctl], %[ctl] test %[ctl], %[ctl]
cmovnz %[x], %[y] cmovnz %[x], %[y]
: [x] "+r" (`x`) : [x] "+r" (`x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`) : [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc" : "cc"
""" """
else: else:
asm """ asm """
test %[ctl], %[ctl] test %[ctl], %[ctl]
cmovnz %[x], %[y] cmovnz %[x], %[y]
: [x] "+r" (`*x`) : [x] "+r" (`*x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`) : [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc" : "cc"
""" """
else:
func ccopy_x86[T](ctl: CTBool[T], x: var T, y: T) {.inline.}=
## Conditional copy
## Copy ``y`` into ``x`` if ``ctl`` is true
static: doAssert(X86)
static: doAssert(GCC_Compatible)
when sizeof(T) == 8:
when defined(cpp):
asm """
testq %[ctl], %[ctl]
cmovnzq %[y], %[x]
: [x] "+r" (`x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
else:
asm """
testq %[ctl], %[ctl]
cmovnzq %[y], %[x]
: [x] "+r" (`*x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
elif sizeof(T) == 4:
when defined(cpp):
asm """
testl %[ctl], %[ctl]
cmovnzl %[y], %[x]
: [x] "+r" (`x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
else:
asm """
testl %[ctl], %[ctl]
cmovnzl %[y], %[x]
: [x] "+r" (`*x`)
: [ctl] "r" (`ctl`), [y] "r" (`y`)
: "cc"
"""
else:
{.error: "Unsupported word size".}
# Public functions # Public functions
# ------------------------------------------------------------ # ------------------------------------------------------------

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff