Skip to content
Snippets Groups Projects
Select Git revision
  • 143c4592f58f761166ba04e8eba6680186424af5
  • master default protected
2 results

fmpi_data.c

Blame
  • user avatar
    raphael.bach authored
    143c4592
    History
    fmpi_data.c 3.39 KiB
    // SPDX-License-Identifier: 0BSD
    /*!
     * @file
     * @license{
     * BSD Zero Clause License
     *
     * Copyright (c) 2022 by Raphael Bach
     *
     * Permission to use, copy, modify, and/or distribute this software for any
     * purpose with or without fee is hereby granted.
     *
     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
     * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
     * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
     * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     * PERFORMANCE OF THIS SOFTWARE.
     * }
     */
    /*==============================================================================
        INCLUDE
    ==============================================================================*/
    // Own header
    #include "fmpi_data.h"
    // C Standard Library
    #include <assert.h>
    #include <stddef.h> // NULL, size_t
    // Internal
    #include "internal/fmpi_common.h"
    #include "internal/fmpi_ctx.h"
    #include "internal/fmpi_type.h"
    /*==============================================================================
        PUBLIC FUNCTION DEFINITION
    ==============================================================================*/
    #define FMPI_DATA_FUNC_DEFINITION_T(T) \
    struct fmpi_data fmpi_data_out_##T( \
        const struct fmpi_ctx * const ctx, T * const data \
    ){ \
        assert(ctx != NULL); \
        assert(data != NULL); \
        return (struct fmpi_data){ \
            .type = { \
                .base    = FMPI_TYPE_##T, \
                .derived = FMPI_TYPE_NONE, \
                .size    = sizeof(T) \
            }, \
            .cnt = 1, \
            .size = sizeof(T), \
            .dim_len = {0, 0, 0}, \
            .dim_cnt = 0, \
            .raw = data \
        }; \
    }
    
    FMPI_DEFINE_FUNCS_T(FMPI_DATA_FUNC_DEFINITION_T, FMPI_DATA_TYPES)
    
    #define FMPI_DATA_FUNC_DEFINITION_DT(D, T) \
    struct fmpi_data fmpi_data_##D##d_in_##T( \
        const struct fmpi_ctx * const ctx, T * const data, \
        const size_t x, const size_t y, const size_t z \
    ){ \
        _Static_assert((D) <= FMPI_DIM_MAX, ""); \
        assert(ctx != NULL); \
        assert(data != NULL); \
        const size_t cnt = (x * y * z); \
        return (struct fmpi_data){ \
            .type = { \
                .base    = FMPI_TYPE_##T, \
                .derived = FMPI_TYPE_ARRAY, \
                .size    = sizeof(T) \
            }, \
            .cnt = cnt, \
            .size = cnt * sizeof(T), \
            .dim_len = {x, y, z}, \
            .dim_cnt = (D), \
            .raw = data \
        }; \
    } \
    struct fmpi_data fmpi_data_##D##d_out_##T( \
        const struct fmpi_ctx * const ctx, T * const data, \
        const size_t x, const size_t y, const size_t z \
    ){ \
        _Static_assert((D) <= FMPI_DIM_MAX, ""); \
        assert(ctx != NULL); \
        assert(data != NULL); \
        const size_t cnt = (x * y * z); \
        return (struct fmpi_data){ \
            .type = { \
                .base    = FMPI_TYPE_##T, \
                .derived = FMPI_TYPE_ARRAY, \
                .size    = sizeof(T) \
            }, \
            .cnt = cnt, \
            .size = cnt * sizeof(T), \
            .dim_len = {x, y, z}, \
            .dim_cnt = (D), \
            .raw = data \
        }; \
    }
    
    FMPI_DEFINE_FUNCS_DT(FMPI_DATA_FUNC_DEFINITION_DT, 1, FMPI_DATA_TYPES)
    FMPI_DEFINE_FUNCS_DT(FMPI_DATA_FUNC_DEFINITION_DT, 2, FMPI_DATA_TYPES)
    FMPI_DEFINE_FUNCS_DT(FMPI_DATA_FUNC_DEFINITION_DT, 3, FMPI_DATA_TYPES)