source: libcfa/prelude/builtins.c @ 81e768d

Last change on this file since 81e768d was 108b2c7, checked in by Andrew Beach <ajbeach@…>, 3 weeks ago

Moved the non-trivial bodies of the power (exponental) functions out of the prelude and into the library. They have caused some issues in debugging there.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// builtins.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Fri Jul 21 16:21:03 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Nov  8 17:07:15 2024
13// Update Count     : 144
14//
15
16#define __cforall_builtins__
17
18// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
19// Note: needs to occur early, because it is used to generate destructor calls during code generation
20forall(T &)
21struct __Destructor {
22        T * object;
23        void (*dtor)(T *);
24};
25
26// defined destructor in the case that non-generated code wants to use __Destructor
27forall(T &)
28static inline void ^?{}(__Destructor(T) & x) {
29        if (x.object && x.dtor) {
30                x.dtor(x.object);
31        }
32}
33
34// easy interface into __Destructor's destructor for easy codegen purposes
35extern "C" {
36        forall(T &)
37        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
38                ^(*dtor){};
39        }
40}
41
42// exception implementation
43
44typedef unsigned long long __cfaabi_abi_exception_type_t;
45
46#include "../src/virtual.h"
47#include "../src/exception.h"
48
49void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
50void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
51
52forall(T &)
53static inline T & identity(T & i) {
54        return i;
55}
56
57// generator support
58struct generator$ {
59        inline int;
60};
61
62static inline void  ?{}(generator$ & this) { ((int&)this) = 0; }
63static inline void ^?{}(generator$ &) {}
64
65forall( T & )
66trait is_generator {
67      void main(T & this);
68      generator$ * get_generator(T & this);
69};
70
71forall(T & | is_generator(T))
72static inline T & resume(T & gen) {
73        main(gen);
74        return gen;
75}
76
77// implicit increment, decrement if += defined, and implicit not if != defined
78
79// C11 reference manual Section 6.5.16 (page101): "An assignment expression has the value of the left operand after the
80// assignment, but is not an lvalue." Hence, return a value not a reference.
81static inline {
82        forall( T | { T ?+=?( T &, one_t ); } )
83        T ++?( T & x ) { return x += 1; }
84
85        forall( T | { T ?+=?( T &, one_t ); } )
86        T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
87
88        forall( T | { T ?-=?( T &, one_t ); } )
89        T --?( T & x ) { return x -= 1; }
90
91        forall( T | { T ?-=?( T &, one_t ); } )
92        T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
93
94        forall( T | { int ?!=?( T, zero_t ); } )
95        int !?( T & x ) { return !( x != 0 ); }
96} // distribution
97
98// universal typed pointer constant
99static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
100static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
101
102#if defined(__SIZEOF_INT128__)
103// constructor for 128-bit numbers (all constants are unsigned as +/- are operators)
104static inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) {
105        this = (unsigned int128)h << 64 | (unsigned int128)l;
106} // ?{}
107#endif // __SIZEOF_INT128__
108
109// for-control index constraints
110// forall( T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); T ?+=?( T &, T ); T ?-=?( T &, T ); int ?<?( T, T ); } )
111// static inline T __for_control_index_constraints__( T t ) { return t; }
112
113// exponentiation operator implementation
114
115extern "C" {
116        float powf( float x, float y );
117        double pow( double x, double y );
118        long double powl( long double x, long double y );
119        float _Complex cpowf( float _Complex x, _Complex float z );
120        double _Complex cpow( double _Complex x, _Complex double z );
121        long double _Complex cpowl( long double _Complex x, _Complex long double z );
122} // extern "C"
123
124static inline {
125        float ?\?( float x, float y ) { return powf( x, y ); }
126        double ?\?( double x, double y ) { return pow( x, y ); }
127        long double ?\?( long double x, long double y ) { return powl( x, y ); }
128        float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
129        double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
130        long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
131} // distribution
132
133int ?\?( int x, unsigned int y );
134long int ?\?( long int x, unsigned long int y );
135long long int ?\?( long long int x, unsigned long long int y );
136// unsigned computation may be faster and larger
137unsigned int ?\?( unsigned int x, unsigned int y );
138unsigned long int ?\?( unsigned long int x, unsigned long int y );
139unsigned long long int ?\?( unsigned long long int x, unsigned long long int y );
140
141forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
142        OT ?\?( OT x, unsigned int y );
143        OT ?\?( OT x, unsigned long int y );
144        OT ?\?( OT x, unsigned long long int y );
145} // distribution
146
147static inline {
148        int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
149        long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
150        long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
151        unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
152        unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
153        unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
154} // distribution
155
156struct quasi_void {};
157static inline void ?{}(quasi_void &) {}
158static inline void ?{}(quasi_void &, quasi_void) {}
159static inline void ^?{}(quasi_void &) {}
160static inline quasi_void ?=?(quasi_void &, quasi_void & _src) { return _src; }
161
162// Local Variables: //
163// mode: c //
164// tab-width: 4 //
165// End: //
Note: See TracBrowser for help on using the repository browser.