source: libcfa/prelude/builtins.c @ df8ba61a

Last change on this file since df8ba61a was 8a1d62b6, checked in by Andrew Beach <ajbeach@…>, 16 months ago

Reorganize ?\? operator to shorten it a bit and move lines of code closer to the comments associated with them.

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[a5f0529]1//
[a1edafa]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.
[a5f0529]6//
7// builtins.c --
8//
[a1edafa]9// Author           : Peter A. Buhr
10// Created On       : Fri Jul 21 16:21:03 2017
[88ac8672]11// Last Modified By : Peter A. Buhr
[8a97248]12// Last Modified On : Thu Feb  2 11:33:56 2023
13// Update Count     : 135
[a5f0529]14//
[a1edafa]15
[c960331]16#define __cforall_builtins__
17
[b867278]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
[fd54fef]20forall(T &)
[b867278]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
[fd54fef]27forall(T &)
[b867278]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" {
[fd54fef]36        forall(T &)
[b867278]37        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
38                ^(*dtor){};
39        }
40}
41
[a1edafa]42// exception implementation
43
[36982fc]44typedef unsigned long long __cfaabi_abi_exception_type_t;
[fa4805f]45
[7726839]46#include <limits.h>                                                                             // CHAR_BIT
[bf71cfd]47#include "../src/virtual.h"
48#include "../src/exception.h"
[a1edafa]49
[169d944]50void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
[f79cdf8]51void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[169d944]52
[fd54fef]53forall(T &)
[427854b]54static inline T & identity(T & i) {
55        return i;
56}
57
58// generator support
[e84ab3d]59struct generator$ {
[427854b]60        inline int;
61};
62
[e84ab3d]63static inline void  ?{}(generator$ & this) { ((int&)this) = 0; }
64static inline void ^?{}(generator$ &) {}
[427854b]65
[8a97248]66forall( T & )
67trait is_generator {
[427854b]68      void main(T & this);
[e84ab3d]69      generator$ * get_generator(T & this);
[427854b]70};
71
[fd54fef]72forall(T & | is_generator(T))
[427854b]73static inline T & resume(T & gen) {
74        main(gen);
75        return gen;
76}
77
[0d8266c]78// implicit increment, decrement if += defined, and implicit not if != defined
[d2887f7]79
[2ead704]80// C11 reference manual Section 6.5.16 (page101): "An assignment expression has the value of the left operand after the
81// assignment, but is not an lvalue." Hence, return a value not a reference.
[8a30423]82static inline {
[8477fc4]83        forall( T | { T ?+=?( T &, one_t ); } )
84        T ++?( T & x ) { return x += 1; }
[d2887f7]85
[8477fc4]86        forall( T | { T ?+=?( T &, one_t ); } )
87        T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
[d2887f7]88
[8477fc4]89        forall( T | { T ?-=?( T &, one_t ); } )
90        T --?( T & x ) { return x -= 1; }
[d2887f7]91
[8477fc4]92        forall( T | { T ?-=?( T &, one_t ); } )
93        T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
[0d8266c]94
[8477fc4]95        forall( T | { int ?!=?( T, zero_t ); } )
96        int !?( T & x ) { return !( x != 0 ); }
[8a30423]97} // distribution
[7579ac0]98
99// universal typed pointer constant
[fd54fef]100static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[04423b53]101static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
[a1edafa]102
[cf5af9c]103#if defined(__SIZEOF_INT128__)
[b56f55c]104// constructor for 128-bit numbers (all constants are unsigned as +/- are operators)
105static inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) {
106        this = (unsigned int128)h << 64 | (unsigned int128)l;
107} // ?{}
[cf5af9c]108#endif // __SIZEOF_INT128__
[b56f55c]109
[c99a0d1]110// for-control index constraints
111// forall( T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); T ?+=?( T &, T ); T ?-=?( T &, T ); int ?<?( T, T ); } )
112// static inline T __for_control_index_constraints__( T t ) { return t; }
113
[a1edafa]114// exponentiation operator implementation
115
116extern "C" {
117        float powf( float x, float y );
118        double pow( double x, double y );
119        long double powl( long double x, long double y );
120        float _Complex cpowf( float _Complex x, _Complex float z );
121        double _Complex cpow( double _Complex x, _Complex double z );
122        long double _Complex cpowl( long double _Complex x, _Complex long double z );
123} // extern "C"
124
[8a30423]125static inline {
126        float ?\?( float x, float y ) { return powf( x, y ); }
127        double ?\?( double x, double y ) { return pow( x, y ); }
128        long double ?\?( long double x, long double y ) { return powl( x, y ); }
129        float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
130        double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
131        long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
132} // distribution
[a1edafa]133
[8dbfb7e]134#define __CFA_EXP__() \
[8a30423]135        if ( y == 0 ) return 1;                                                         /* convention */ \
[8a1d62b6]136        __CFA_EXP_INT__(                                                                        /* special cases for integral types */ \
137                if ( x == 1 ) return 1;                                                 /* base case */ \
138                if ( x == 2 ) return x << (y - 1);                              /* positive shifting */ \
139                if ( y >= sizeof(y) * CHAR_BIT ) return 0;              /* immediate overflow, negative exponent > 2^size-1 */ \
140        ) \
[88ac8672]141        typeof(x) op = 1;                                                                       /* accumulate odd product */ \
[8dbfb7e]142        for ( ; y > 1; y >>= 1 ) {                                                      /* squaring exponentiation, O(log2 y) */ \
[88ac8672]143                if ( (y & 1) == 1 ) op = op * x;                                /* odd ? */ \
144                x = x * x; \
[8dbfb7e]145        } \
[88ac8672]146        return x * op
[8a1d62b6]147#define __CFA_EXP_INT__(...) __VA_ARGS__
[8dbfb7e]148
[8a30423]149static inline {
[24d6572]150        int ?\?( int x, unsigned int y ) { __CFA_EXP__(); }
[88ac8672]151        long int ?\?( long int x, unsigned long int y ) { __CFA_EXP__(); }
152        long long int ?\?( long long int x, unsigned long long int y ) { __CFA_EXP__(); }
[8a30423]153        // unsigned computation may be faster and larger
[24d6572]154        unsigned int ?\?( unsigned int x, unsigned int y ) { __CFA_EXP__(); }
[88ac8672]155        unsigned long int ?\?( unsigned long int x, unsigned long int y ) { __CFA_EXP__(); }
156        unsigned long long int ?\?( unsigned long long int x, unsigned long long int y ) { __CFA_EXP__(); }
[8a30423]157} // distribution
[a1edafa]158
[8a1d62b6]159#undef __CFA_EXP_INT__
160#define __CFA_EXP_INT__(...)
[e472d54]161
[fd54fef]162static inline forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
[88ac8672]163        OT ?\?( OT x, unsigned int y ) { __CFA_EXP__(); }
164        OT ?\?( OT x, unsigned long int y ) { __CFA_EXP__(); }
165        OT ?\?( OT x, unsigned long long int y ) { __CFA_EXP__(); }
[8a30423]166} // distribution
[8dbfb7e]167
[8a1d62b6]168#undef __CFA_EXP_INT__
169#undef __CFA_EXP__
[a1edafa]170
[8a30423]171static inline {
[24d6572]172        int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
[8a30423]173        long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
[88ac8672]174        long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
[24d6572]175        unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
[8a30423]176        unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
[88ac8672]177        unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
[8a30423]178} // distribution
[a1edafa]179
180// Local Variables: //
181// mode: c //
182// tab-width: 4 //
183// End: //
Note: See TracBrowser for help on using the repository browser.