compiler: implement @divCeil builtin #36043
Labels
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
No milestone
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig!36043
Loading…
Reference in a new issue
No description provided.
Delete branch "pavelverigo/zig:div-ceil"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Third time's the charm.
pull/16807
pull/21757
Added co-author from the previous pull requests.
I'll wait for CI to pass before updatingstage1.wasmand the compiler.One controversial thing I decided to add is
expand_div_ceilin legalize to supportx86_64. There was no existing logic (like for@divFloor), so this was implemented from scratch. In the future it'll probably either be removed or kept if it proves useful for other backends.Closes github/16022
229257bf4a83828bb46183828bb461a8e05b8cc3Nice work
@pavelverigo wrote in #36043 (comment):
Do you need to update stage1.wasm? In general it's better to avoid doing it where possible.
@andrewrk wrote in #36043 (comment):
I want to use
@divCeilinside the compiler (Sema/arith.zig) + I am planning to updatemath.divCeilit may be used in compiler already.I suggest to also handle this TODO from
src/main.zig:I previosly suggeted this here: #30875 (comment), but I think this PR will be merged way sooner.
a8e05b8cc300fd7090ef@K4 Done.
00fd7090efd17bd29726d17bd29726485f45b1c7I'm less worried about the non-optimizing backends as long as they pass tests, including:
@ -3581,0 +3629,4 @@const rhs_sign = try self.wip.bin(.@"and", rhs, smin, "divCeil.rhs_sign");const rem_xor_rhs_sign = try self.wip.bin(.xor, rem, rhs_sign, "divCeil.rem_xor_rhs_sign");const same_sign = try self.wip.icmp(.ult, rem_xor_rhs_sign, smin, "divCeil.same_sign");Please don't use this complicated expression if you aren't going to fold in the comparison of
remwith zero, why are you clearing unobserved bits inrhs, why are you using an unsigned comparison? I believe just using.sgt, rem_xor_rhs_sign, zerohere properly accounts for therem_nonzerocheck.I arrived at this by modifying the
divFloorlowering above and mechanically applying therem != 0 and same_signlogic to it, without fully thinking through thesmintrick. You right,.sgtwill indeed be equivalent to that 3 lines. For unsigned comparison point again that was fromdivFloor.I will push new version in coming hours.
packedkeyword tobitpack#30875485f45b1c7b4c4939043I rebuild
stage1.wasmagain, as compiler commit changed.Also missing
lib/std/math/big/int_test.zigtests.@ -1224,0 +1283,4 @@// => -b * @divTrunc(-a, -b) - b + modCeil(-a, -b) = -a// => modCeil(-a, -b) = @rem(-a, -b) + bq.addScalar(q.toConst(), 1);r.add(r.toConst(), y.toConst().abs());Is this not equivalent to
r.sub(r.toConst(), y.toConst());from the previous case? Handling these two cases differently just makes it look like the math must be wrong.Ok, I merged those. Looking at
divFloorimpl above I can guess, author of this was following same style.@andrewrk I will merge, when it CI is fully green. You did not reply, but are you ok with
stage1.wasmupdate?Yes, sorry it was not meant to be a merge blocking question.
Don't forget to type up release notes for this language change please 🙏
packedkeyword tobitpack#30875Release notes
@divCeilThe new
@divCeilbuiltin performs integer division rounded toward positive infinity, complementing the existing@divTrunc,@divFloor, and@divExactbuiltins.As with the other division builtins, caller guarantees that
denominator != 0and that result does not overflow.No more
std.math.divCeil(a, b) catch unreachable!