Wouldn't this be better with bitfields?

Dukc ajieskola at gmail.com
Wed Jul 3 09:24:52 UTC 2024


Walter Bright kirjoitti 3.7.2024 klo 2.32:
> static uint addsub_imm(uint sf, uint op, uint S, uint sh, uint imm12, 
> ubyte Rn, ubyte Rd)
> {
>      return (sf     << 31) |
>             (op     << 30) |
>             (S      << 29) |
>             (0x22   << 23) |
>             (sh     << 22) |
>             (imm12  << 10) |
>             (Rn     <<  5) |
>              Rd;
> }
> ```
> That's just to initialize it. Never mind the shift/mask code to read a 
> fields, or change just one of the fields. There's a large number of 
> these functions in instr.d. Wouldn't it be better with:
> 
> ```
> struct addsub_imm { uint Rd:5, Rn:5, imm12:12, sh:1, x22:6, S:1, op:1, 
> sf: 1; }
> ```
> 
> ??

Yes. And we already basically have it:

```D
import std.bitmanip;
struct addsub_imm
{   mixin(bitfields!
     (  ubyte, "Rd", 5,
        ubyte, "Rn", 5,
        uint, "imm12", 12,
        uint, "sh", 1,
        ubyte, "x22", 6,
        uint, "S", 1,
        uint, "op", 1,
        uint, "sf", 1
     ));
}
```

The language builtin would have somewhat nicer syntax though. What if 
bitfields were moved from `std.bitmanip` to `core.*` and the compiler 
would lower any bitfield declarations in struct/class to a `bitfields` 
mixin?


More information about the Digitalmars-d mailing list