<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: No exception when divided by zero. in i.MX Processors</title>
    <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761971#M216190</link>
    <description>&lt;P&gt;This again may be optimized out to avoid /0 and use compile time constants. Try making your "a" global scope and volatile, so that -=1 and further /=0 can't be optimized out.&lt;/P&gt;</description>
    <pubDate>Wed, 22 Nov 2023 14:08:23 GMT</pubDate>
    <dc:creator>kef2</dc:creator>
    <dc:date>2023-11-22T14:08:23Z</dc:date>
    <item>
      <title>No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1760718#M216126</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;I am working on imx8mmevk, with linux image built from Yocto. And I found that there will be no exception raised when a number divide zero at runtime. Below is an example in C:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;int a=2/0;
float b=1.2/0;

// the result is: a=0, b=inf
printf("a=%d, b=%f", a, b);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;As I know, registers FPCR (floating-point control register) with DZE bit and FPSR (floating-point status register) with DZC bit were designed to handle the situation for armv8. But referring to reference documents from &lt;A href="https://www.nxp.com/products/processors-and-microcontrollers/arm-processors/i-mx-applications-processors/i-mx-8-applications-processors/i-mx-8m-mini-arm-cortex-a53-cortex-m4-audio-voice-video:i.MX8MMINI#documentation" target="_blank" rel="noopener"&gt;i.MX 8M Mini | Arm Cortex A53 | Cortex M4 | NXP Semiconductors&lt;/A&gt;,&amp;nbsp; I found that there is no DZE bit in a53, and it seems no way to get the exception.&lt;/P&gt;&lt;P&gt;I don't know if I got some mistakes, and I wonder how to handle the problem.&lt;/P&gt;&lt;P&gt;Could someone give me a hand?&lt;/P&gt;&lt;P&gt;=============================================&lt;/P&gt;&lt;P&gt;Edit at 2023.11.24.&lt;/P&gt;&lt;P&gt;Further ask.&lt;/P&gt;&lt;P&gt;Can I write my own ISR to handle division by zero (especially for floating-point)? May someone could give me advise.&lt;/P&gt;&lt;P&gt;=============================================&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;fffengzia&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Nov 2023 07:08:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1760718#M216126</guid>
      <dc:creator>fffengzia</dc:creator>
      <dc:date>2023-11-24T07:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761947#M216183</link>
      <description>&lt;P&gt;Have you tried to write a kernel module to test?&amp;nbsp;&lt;SPAN&gt;It is possible that the compiler will handle it if write it this way.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Harvey&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 13:20:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761947#M216183</guid>
      <dc:creator>Harvey021</dc:creator>
      <dc:date>2023-11-22T13:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761949#M216184</link>
      <description>&lt;PRE&gt;int a=2/0;
float b=1.2/0;&lt;/PRE&gt;&lt;P&gt;These ^^ are compile time constants in most of compilers, which just can't raise any exception at run time. Did your compiler emit any warnings?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 13:31:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761949#M216184</guid>
      <dc:creator>kef2</dc:creator>
      <dc:date>2023-11-22T13:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761957#M216188</link>
      <description>&lt;P&gt;Maybe I haven't expressed my situation clearly. There is nothing to do with compiler because what I need is to get an exception at runtime.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I wrote it "1/0" or "1.2/0"&amp;nbsp;&lt;SPAN&gt;explicitly, the compile does through an error or warning. But what if the zero is a variable, and it becomes 0 accidently (or carelessly)?&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$ cat main.c
#include &amp;lt;stdio.h&amp;gt;

int main(int argc, char* argv[])
{
        int a = 1, b = 1;
        float c = 1.2;

        int iRes;
        float fRes;


        // Accidently or Carelessly
        a -= 1;


        iRes = b/a;
        fRes = c/a;

        printf("1/0=%d, 1.2/0=%f\n", iRes, fRes);

        return 0;
}
$ gcc -Wall main.c #No warning or error here
$ ./a.out
1/0=0, 1.2/0=inf&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 13:46:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761957#M216188</guid>
      <dc:creator>fffengzia</dc:creator>
      <dc:date>2023-11-22T13:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761964#M216189</link>
      <description>&lt;P&gt;I am sorry that I haven't express it clearly.&lt;/P&gt;&lt;P&gt;I need the exception at runtime, for the real situation is that the dividend is a variable and it becomes 0 accidently and then got into calculation.&lt;/P&gt;&lt;P&gt;I wrote an example code in my reply to &lt;SPAN&gt;Harvey, may that would explain my problem clearly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;fffengzia&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 13:56:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761964#M216189</guid>
      <dc:creator>fffengzia</dc:creator>
      <dc:date>2023-11-22T13:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761971#M216190</link>
      <description>&lt;P&gt;This again may be optimized out to avoid /0 and use compile time constants. Try making your "a" global scope and volatile, so that -=1 and further /=0 can't be optimized out.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 14:08:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1761971#M216190</guid>
      <dc:creator>kef2</dc:creator>
      <dc:date>2023-11-22T14:08:23Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762495#M216250</link>
      <description>&lt;P&gt;&lt;BR /&gt;Arm® Architecture Reference Manual&lt;BR /&gt;Armv8, for A-profile architecture&lt;BR /&gt;Copyright © 2013-2021 Arm Limited or its affiliates. All rights reserved.&lt;BR /&gt;ARM DDI 0487G.b (ID072021)&lt;/P&gt;&lt;P&gt;No indication of this overflow is produced&lt;/P&gt;&lt;P&gt;A division by zero results in a zero being written to the destination register, without any indication that the division by zero occurred.&lt;/P&gt;&lt;P&gt;(that is why you find a = 0)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ssssss.png" style="width: 822px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/251236i8883FDB095A15A23/image-size/large?v=v2&amp;amp;px=999" role="button" title="ssssss.png" alt="ssssss.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Please search fdiv to find information. floating divide has too many informati1, can not paste everything here.&lt;BR /&gt;It is also related to IEEE 754&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;a=0, b=inf inf=infinity&lt;/P&gt;&lt;P&gt;When you compile, compiler will give warning. You can change warning as error to stop compiling.&lt;BR /&gt;aarch64-linux-gnu-gcc -static -o div0 div0.c&lt;BR /&gt;div0.c: In function ‘main’:&lt;BR /&gt;div0.c:7:9: warning: division by zero [-Wdiv-by-zero]&lt;BR /&gt;int a=2/0;&lt;BR /&gt;^&lt;BR /&gt;div0.c:8:13: warning: division by zero [-Wdiv-by-zero]&lt;BR /&gt;float b=1.2/0;&lt;BR /&gt;^&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the assembly code for&lt;BR /&gt;int a=2/0;&lt;BR /&gt;float b=1.2/0;&lt;/P&gt;&lt;P&gt;.arch armv8-a&lt;BR /&gt;.file "div0.c"&lt;BR /&gt;.text&lt;BR /&gt;.section .rodata&lt;BR /&gt;.align 3&lt;BR /&gt;.LC0:&lt;BR /&gt;.string "a=%d, b=%f"&lt;BR /&gt;.text&lt;BR /&gt;.align 2&lt;BR /&gt;.global main&lt;BR /&gt;.type main, %function&lt;BR /&gt;main:&lt;BR /&gt;stp x29, x30, [sp, -48]!&lt;BR /&gt;add x29, sp, 0&lt;BR /&gt;str w0, [x29, 28]&lt;BR /&gt;str x1, [x29, 16]&lt;BR /&gt;mov w0, 2&lt;BR /&gt;mov w1, 0&lt;BR /&gt;sdiv w0, w0, w1&lt;BR /&gt;str w0, [x29, 44]&lt;BR /&gt;movi d0, #0&lt;BR /&gt;mov x0, 3689348814741910323&lt;BR /&gt;movk x0, 0x3ff3, lsl 48&lt;BR /&gt;fmov d1, x0&lt;BR /&gt;fdiv d0, d1, d0&lt;BR /&gt;fcvt s0, d0&lt;BR /&gt;str s0, [x29, 40]&lt;BR /&gt;ldr s0, [x29, 40]&lt;BR /&gt;fcvt d0, s0&lt;BR /&gt;adrp x0, .LC0&lt;BR /&gt;add x0, x0, :lo12:.LC0&lt;BR /&gt;ldr w1, [x29, 44]&lt;BR /&gt;bl printf&lt;BR /&gt;mov w0, 0&lt;BR /&gt;ldp x29, x30, [sp], 48&lt;BR /&gt;ret&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 08:22:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762495#M216250</guid>
      <dc:creator>kimease</dc:creator>
      <dc:date>2023-11-23T08:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762500#M216251</link>
      <description>&lt;P&gt;Harvey,&lt;/P&gt;&lt;P&gt;Please note!&lt;/P&gt;&lt;P&gt;It is a question about armv8 instruction.&lt;/P&gt;&lt;P&gt;Related no to any OS.&lt;/P&gt;&lt;P&gt;Far away from kernel.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 08:24:48 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762500#M216251</guid>
      <dc:creator>kimease</dc:creator>
      <dc:date>2023-11-23T08:24:48Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762588#M216258</link>
      <description>&lt;P&gt;I am so glad that you got my point :-).&lt;/P&gt;&lt;P&gt;The problem of integer division by zero could get its answer now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;From the Architecture Reference Manual:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="fdiv.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/251241iD372B1B9102596D0/image-size/large?v=v2&amp;amp;px=999" role="button" title="fdiv.png" alt="fdiv.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And from A1.5.7:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="dze.png" style="width: 999px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/251244i98AAC11B25ABB6D5/image-size/large?v=v2&amp;amp;px=999" role="button" title="dze.png" alt="dze.png" /&gt;&lt;/span&gt;&amp;nbsp;About the FPCR:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="FPCR.png" style="width: 996px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/251247iC7492531D688370C/image-size/large?v=v2&amp;amp;px=999" role="button" title="FPCR.png" alt="FPCR.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;&lt;SPAN&gt;However, it seems that this register is not forced to be used in every armv8 core, at least for a53:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;ARM® Cortex®-A53 MPCore Processor&lt;BR /&gt;Advanced SIMD and Floating-point&lt;/P&gt;&lt;P&gt;Copyright © 2013-2016 ARM. All rights reserved.&lt;BR /&gt;ARM DDI 0502G (ID041316)&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="a53fpcr.png" style="width: 900px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/251250i2EFC51AFBB48666A/image-size/large?v=v2&amp;amp;px=999" role="button" title="a53fpcr.png" alt="a53fpcr.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But not all a53 cores don't raise exception if floating-point division by zero. I have ever used another board from other manufacturer, which raise an exception if floating-point division happens. And I need to know if the a53 on imx8mmevk does able or not able to act like it.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have referred to documents about imx8mmevk and could not found some useful.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 09:36:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1762588#M216258</guid>
      <dc:creator>fffengzia</dc:creator>
      <dc:date>2023-11-23T09:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1793764#M219074</link>
      <description>&lt;P&gt;As far as I can tell, no ARM cores (not even the Cortex-X1) support trapping floating-point exceptions. Actually, in most systems (CPU:s and operating systems) trapping floating-point exceptions are either turned off by default or not supported at all, usually for good reasons (floating-point exceptions hurt performance, are largely misunderstood and are usually unnecessary).&lt;/P&gt;&lt;P&gt;I recommend that you find other ways of dealing with the problem.&lt;/P&gt;&lt;P&gt;First of all, it should be noted that division by zero is a well defined operation in IEEE 754 floating-point arithmetic. It returns +/-infinity, which is a valid input to other operations (e.g. 42/infinity = 0, 42+infinity=infinity, and so on). In fact, many algorithms actually work out perfectly fine even when division by zero is involved.&lt;/P&gt;&lt;P&gt;Second, if you really need to prevent division by zero in your code (be sure to figure out &lt;EM&gt;why&lt;/EM&gt; it is not allowed), the canonical way to do that is to check the value of the denominator before the division, and handle it appropriately (e.g. throw a C++ exception if the denominator is zero). Another way is to use isfinite() to check the result of the division.&lt;/P&gt;&lt;P&gt;Also, as others have pointed out your examples will be calculated at compile time. In order to really trigger division by zero at runtime, you need to put the division in a function in a separate compilation unit (e.g. main() in main.c calls divide() in division.c), so that the compiler does not know the input values to the division at compile time.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2024 11:05:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1793764#M219074</guid>
      <dc:creator>mbitsnbites</dc:creator>
      <dc:date>2024-01-23T11:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: No exception when divided by zero.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1795187#M219187</link>
      <description>&lt;P&gt;Some ARM's indeed do support FPU exceptions.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;FPSCR&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Bits[15, 12:8] Floating-point exception trap enable bits. These bits are supported only in VFPv2, VFPv3U, and VFPv4U. They are reserved, RAZ/WI, on a system that implements VFPv3 or VFPv4.&lt;/EM&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;Actually, in most systems (CPU:s and operating systems) trapping floating-point exceptions are either turned off by default or not supported at all, usually for good reasons (floating-point exceptions hurt performance, are largely misunderstood and are usually unnecessary).&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;Yeah. Though this ARM FPU exception (they are configurable to enable /0, imprecise, etc) could be kept enabled without affecting performance of any decent code:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; IOE, bit[8] Invalid Operation exception trap enable.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;First of all, it should be noted that division by zero is a well defined operation in IEEE 754 floating-point arithmetic. It returns +/-infinity&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;Yeah, but 0/0 is invalid operation, not a number, Nan. As well invalid is Inf/Inf or any operation with NaN.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 07:31:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/No-exception-when-divided-by-zero/m-p/1795187#M219187</guid>
      <dc:creator>kef2</dc:creator>
      <dc:date>2024-01-25T07:31:55Z</dc:date>
    </item>
  </channel>
</rss>

