File: GB_macrofy_bytes.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (50 lines) | stat: -rw-r--r-- 1,732 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//------------------------------------------------------------------------------
// GB_macrofy_bytes: create a single scalar from an array of bytes
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2021, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// When the macro is used, sizeof (ztype) must be the same as nbytes

#include "GB.h"
#include "GB_stringify.h"

void GB_macrofy_bytes
(
    // input:
    FILE *fp,           // File to write macros, assumed open already
    const char *Name,         // all-upper-case name
    const char *type_name,    // name of the type
    const uint8_t *value,     // array of size nbytes
    size_t nbytes
)
{

    fprintf (fp,
        "#define GB_DECLARE_%s(z) %s z ; \\\n"
        "    {                                                       \\\n"
        "        const uint8_t bytes [%d] =                          \\\n"
        "        {                                                   \\\n"
        "            ",
        Name, type_name, (int) nbytes) ;
    for (int k = 0 ; k < nbytes ; k++)
    {
        fprintf (fp, "0x%02x", (int) (value [k])) ;
        if (k < nbytes-1)
        {
            fprintf (fp, ", ") ;
            if (k > 0 && k % 8 == 7) fprintf (fp, "\\\n            ") ;
        }
    }
    // finalize the array and use memcpy to initialize the scalar
    fprintf (fp,
        "  \\\n"
        "        } ;                                                 \\\n"
        "        memcpy (&z, bytes, %d) ;                            \\\n"
        "    }\n",
        (int) nbytes) ;
}