File: stdckdint.h

package info (click to toggle)
sdcc 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 105,232 kB
  • sloc: ansic: 956,095; cpp: 110,511; makefile: 59,314; sh: 29,875; asm: 17,178; perl: 12,136; yacc: 7,480; lisp: 1,672; python: 907; lex: 805; awk: 498; sed: 89
file content (137 lines) | stat: -rw-r--r-- 5,441 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*-------------------------------------------------------------------------
   stdckdint.h: Checked integer arithmetic

   Copyright (C) 2021, Philipp Klaus Krause, pkk@spth.de

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this library; see the file COPYING. If not, write to the
   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.

   As a special exception, if you link this library with other files,
   some of which are compiled with SDCC, to produce an executable,
   this library does not by itself cause the resulting executable to
   be covered by the GNU General Public License. This exception does
   not however invalidate any other reasons why the executable file
   might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/

#ifndef __SDCC_STDCKDINT_H
#define __SDCC_STDCKDINT_H 1

_Static_assert (sizeof(long long) >= 2 * sizeof(long));

// Default implementation: Suitable for additive operators for everything smaller than long long, and for multiplication for everything smaller than long long except for unsigned long times unsigned long. Not very efficient.
#define __CKD_DEFAULT_IMPL(T,O) \
(T *r, signed long long a, signed long long b) \
{ \
	signed long long result = a O b; \
	*r = result; \
	return (*r == result); \
}

inline _Bool __ckd_add_short __CKD_DEFAULT_IMPL(short, +)
inline _Bool __ckd_add_ushort __CKD_DEFAULT_IMPL(unsigned short, +)
inline _Bool __ckd_add_int __CKD_DEFAULT_IMPL(int, +)
inline _Bool __ckd_add_uint __CKD_DEFAULT_IMPL(unsigned int, +)
inline _Bool __ckd_add_long __CKD_DEFAULT_IMPL(long, +)
inline _Bool __ckd_add_ulong __CKD_DEFAULT_IMPL(unsigned long, +)

inline _Bool __ckd_sub_short __CKD_DEFAULT_IMPL(short, -)
inline _Bool __ckd_sub_ushort __CKD_DEFAULT_IMPL(unsigned short, -)
inline _Bool __ckd_sub_int __CKD_DEFAULT_IMPL(int, -)
inline _Bool __ckd_sub_uint __CKD_DEFAULT_IMPL(unsigned int, -)
inline _Bool __ckd_sub_long __CKD_DEFAULT_IMPL(long, -)
inline _Bool __ckd_sub_ulong __CKD_DEFAULT_IMPL(unsigned long, -)

inline _Bool __ckd_mul_short __CKD_DEFAULT_IMPL(short, *)
inline _Bool __ckd_mul_ushort __CKD_DEFAULT_IMPL(unsigned short, *)
inline _Bool __ckd_mul_int __CKD_DEFAULT_IMPL(int, *)
inline _Bool __ckd_mul_uint __CKD_DEFAULT_IMPL(unsigned int, *)
inline _Bool __ckd_mul_long __CKD_DEFAULT_IMPL(long, *)
inline _Bool __ckd_mul_ulong __CKD_DEFAULT_IMPL(unsigned long, *)

#define __ckd_add_default(r, a, b) \
  _Generic ((r), \
    short * : __ckd_add_short, \
    unsigned short * : __ckd_add_ushort, \
    int * : __ckd_add_int, \
    unsigned int * : __ckd_add_uint, \
    long * : __ckd_add_long, \
    unsigned long * : __ckd_add_ulong) \
    (r, a, b)

#define __ckd_sub_default(r, a, b) \
  _Generic ((r), \
    short * : __ckd_sub_short, \
    unsigned short * : __ckd_sub_ushort, \
    int * : __ckd_sub_int, \
    unsigned int * : __ckd_sub_uint, \
    long * : __ckd_sub_long, \
    unsigned long * : __ckd_sub_ulong) \
    (r, a, b)
    
#define __ckd_mul_default(r, a, b) \
  _Generic ((r), \
    short * : __ckd_mul_short, \
    unsigned short * : __ckd_mul_ushort, \
    int * : __ckd_mul_int, \
    unsigned int * : __ckd_mul_uint, \
    long * : __ckd_mul_long \
    unsigned long * : __ckd_mul_ulong \
    (r, a, b)

extern _Bool __ckd_add_unimplemented (void *, unsigned long long, unsigned long long);

#define ckd_add(r, a, b) \
  _Generic ((a), \
  signed long long: __ckd_add_unimplemented(r, a, b), \
  unsigned long long: __ckd_add_unimplemented(r, a, b), \
  default: \
    _Generic ((b), \
    signed long long: __ckd_add_unimplemented(r, a, b), \
    unsigned long long: __ckd_add_unimplemented(r, a, b), \
    default: __ckd_add_default(r, a, b))) \

extern _Bool __ckd_sub_unimplemented (void *, unsigned long long, unsigned long long);

#define ckd_sub(r, a, b) \
  _Generic ((a), \
  signed long long: __ckd_sub_unimplemented(r, a, b), \
  unsigned long long: __ckd_sub_unimplemented(r, a, b), \
  default: \
    _Generic ((b), \
    signed long long: __ckd_sub_unimplemented(r, a, b), \
    unsigned long long: __ckd_sub_unimplemented(r, a, b), \
    default: __ckd_sub_default(r, a, b))) \

extern _Bool __ckd_mul_unimplemented (void *, unsigned long long, unsigned long long);

#define ckd_mul(r, a, b) \
  _Generic ((a), \
  signed long long: __ckd_mul_unimplemented(r, a, b), \
  unsigned long long: __ckd_mul_unimplemented(r, a, b), \
  unsigned long: \
    _Generic ((b), \
    signed long long: __ckd_mul_unimplemented(r, a, b), \
    unsigned long long: __ckd_mul_unimplemented(r, a, b), \
    unsigned long: __ckd_mul_unimplemented(r, a, b), \
    default: __ckd_mul_default(r, a, b)) \
  default: \
    _Generic ((b), \
    signed long long: __ckd_mul_unimplemented(r, a, b), \
    unsigned long long: __ckd_mul_unimplemented(r, a, b), \
    default: __ckd_mul_default(r, a, b))) \

#endif