aboutsummaryrefslogtreecommitdiff
path: root/include/types.h
blob: 7e0066cb7fb60df3ee6da5515b5bf48dc7ba17c2 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// types.h --- basic types                                             -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
// Code:

#ifndef INCLUDED_COSMOS_TYPES_H
#define INCLUDED_COSMOS_TYPES_H

#include <stdint.h>
#include <float.h>
#include <stddef.h>
#include <unistd.h>

typedef   int8_t  i8;
typedef  uint8_t  u8;
typedef  int16_t i16;
typedef uint16_t u16;
typedef  int32_t i32;
typedef uint32_t u32;
typedef  int64_t i64;
typedef uint64_t u64;

#define  I8_MAX   INT8_MAX
#define  I8_MIN   INT8_MIN
#define  U8_MAX  UINT8_MAX
#define I16_MAX  INT16_MAX
#define I16_MIN  INT16_MIN
#define U16_MAX UINT16_MAX
#define I32_MAX  INT32_MAX
#define I32_MIN  INT32_MIN
#define U32_MAX UINT32_MAX
#define I64_MAX  INT64_MAX
#define I64_MIN  INT64_MIN
#define U64_MAX UINT64_MAX

// typedef          __int128 i128;
// typedef unsigned __int128 u128;

typedef float  f32;
typedef double f64;

// typedef  _Float16  f16;
// typedef __float80  f80;
// typedef __float128 f128;

typedef ssize_t isz;
typedef  size_t usz;

typedef intptr_t  iptr;
typedef uintptr_t uptr;

typedef ptrdiff_t ptrdiff;

// Memory Block
struct mb { void *ptr; usz sz; };
#define MB_NULL  ((struct mb) {0})
#define MB_FROM(PTR, SZ) ((struct mb) { .ptr = (void *) PTR, .sz = SZ })
#define MB( lit) ((struct mb) { .ptr = &(lit), .sz = sizeof (lit)     })
#define MB0(lit) ((struct mb) { .ptr = &(lit), .sz = sizeof (lit) - 1 })
#define MB_FMT(mb) (int) (mb).sz, (mb).ptr
#define MB_END(mb) ((void *) ((u8 *) (mb).ptr + mb.sz))

static inline int
usz_mul_overflow (usz a, usz b, usz *out)
{
#if defined (__GNUC__) || defined (__clang__)
  return __builtin_mul_overflow (a, b, out);
#else

  if (a != 0 && b > (usz) -1/a)
    return 1;

  *out = a*b;

  return 0;
#endif
}

#endif // INCLUDED_COSMOS_TYPES_H

// types.h ends here