www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - translate alias align from C to D

reply Dakota <dakota gmail.com> writes:
```c
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)

#endif

#if defined(_MSC_VER)
/* do not use alignment for older visual studio versions */






#else

#endif

#ifndef CGLM_ALL_UNALIGNED

#else

#endif

#ifdef __AVX__

#else

#endif

typedef CGLM_ALIGN_IF(16) float vec4[4];
typedef CGLM_ALIGN_IF(16) vec2  mat2[2];
typedef CGLM_ALIGN_MAT    vec4  mat4[4];
```

I want tranlate this into d without importC, what is the best way 
to do it?
Jan 13
next sibling parent reply Matheus Catarino <matheus-catarino hotmail.com> writes:
On Monday, 13 January 2025 at 11:36:45 UTC, Dakota wrote:
 I want tranlate this into d without importC, what is the best 
 way to do it?
You could simply use dstep or dmd/ldc2 `-H`|`-Hf=<filename>`. My test on linux shows that both can translate/convert the file. Although there are differences such as: - copies the comments - does not found `<stdalign.h>` - does not translate ifdef/endif to version - translates C source and C headers - does not copy comments - does not translate ifdef/endif to version - translate only C source files [add header in source] - also translates compiler directives **from** ```c typedef CGLM_ALIGN_IF(16) float vec4[4]; typedef CGLM_ALIGN_IF(16) float vec2[2]; typedef CGLM_ALIGN_IF(16) vec2 mat2[2]; typedef CGLM_ALIGN_MAT vec4 mat4[4]; ``` **to** ```d alias vec4 = float[4]; alias vec2 = float[2]; alias mat2 = float[2][2]; alias mat4 = float[4][4]; ``` However, `CGLM_ALIGN`: **from** ```c #if defined(_MSC_VER) /* do not use alignment for older visual studio versions */ #else #endif #ifndef CGLM_ALL_UNALIGNED #else #endif #ifdef __AVX__ #else #endif ``` **to** ```d // importC auto CGLM_ALIGN(__MP19)(__MP19 X) { return __attribute(aligned(X)); } auto CGLM_ALIGN_IF(__MP20)(__MP20 X) { return CGLM_ALIGN(X); } ``` ```d // dstep /* do not use alignment for older visual studio versions */ /* Visual Studio 2017 version 15.6 */ /* no alignment */ alias CGLM_ALIGN_IF = CGLM_ALIGN; /* no alignment */ enum CGLM_ALIGN_MAT = CGLM_ALIGN(16); ``` In any case, it will require manual adjustments. **Note:** This isn't perfect, but hopefully it gives you some idea of what it would be like. ```d import core.stdcpp.xutility; version (CppRuntime_Microsoft) { static if (_MSC_VER < 1913) { enum CGLM_ALL_UNALIGNED = true; } else { enum CGLM_ALIGN(ushort X) = "align(" ~ X.stringof ~ ")"; } } else { enum CGLM_ALIGN(ushort X) = "align(" ~ X.stringof ~ ")"; } static if (!is(typeof(CGLM_ALL_UNALIGNED))) { enum CGLM_ALIGN_IF(ushort X) = CGLM_ALIGN!X; } version (AVX) { enum CGLM_ALIGN_MAT = CGLM_ALIGN!32; } else { enum CGLM_ALIGN_MAT = CGLM_ALIGN!16; } ```
Jan 13
parent Dakota <dakota gmail.com> writes:
On Monday, 13 January 2025 at 14:52:05 UTC, Matheus Catarino 
wrote:
 On Monday, 13 January 2025 at 11:36:45 UTC, Dakota wrote:
 You could simply use dstep or dmd/ldc2 `-H`|`-Hf=<filename>`.
Thanks for tips. My point is c allot align to alias, is D support it ? I want avoid importC, because I want to write this in D, and the data need pass to C so the memory layout need to be same.
Jan 13
prev sibling parent Lance Bachmeier <no spam.net> writes:
On Monday, 13 January 2025 at 11:36:45 UTC, Dakota wrote:

 I want tranlate this into d without importC, what is the best 
 way to do it?
When you say "without importC" do you mean without compiling it using importC? Because if you're able to use importC for just the translation, you can put the code in file.c and then ``` dmd -inline -c file.c -Hf=file.d ``` https://dlang.org/spec/importc.html#ctod The snippet you've posted doesn't work because `vec2` isn't defined. It does if I add `struct vec2;`.
Jan 13