summary refs log blame commit diff stats
path: root/tinyc/win32/include/excpt.h
blob: 26cc9437c0b4c79d991f3fbe8e5f904123e611fe (plain) (tree)


















































































































                                                                                                                                                                                                                                                                                          

      

                  

      
                 
      
/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the w64 mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within this package.
 */
#ifndef _INC_EXCPT
#define _INC_EXCPT

#include <_mingw.h>

#pragma pack(push,_CRT_PACKING)

#ifdef __cplusplus
extern "C" {
#endif

  struct _EXCEPTION_POINTERS;

#ifndef EXCEPTION_DISPOSITION
#define EXCEPTION_DISPOSITION   int
#endif
#define ExceptionContinueExecution 0
#define ExceptionContinueSearch 1
#define ExceptionNestedException 2
#define ExceptionCollidedUnwind 3

#if (defined(_X86_) && !defined(__x86_64))
  struct _EXCEPTION_RECORD;
  struct _CONTEXT;

  EXCEPTION_DISPOSITION __cdecl _except_handler(struct _EXCEPTION_RECORD *_ExceptionRecord,void *_EstablisherFrame,struct _CONTEXT *_ContextRecord,void *_DispatcherContext);
#elif defined(__ia64__)

  typedef struct _EXCEPTION_POINTERS *Exception_info_ptr;
  struct _EXCEPTION_RECORD;
  struct _CONTEXT;
  struct _DISPATCHER_CONTEXT;

  _CRTIMP EXCEPTION_DISPOSITION __cdecl __C_specific_handler (struct _EXCEPTION_RECORD *_ExceptionRecord,unsigned __int64 _MemoryStackFp,unsigned __int64 _BackingStoreFp,struct _CONTEXT *_ContextRecord,struct _DISPATCHER_CONTEXT *_DispatcherContext,unsigned __int64 _GlobalPointer);
#elif defined(__x86_64)

  struct _EXCEPTION_RECORD;
  struct _CONTEXT;
#endif

#define GetExceptionCode _exception_code
#define exception_code _exception_code
#define GetExceptionInformation (struct _EXCEPTION_POINTERS *)_exception_info
#define exception_info (struct _EXCEPTION_POINTERS *)_exception_info
#define AbnormalTermination _abnormal_termination
#define abnormal_termination _abnormal_termination

  unsigned long __cdecl _exception_code(void);
  void *__cdecl _exception_info(void);
  int __cdecl _abnormal_termination(void);

#define EXCEPTION_EXECUTE_HANDLER 1
#define EXCEPTION_CONTINUE_SEARCH 0
#define EXCEPTION_CONTINUE_EXECUTION -1

  /* CRT stuff */
  typedef void (__cdecl * _PHNDLR)(int);

  struct _XCPT_ACTION {
    unsigned long XcptNum;
    int SigNum;
    _PHNDLR XcptAction;
  };

  extern struct _XCPT_ACTION _XcptActTab[];
  extern int _XcptActTabCount;
  extern int _XcptActTabSize;
  extern int _First_FPE_Indx;
  extern int _Num_FPE;

  int __cdecl __CppXcptFilter(unsigned long _ExceptionNum,struct _EXCEPTION_POINTERS * _ExceptionPtr);
  int __cdecl _XcptFilter(unsigned long _ExceptionNum,struct _EXCEPTION_POINTERS * _ExceptionPtr);

  /*
  * The type of function that is expected as an exception handler to be
  * installed with _try1.
  */
  typedef EXCEPTION_DISPOSITION (*PEXCEPTION_HANDLER)(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);

#ifndef HAVE_NO_SEH
  /*
  * This is not entirely necessary, but it is the structure installed by
  * the _try1 primitive below.
  */
  typedef struct _EXCEPTION_REGISTRATION {
    struct _EXCEPTION_REGISTRATION *prev;
    EXCEPTION_DISPOSITION (*handler)(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
  } EXCEPTION_REGISTRATION, *PEXCEPTION_REGISTRATION;

  typedef EXCEPTION_REGISTRATION EXCEPTION_REGISTRATION_RECORD;
  typedef PEXCEPTION_REGISTRATION PEXCEPTION_REGISTRATION_RECORD;
#endif

#if (defined(_X86_) && !defined(__x86_64))
#define __try1(pHandler) \
  __asm__ ("pushl %0;pushl %%fs:0;movl %%esp,%%fs:0;" : : "g" (pHandler));

#define	__except1	\
  __asm__ ("movl (%%esp),%%eax;movl %%eax,%%fs:0;addl $8,%%esp;" \
  : : : "%eax");
#elif defined(__x86_64)
#define __try1(pHandler) \
  __asm__ ("pushq %0;pushq %%gs:0;movq %%rsp,%%gs:0;" : : "g" (pHandler));

#define	__except1	\
  __asm__ ("movq (%%rsp),%%rax;movq %%rax,%%gs:0;addq $16,%%rsp;" \
  : : : "%rax");
#else
#define __try1(pHandler)
#define __except1
#endif

#ifdef __cplusplus
}
#endif

#pragma pack(pop)
#endif
pan class="p">(high(occur)) inc occur[x] doAssert max(occur) <= 140 and min(occur) >= 60 # gives some slack var a = [0, 1] shuffle(a) doAssert a in [[0,1], [1,0]] doAssert rand(0) == 0 doAssert sample("a") == 'a' when compileOption("rangeChecks"): doAssertRaises(RangeDefect): discard rand(-1) doAssertRaises(RangeDefect): discard rand(-1.0) # don't use causes integer overflow doAssert compiles(rand[int](low(int) .. high(int))) main() block: when not defined(js): doAssert almostEqual(rand(12.5), 4.012897747078944) doAssert almostEqual(rand(2233.3322), 879.702755321298) type DiceRoll = range[0..6] doAssert rand(DiceRoll).int == 4 var rs: RunningStat for j in 1..5: for i in 1 .. 100_000: rs.push(gauss()) doAssert abs(rs.mean-0) < 0.08, $rs.mean doAssert abs(rs.standardDeviation()-1.0) < 0.1 let bounds = [3.5, 5.0] for a in [rs.max, -rs.min]: doAssert a >= bounds[0] and a <= bounds[1] rs.clear() block: type DiceRoll = range[3..6] var flag = false for i in 0..<100: if rand(5.DiceRoll) < 3: flag = true doAssert flag # because of: rand(max: int): int block: # random int block: # there might be some randomness var set = initHashSet[int](128) for i in 1..1000: incl(set, rand(high(int))) doAssert len(set) == 1000 block: # single number bounds work var rand: int for i in 1..1000: rand = rand(1000) doAssert rand <= 1000 doAssert rand >= 0 block: # slice bounds work var rand: int for i in 1..1000: rand = rand(100..1000) doAssert rand <= 1000 doAssert rand >= 100 block: # again gives new numbers var rand1 = rand(1000000) when not defined(js): os.sleep(200) var rand2 = rand(1000000) doAssert rand1 != rand2 block: # random float block: # there might be some randomness var set = initHashSet[float](128) for i in 1..100: incl(set, rand(1.0)) doAssert len(set) == 100 block: # single number bounds work var rand: float for i in 1..1000: rand = rand(1000.0) doAssert rand <= 1000.0 doAssert rand >= 0.0 block: # slice bounds work var rand: float for i in 1..1000: rand = rand(100.0..1000.0) doAssert rand <= 1000.0 doAssert rand >= 100.0 block: # again gives new numbers var rand1: float = rand(1000000.0) when not defined(js): os.sleep(200) var rand2: float = rand(1000000.0) doAssert rand1 != rand2 block: # random sample block: # "non-uniform array sample unnormalized int CDF let values = [10, 20, 30, 40, 50] # values let counts = [4, 3, 2, 1, 0] # weights aka unnormalized probabilities var histo = initCountTable[int]() let cdf = counts.cumsummed # unnormalized CDF for i in 0 ..< 5000: histo.inc(sample(values, cdf)) doAssert histo.len == 4 # number of non-zero in `counts` # Any one bin is a binomial random var for n samples, each with prob p of # adding a count to k; E[k]=p*n, Var k=p*(1-p)*n, approximately Normal for # big n. So, P(abs(k - p*n)/sqrt(p*(1-p)*n))>3.0) =~ 0.0027, while # P(wholeTestFails) =~ 1 - P(binPasses)^4 =~ 1 - (1-0.0027)^4 =~ 0.01. for i, c in counts: if c == 0: doAssert values[i] notin histo continue let p = float(c) / float(cdf[^1]) let n = 5000.0 let expected = p * n let stdDev = sqrt(n * p * (1.0 - p)) doAssert abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev block: # non-uniform array sample normalized float CDF let values = [10, 20, 30, 40, 50] # values let counts = [0.4, 0.3, 0.2, 0.1, 0] # probabilities var histo = initCountTable[int]() let cdf = counts.cumsummed # normalized CDF for i in 0 ..< 5000: histo.inc(sample(values, cdf)) doAssert histo.len == 4 # number of non-zero in ``counts`` for i, c in counts: if c == 0: doAssert values[i] notin histo continue let p = float(c) / float(cdf[^1]) let n = 5000.0 let expected = p * n let stdDev = sqrt(n * p * (1.0 - p)) # NOTE: like unnormalized int CDF test, P(wholeTestFails) =~ 0.01. doAssert abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev