Division by 0 - TMS5704357 CortexR5 - Page 1
Forum section: electronics / general

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 04:43:18 pm
content: Hello I was verifying the activation of the exception in case of division by zero. currently on my CCS project I have deleted all libraries. I do a simple a = 1/0. I get the warning but then in the end the exception does not activate and if I go to debug and I see the value of the variable I see that inside it I have the value zero; honestly I was expecting "infinite". I am wrong ? further thing. I saw that there are some functions contained in the libc, which I have excluded, such as u_div0, but having not included them which division function is my project using? meanwhile, I limit myself to these questions. thank you

author: JPortici
author_rank: Super Contributor
timestamp: March 15, 2022, 04:49:58 pm
content: Cool, a thread about a Cortex R for a change.Post your actual code, because i think that if your statement is actually 1/0 it will get optimized at compile time and not perform the division

author: Siwastaja
author_rank: Super Contributor
timestamp: March 15, 2022, 05:00:53 pm
content: Try withvolatile int a = 1;volatile int b = 0;int result = a/b;to prevent optimization of the constant values.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 05:30:38 pm
content: HiMy code isint a=1;Int mani(){a=a/1;}To enable undefined exception i enabled DZ bit of SCTLR register.If i’m not using library libc, in which there are all files div.asm, how the System can perform this operation ?If i don’t have that files .asm Is it possible that the exception is not shown?Thank you

author: Siwastaja
author_rank: Super Contributor
timestamp: March 15, 2022, 05:43:09 pm
content: The code divides by 1, not zero, and I'm sure you don't have type "Int" defined, with function mani(), which fails to return said Int.Show theactualcode, including the division by zero, but also you enabling DZ in SCTLR.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 05:54:41 pm
content: sorry for the mistakes but i'm typing from the phone.int a=1;Int mani(){a=a/0;}the variable is of type int I assure you. the code to enable DZ is in assembler, and i see the instruction on the manual of R5.

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 15, 2022, 06:08:18 pm
content: As exposed above, there is a big chance this expression is just getting optimized out as it is.But in any case, the compiler should issue a warning here. GCC certainly does, even without -Wall.Code:warning: division by zero [-Wdiv-by-zero]So, I did a little experiment with GCC.- It does issue the above warning. Read compiler warnings. (Maybe you just didn't care about the warning as you were looking for getting an exception anyway.)- With no optimization enabled or up to -O1, it does emit the division instruction. So that should raise an exception if such exceptions are enabled on the CPU.- As I suspected, with optimizations enabled (beyond -O1 with GCC 11.2, not tried with earlier versions), it does optimize out the division, so no code for the division is actually emitted. That would explain there is no exception raised at runtime.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 06:10:28 pm
content: If i’m not using library libc, in which there are all files div.asm, how the System can perform this operation ?If i don’t have that files .asm Is it possible that the exception is not shown?Can you help me to understand the se two questions?Another thing:also in one of the div.asm files I saw all the function that goes to check if the divider is zero. so if I don't include this function, why I remove the libc library, how does Code Composer handle this operation? thank you

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 06:31:59 pm
content: Hi and thanks for you reply.Yes i have the same warning message.So in practice from a version onwards of GCC if optimization is enabled does not trigger the exception?Quote from: lorenrus on March 15, 2022, 06:10:28 pmCan you please answer me also to these doubts? Thank you

author: gf
author_rank: Super Contributor
timestamp: March 15, 2022, 06:33:07 pm
content: Integer division by zero is undefined behavior in C. So any outcome is "correct". It can raise an exception, but alternatively it can also return an arbitrary value.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 06:38:44 pm
content: Quote from: JPortici on March 15, 2022, 04:49:58 pmSo sorry when you say that it is optimized in practice what happens? Thank you

author: lorenrus
author_rank: Regular Contributor
timestamp: March 15, 2022, 06:40:50 pm
content: Quote from: gf on March 15, 2022, 06:33:07 pmYes i agree, in fact I expected the function to be activated excpetion undefined function.

author: gf
author_rank: Super Contributor
timestamp: March 15, 2022, 06:50:33 pm
content: Quote from: lorenrus on March 15, 2022, 06:40:50 pmIf you want defined behavior then you need to check the divisor explicitly. The object code generated by the compiler is free to do anything in this case. It can raise an exception, but it can also silently return the value 42 as result of a/0.Undefined Behavior means exactly that: You can't expect any particular outcome.

author: ve7xen
author_rank: Super Contributor
timestamp: March 15, 2022, 06:54:07 pm
content: The compiler is generally responsible for providing software implementations of standard C operations that are not supported by the CPU, either inline or via a library (libgcc in GCC's case). But they will only be used if there is no hardware support (or if you specifically request them), and I believe ARMv8 should include hardware integer divide, so I would expect it to emit the divide instruction rather than call library code. It is possible that in your case GCC recognizes that this divide will always be divide by zero, and does whatever GCC does for the divide-by-zero case instead of emitting anything to the CPU for it.In C99, integer division by zero is UB, so if the compiler can prove this, it can do 'whatever it wants', including crash, emit an exception, emit the divide instruction to the CPU, assume a certain result...whatever.Please post the exact code you are asking about, and we can analyze it. Most likely you have either not marked the variables volatile, so the compiler can infer the result at compile-time and doesn't emit any instructions, or it can prove that it's always division-by-zero, and does whatever it does in the divide by zero case (whatever it wants). I believe GCC will infer 0 as the output if it can prove divide by zero, but it's UB so... YMMV.https://gcc.godbolt.org/is a good tool here. Please post your code...

author: eutectique
author_rank: Frequent Contributor
timestamp: March 15, 2022, 09:51:30 pm
content: When you step through assembly instructions, what do you see? What are the values of the registers?

author: rsjsouza
author_rank: Super Contributor
timestamp: March 15, 2022, 10:14:37 pm
content: I did a quick test and, using the TI ARM compiler, the "divide by zero" results in the maximum value. I am using an R4 core (TMS570LS0432 Launchpad)

author: JPortici
author_rank: Super Contributor
timestamp: March 15, 2022, 10:48:47 pm
content: Quote from: lorenrus on March 15, 2022, 05:54:41 pmAs a general rule, don't.Take your time, but give us correct information on each post because if we have to guess what your intended to write we may not want to bother looking at the thread anymore.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 16, 2022, 07:21:16 am
content: Quote from: rsjsouza on March 15, 2022, 10:14:37 pmbut the exception activates you? however, optimization is disabled.

author: hans
author_rank: Super Contributor
timestamp: March 16, 2022, 03:15:28 pm
content: Quote from: ve7xen on March 15, 2022, 06:54:07 pmI think the C99 spec says the behaviour is undefined, because it can vary on the platform the code is running on it.Some platforms may generate exceptions, traps, while others may simply ignore the fault and write a zero to the output register. Obv a good compiler with static analyzer will complain (and preferably and warning is escalated to a compilation error), but in terms of hardware behaviour of the sdiv/udiv instruction (or library).. it depends.On ARMv8, a zero divisor will not cause a hardware exception/trap and will move 'zero' into the result register:https://developer.arm.com/documentation/den0024/a/The-A64-instruction-set/Data-processing-instructions/Multiply-and-divide-instructions?lang=enOn ARMv7-R the DZ bit controls whether an exception is generated, or zero is moved into result register:https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/divide-and-conquerThe -A and -M variants have different behaviour.However, if the CPU does not contain hardware division instructions, then the arm library is responsible for divide-by-zero behaviour. There is a idiv0 function that can be (re)defined for this behaviour:https://developer.arm.com/documentation/dui0773/e/Coding-Considerations/Integer-division-by-zero-errors-in-C-code?lang=enSo in that sense the divide-by-zero behaviour, in C99 dialectal terms, is undefined.. because it can change with the system/hardware environment.Maybe the TI compiler does something different than the standard ARM GCC compiler, or Clang, or IAR.. don't know. Perhaps something to investigate.(I also agree with JPortici, if this question isn't a life-death emergency I'd rather respond to clear answers than many confusing bits of mysterious information)

author: tggzzz
author_rank: Super Contributor
timestamp: March 16, 2022, 03:51:43 pm
content: Quote from: lorenrus on March 15, 2022, 06:38:44 pmYou can't complain (about incorrect operation) if demons fly out of your nose or the compiler deletes your filesystem.https://www.urbandictionary.com/define.php?term=nasal%20demonshttps://accu.org/journals/overload/21/115/maudel_1857/Isn't C wonderful.

author: TomS_
author_rank: Frequent Contributor
timestamp: March 16, 2022, 05:04:23 pm
content: Quote from: lorenrus on March 15, 2022, 06:38:44 pmPut simply:The compiler analyses the code before translating it into instructions.It finds that it knows what the value ofais (1), and that you are trying to divide it by 0 - based on this it can assign the result at compile time rather than generating instructions to force the CPU to go through the process for you (thats one kind of optimisation).But it also knows you arent doing anything with the result of the division either (not returning it, not feeding it into anything else etc), so it just doesnt bother generating any instructions at all for what you have asked, therefore no code is ever actually executed on the CPU to generate an exception (another kind of optimisation).If you want to force the CPU to perform the division then do as suggested by Siwastaja (makeavolatile), then the compiler cant make any such assumptions and will likely generate instructions to perform the division which could then lead to an exception.

author: brucehoult
author_rank: Super Contributor
timestamp: March 17, 2022, 12:04:19 am
content: Quote from: hans on March 16, 2022, 03:15:28 pmIn comparison, RISC-V gives all bits 1s for division by 0 (regardless of whether the division is signed or unsigned).Provided the division is not returning the result to the same register that contains the divisor, if you want to change control flow on a division by zero you can just add a single instruction (BNEZ, a 2 byte opcode if the branch is less than ±256 bytes and the register is in a0-a5,s0-s1) *after* the division instruction, which will of course execute while the division is in progress.A64 also has a BNEZ instruction, so can do the same.

author: lorenrus
author_rank: Regular Contributor
timestamp: March 17, 2022, 07:04:27 pm
content: Quote from: hans on March 16, 2022, 03:15:28 pmThank youWhat does it mean "only r1", in the image ?Thank you

author: eutectique
author_rank: Frequent Contributor
timestamp: March 17, 2022, 07:24:31 pm
content: Quote from: lorenrus on March 17, 2022, 07:04:27 pmCortex-R5 release 1, perhaps?