# C Is Not a Low-level Language

**URL:** <https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464>\
**Category:** Uncategorized\
**Created:** [December 29, 2021, 2:26pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464 "2021-12-29T14:26:54Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [August 13, 2023, 5:20pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/21 "2023-08-13T17:20:58Z")

</div>

> [@ivanpribec](#):
>
> The issue here, is the C compiler cannot automatically determine the size of the the `x` and `y` pointers.

Isn’t there also an issue involving aliases? In C, the two arrays are allowed to be aliased, and the compiler must allow for all possible memory associations between the two (i.e. no overlaps of memory, full overlap of memory, and all possible partial overlaps). If the C code were written as `float* a` (which actually is consistent with the original fortran reference specification), then the compiler must assume that the scalar might also be aliased to the arrays, and that must also be accounted for in generating the instructions.

In fortran, the two arrays and the scalar are not allowed to be aliased, so the compiler can be aggressive with reordering the memory fetches and stores, strip mining the loop, making optimal use of registers, and so on.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [August 13, 2023, 5:35pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/22 "2023-08-13T17:35:04Z")

</div>

> [@Pap](#):
>
> C is not a low-level language. It is high-level Assembly (which I guess it qualifies as a language.)

Or is it a high-low-level language? Or a low-high-level language? 😄  
Probably it depends also a lot on your programming style, as it has a rather flexible syntax. I have already seen here some C code with very high computing performances, better than Fortran, but looking like super Assembly language… It is also one of the best language for code obfuscation…

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [August 13, 2023, 5:41pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/23 "2023-08-13T17:41:30Z")

</div>

I’d be interested to see some C that actually compiles to a faster executing binary without utilizing the escape hatch of inline assembly. Obviously if you can entirely circumvent the compiler and just have it produce the machine code for the exact instructions you want then yes, it’s probably going to utilize SIMD better and stuff like that.

I’m not extremely familiar with the actual C standard, but it looks to me that the x86 intrinsic functions aren’t actually part of it. That said, if MSVC, ICC/ICX and GCC all support them with the same syntax… Who cares what the standard says.

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [August 13, 2023, 8:01pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/24 "2023-08-13T20:01:50Z")

</div>

Everything is a pointer, semi-colons and curly brackets are my three main reasons for disliking the language. Remove those and I would have no problem with it. Oh wait. If you remove those you don’t have a language. Never mind. 🙂

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [August 13, 2023, 11:22pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/25 "2023-08-13T23:22:27Z")

</div>

> [@RonShepard](#):
>
> Isn’t there also an issue involving aliases?

There is, and indeed it can lead more efficient code. But this applies irrespective of OpenMP, which I mentioned in my original post.

The aliasing issue can be overcome in C with the standard `restrict` type qualifier (since C99). Here’s an example ([Compiler Explorer](https://godbolt.org/z/scjWcETEx)), for an x86-64 processor:

```cpp
void multiply_scalar(float *a, float *b) {
    *b = 3.0f*(*a);
}

void multiply_stream(float *a, float *b) {
    for (int i = 0; i < 4; i++)
        b[i] = 3.0f*a[i];
}

void multiply_stream_noalias(float * restrict a, float * restrict b) 
{
    for (int i = 0; i < 4; i++)
        b[i] = 3.0f*a[i];
}

```

```assembly
multiply_scalar(float*, float*):
        vmovss xmm0, DWORD PTR .LC0[rip]
        vmulss xmm0, xmm0, DWORD PTR [rdi]
        vmovss DWORD PTR [rsi], xmm0
        ret
multiply_stream(float*, float*):
        vmovss xmm1, DWORD PTR .LC0[rip]
        xor eax, eax
.L4:
        vmulss xmm0, xmm1, DWORD PTR [rdi+rax]
        vmovss DWORD PTR [rsi+rax], xmm0
        add rax, 4
        cmp rax, 16
        jne .L4
        ret
multiply_stream_noalias(float*, float*):
        vbroadcastss xmm0, DWORD PTR .LC0[rip]
        vmulps xmm0, xmm0, XMMWORD PTR [rdi] 
        vmovups XMMWORD PTR [rsi], xmm0
        ret
.LC0:
        .long 1077936128

```

As you can see, in `multiply_stream` there is a loop implementing scalar multiplication (`vmulss`). Once the `restrict` qualifier is added (or `__restrict` extension in C++), the compiler performs 4 multiplications in parallel with the `vmulps` instruction. The `xmm` registers are part of the [Streaming SIMD Extension](https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) (SSE). They are 128-bit wide, meaning they can fit four floats, or two doubles. When you multiply in scalar mode, you are essentially wasting three out of four SIMD lanes.

(Note, at `-O3` optimization some C and C++ compilers may insert runtime checks for aliasing with separate vectorized and scalar codepaths depending on the aliasing result.)

In Fortran, because of the default aliasing rules, the vector instruction is produced directly:

```fortran
subroutine multiply_stream(a,b)
   real, intent(in) :: a(4)
   real, intent(out) :: b(4)
   do i = 1, 4
    b(i) = 3.*a(i)
   end do
end subroutine

```

```assembly
multiply_stream_:
        vbroadcastss xmm0, DWORD PTR .LC0[rip]
        vmulps xmm0, xmm0, XMMWORD PTR [rdi]
        vmovups XMMWORD PTR [rsi], xmm0
        ret
.LC0:
        .long 1077936128

```

Addendum: using [Intel Intrinsics](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html) (SSE), the function to multiply four floats with a constant value can be written as,

```cpp
// Multiply elements of a by 3 and store result in b
void multiply_sse(float *a, float *b) {
    __m128 r = _mm_set_ps1(3.0f); // Broadcast 3.0 to all elements of r
    r = _mm_mul_ps (r, _mm_loadu_ps(a)); // Multiply packed single-precision operands
                                         // (for second operand unaligned load is used)
    _mm_storeu_ps(b, r); // Store result in b
}

```

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [August 13, 2023, 11:47pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/26 "2023-08-13T23:47:22Z")

</div>

> [@ivanpribec](#):
>
> The aliasing issue can be overcome in C with the standard `restrict` type qualifier (since C99).

When this is done in C, who is responsible for ensuring there are no aliases. Is it up to the programmer, or is the compiler supposed to verify it at run time?

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [August 13, 2023, 11:49pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/27 "2023-08-13T23:49:56Z")

</div>

It’s up to the programmer (just like with Fortran)

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [August 14, 2023, 1:31am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/28 "2023-08-14T01:31:50Z")

</div>

I will note that it is at least a little insane that the Fortran language expects procedure arguments shall not alias, but no compiler will even warn if this assumption is violated at a call site.

There exist in large production code bases “routines that cannot be optimized” because anything beyond -O0 will make the compiler generate code that actually takes advantage of the no aliasing assumption. It would be nice if the compiler would at least warn the user when core assumptions of the language are being violated.

EDIT: A correction - it is possible there are compilers that will correctly warn the user (or better refuse to compile an issue an error) for the language assumption violation that is argument aliasing. However, `gfortran`, `ifort`, `ifx`, and `aocc flang` all fail to do so. They each compile and run the following program, but yield different outputs.

```auto
program main
implicit none

    real :: x, y

    x = 1.0
    y = 2.0
    write(*,*) 'MAIN -- x: ',x,', y: ',y

    call sub1(x, x)
    write(*,*) 'MAIN -- x: ',x,', y: ',y

    call sub2(y, y)
    write(*,*) 'MAIN -- x: ',x,', y: ',y

end program main
subroutine sub1(arg1, arg2)
implicit none
    real, intent(in) :: arg1
    real, intent(out) :: arg2
    write(*,*) 'entering sub1 -- arg1/arg2: ',arg1,arg2
    arg2 = arg1*10.0
    write(*,*) 'leaving sub1 -- arg1/arg2: ',arg1,arg2
end subroutine sub1
subroutine sub2(arg1, arg2)
implicit none
    real, intent(in) :: arg1
    real, intent(out) :: arg2
    write(*,*) 'entering sub2 -- arg1/arg2: ',arg1,arg2
    call sub1(arg1, arg1)
    write(*,*) 'sub2 returning from "call sub1" -- arg1/arg2: ',arg1,arg2
    arg2 = arg1*10.0
    write(*,*) 'leaving sub2 -- arg1/arg2: ',arg1,arg2
end subroutine sub2

```

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [August 14, 2023, 1:35am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/29 "2023-08-14T01:35:24Z")

</div>

If you want a language that actually does that you should try out Rust. Rust works by ensuring that all functions either get only immutable references or one mutable reference.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [August 14, 2023, 2:07am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/30 "2023-08-14T02:07:57Z")

</div>

Rust is interesting. If I were to branch out language wise, it would be a top contender right next to Zig. My interest with Zig is more along the lines of `comptime`, but ease of use with C is also a big plus. Rust seems… Like a lot of hassle? I think it would be very rewarding, but the level of input effort required for basic competency sounds significantly higher than lots of other languages that I could devote my free time to learning.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 14, 2023, 2:39am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/31 "2023-08-14T02:39:22Z")

</div>

> [@tyranids](#):
>
> I will note that it is at least a little insane that the Fortran language expects procedure arguments shall not alias, but no compiler will even warn if this assumption is violated at a call site.

Gfortran will, at least in some cases. After compiling the module

```auto
module m
implicit none
contains
subroutine sub(i,i2)
integer, intent(in) :: i
integer, intent(out) :: i2
i2 = 2*i
end subroutine sub
end module m

```

`gfortran -c -Wall -Wextra xxalias.f90` for

```auto
program main
use m
implicit none
integer :: i
i = 3
call sub(i,i)
print*,i
end program main

```

says

```auto
xxalias.f90:6:9:

    6 | call sub(i,i)
      | 1
Warning: Same actual argument associated with INTENT(IN) argument 'i' and INTENT(OUT) argument 'i2' at (1)

```

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [August 21, 2023, 3:27pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/32 "2023-08-21T15:27:37Z")

</div>

> [@tyranids](#):
>
> no compiler will even warn if this assumption is violated at a call site.

That is not true.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 21, 2023, 11:10pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/33 "2023-08-21T23:10:53Z")

</div>

@themos do you know if the NAG compiler warns about any possible alias, or only some?

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [August 22, 2023, 10:01am UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/34 "2023-08-22T10:01:19Z")

</div>

The claim ([https://www.nag.com/nagware/np/r71\_doc/manual/compiler\_2\_4.html#OPTIONS](https://www.nag.com/nagware/np/r71_doc/manual/compiler_2_4.html#OPTIONS)) is that

> The _-C=alias_ option will produce a runtime error when it is detected that assignment to a dummy argument affects another dummy argument. At this release this is only detected for scalar dummy arguments.

I know NAG will welcome feedback on desirable features from paying customers. After 30-odd years of development, I have to assume that the demand for more alias detection just wasn’t there in sufficient quantity.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [August 22, 2023, 12:56pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/35 "2023-08-22T12:56:45Z")

</div>

Thank you for pointing out my error. I have since corrected my post. Among the compilers I regularly interact with, `gfortran`, `ifort`, `ifx`, and AMD `flang`, none of them emit compiler errors (or even warnings) about the following program - at least not with default flags (i.e. none). They also produce different results:

```auto
subroutine sub1(arg1, arg2)
implicit none
    real, intent(in) :: arg1
    real, intent(out) :: arg2
    write(*,*) 'entering sub1 -- arg1/arg2: ',arg1,arg2
    arg2 = arg1*10.0
    write(*,*) 'leaving sub1 -- arg1/arg2: ',arg1,arg2
end subroutine sub1

```

```auto
subroutine sub2(arg1, arg2)
implicit none
    real, intent(in) :: arg1
    real, intent(out) :: arg2
    write(*,*) 'entering sub2 -- arg1/arg2: ',arg1,arg2
    call sub1(arg1, arg1)
    write(*,*) 'sub2 returning from "call sub1" -- arg1/arg2: ',arg1,arg2
    arg2 = arg1*10.0
    write(*,*) 'leaving sub2 -- arg1/arg2: ',arg1,arg2
end subroutine sub2

```

```auto
program main
implicit none

    real :: x, y

    x = 1.0
    y = 2.0
    write(*,*) 'MAIN -- x: ',x,', y: ',y

    call sub1(x, x)
    write(*,*) 'MAIN -- x: ',x,', y: ',y

    call sub2(y, y)
    write(*,*) 'MAIN -- x: ',x,', y: ',y

end program main

```

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 22, 2023, 1:16pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/36 "2023-08-22T13:16:48Z")

</div>

@tyranids looks like you are using implicit interfaces, i.e. the routines are not in a module, are not contained, and they don’t have interface blocks. Then, when the compiler encounters a `call`, it doesn’t know that one of the dummy argument is `intent(out)` (remind that aliasing is fine as long as the arguments are not modified).

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [August 22, 2023, 1:42pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/37 "2023-08-22T13:42:34Z")

</div>

Exactly, and this is a common configuration of legacy code bases, which also happens to be (likely the most) common use case for the Fortran programming language.

I would argue it is a fundamental design flaw that a language would have underlying rules with no way to verify that they are followed at compile time. This is especially true when not following said rules leads to different results.

EDIT: Even more common would be not including the intent bits at all. The fact that they _can_ exist and still be ignored or effectively do nothing is an entirely separate issue (which I would also consider unacceptable).

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 22, 2023, 1:52pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/38 "2023-08-22T13:52:25Z")

</div>

Gfortran by default refuses to compile the subroutines when they are placed in a

> **module**
>
> ```auto
> module m
> contains
> subroutine sub1(arg1, arg2)
> implicit none
> real, intent(in) :: arg1
> real, intent(out) :: arg2
> write(*,*) 'entering sub1 -- arg1/arg2: ',arg1,arg2
> arg2 = arg1*10.0
> write(*,*) 'leaving sub1 -- arg1/arg2: ',arg1,arg2
> end subroutine sub1
> subroutine sub2(arg1, arg2)
> implicit none
> real, intent(in) :: arg1
> real, intent(out) :: arg2
> write(*,*) 'entering sub2 -- arg1/arg2: ',arg1,arg2
> call sub1(arg1, arg1)
> write(*,*) 'sub2 returning from "call sub1" -- arg1/arg2: ',arg1,arg2
> arg2 = arg1*10.0
> write(*,*) 'leaving sub2 -- arg1/arg2: ',arg1,arg2
> end subroutine sub2
> end module m
> 
> ```

```auto
alias.f90:16:20:

   16 | call sub1(arg1, arg1)
      | 1
Error: Dummy argument 'arg1' with INTENT(IN) in variable definition context (actual argument to INTENT = OUT/INOUT) at (1)

```

A known Fortran best practice is to put procedures in modules, or at least to provide explicit interfaces. This, along with adding argument intents, is part of the modernization process.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 22, 2023, 2:00pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/39 "2023-08-22T14:00:40Z")

</div>

> [@tyranids](#):
>
> I would argue it is a fundamental design flaw that a language would have underlying rules with no way to verify that they are followed at compile time. This is especially true when not following said rules leads to different results.

This is why IMPLICIT NONE(EXTERNAL) was added to Fortran 2018 - to force the programmer to specify the interface (or at least to explicitly declare external) called procedures. Many compilers nowadays have the ability to check interfaces if you don’t use them, but aliasing is a tricky thing to reliably diagnose.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 22, 2023, 2:11pm UTC](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464/40 "2023-08-22T14:11:47Z")

</div>

> [@tyranids](#):
>
> Exactly, and this is a common configuration of legacy code bases, which also happens to be (likely the most) common use case for the Fortran programming language.

Legacy code base is as it is, you won’t change the past…

> [@tyranids](#):
>
> I would argue it is a fundamental design flaw that a language would have underlying rules with no way to verify that they are followed at compile time.

This kind of “flaw” is quite common in any language, especially in oldest ones like Fortran. You cannot expect a compiler to catch every possible violation of the standard. Yet, modern Fortran aims at catching more of them, but it’s the responsability of the developer to effectively use the modern feature (an analogy for what it’s worth: modern cars have seatbelts, but still, you are allowed to drive old cars without seatbelts).

[Previous page](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464.md?page=1)

[Next page](https://fortran-lang.discourse.group/t/c-is-not-a-low-level-language/2464.md?page=3)
