Org Wiki

Main Programming Platforms Projects Emacs Russian Quotes Old Lisa wiki


Table of Contents

1 Of Interest

2 Issues

2.1 malloc issue

Disable malloc caching with GLIBCPP_FORCE_NEW (var for latest gcc different!)

3 vim

3.1 Stop vimdiff mode

Within vim:

: windo diffoff

4 Shell/bash

5 Bare Metal

6 ARM Neon

6.1 Code examples

github: arm_neon_example

Tested with Pi4 32-bits

To compile, e.g.:

gcc matrix_add_number.c -mfpu=neon -o matrix_add_number
  • You don't need CMake at all

7 C++

7.1 Snippets

7.1.1 Intersection of sets

#include <bits/stdc++.h>  // set_intersection

// Set intersection
inline std::set<Reg> operator&(std::set<Reg> const &lhs, std::set<Reg> const &rhs) {
  std::set<Reg> ret;

  set_intersection(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
                 std::inserter(ret, ret.begin()));

  return ret;
}

7.1.2 Capture stderr to string

**
 * Captures stderr output from enclosed function and puts it
 * in a string.
 *
 * This could conceivably be expanded for more general use, but
 * I don't need that right now.
 *
 * Source: https://exceptionshub.com/c-redirecting-stdout.html
 */
std::string stderr_to_string(Seq<uint64_t> &instructions) {
  using std::streambuf;
  using std::ostringstream;
  using std::cerr;
  using std::cout;
  using std::endl;

  // Redirect stderr.
  streambuf *oldCoutStreamBuf = cerr.rdbuf();
  ostringstream strCout;
  cerr.rdbuf(strCout.rdbuf());

  // Do something here that writes to std err

  // Restore old cout.
  cerr.rdbuf(oldCoutStreamBuf);

  return strCout.str();
}

7.1.3 Convert string to lines in vector

/**
 * Split passed string into a vector on the newlines.
 *
 * Source: https://stackoverflow.com/a/13172579/1223531
 */
std::vector<std::string> string_to_vector(std::string const &buf) {
  assert(buf.c_str() != nullptr);
  std::vector<std::string> ret;

  std::stringstream ss(buf.c_str());
  std::string to;

  while(std::getline(ss, to, '\n')){
    ret.push_back(to);
  }

  return ret;
}

7.1.4 Get type id of class

Following as member of a given class:

std::string type_id() const override {
  return std::string(typeid(*this).name());
}

7.1.5 Usage of std::function

In class:

  using ReserveJobCB = std::function<bool(ActionState &state)>;
...
  virtual bool reserve(ActionState &state, ReserveJobCB *cb) {
    assert(cb != nullptr); // callback MUST be defined here
...
    if (!(*cb)(state)) {
      ret = false;
    }
...
  }
  • Note that this is passed as pointer; ref also OK of course

In call:

ITransportHandler::ReserveJobCB cb = [] (ActionState &state) -> bool {
  profiler_lock lck("scan.base.get.accept");
  state.handle_incoming_job();
  return true; // TODO do we need a return value at all here??
};

while (state.loop_condition()) {
  if (!reserve(state, &cb)) break;
}
  • No automatic conversion to std::function of lambda; need to init explicitly and then assign

7.1.6 Hex presentation of bitfields

int length = sizeof(YourType);
unsigned char const *data = reinterpret_cast<unsigned char const *>(&your_bitfields);  // barf puke

std::stringstream ss;
ss << std::hex << std::setfill('0');
for(int i= 0; i < length; ++i) {
  ss << std::setw(2) << (int) data[i];
}

7.2 string_view

Source: Talk “Enough string_view to Hang Ourselves” - for the most part

  • It's a reference essentialy, but tries to behave as a value type (borrow type)
  • Perfectly safe as a function parameter; also for-loop control variables ok
  • All other cases, avoid (even if it is possible)
    • Don't store! Lifetime issues
    • string_view does not extend the lifetime of temporaries! e.g. std::string_view v = str();
    • Don't assign string_view to auto; returning auto may work
    • string_view(nullptr) is undefined behaviour! Thus, compiles (shit)
  • Proper way to assign to std::string:
    • Avoid! Better to pass in whole string view
    • Special std::string ctor, c++17 only
    • In practice hoop jumping required (too complicated to want to learn)
// TODO
void func(std::string_view v) {  // ! pass by value
  std::string buf = std::string(v.data(), v.size());  // Size MUST be passed
}

8 gdb

8.1 Show entire string in debug session:

(gdb) call(void) puts(stmtStack().dump().c_str())
(gdb) call(void) puts(instrs.dump(true).c_str())
(gdb) call(void) puts(result.dump().c_str())

Also works:

(gdb) call puts(stmtStack().dump().c_str())

9 CATCH2

Overview:

> runTests -l         # list all tags

9.1 Specifying which tests to run

Combinations possible of following:

> runTests [a][a]     # AND
> runTests [a],[a]    # OR
> runTests ~[a]       # NOT
> runTests [a] [b]    # run [a] EXCEPT [b]

10 Ruby

10.1 Examine

10.3 Of interest

  • shallow attributes - ruby member var's with type checking
  • Steep - Gradual typing for Ruby. Static type checking only

10.4 Linting

10.6 Snippets

10.6.1 Read last line of file

tmp = IO.readlines(error_filename)[-1]

10.7 Seeing is Believing

This is an emacs mode to run ruby in an emacs buffer

Remember to install gem first

Usage in emacs:

M-x seeing_is_believing - toggle minor mode
C-c ? s - run current buffer
C-c ? c - clear current buffer

Additions to emacs:

(package-install-file (expand-file-name "~/installs/emacs/seeing-is-believing.el"))
...
(require 'seeing-is-believing)
(add-hook 'ruby-mode-hook 'seeing-is-believing)

Change package file:

; WRI CHANGE ;; seeing-is-believing-mode.el ends here
;;; seeing-is-believing.el ends here

10.8 Type checking in Ruby

Got all excited by this video, but the code hasn't been made public so vapourware.

Further:

10.9 Contracts and aspect-oriented programming

10.11 Names of constants

Get a constant reference by string name:

dd_def = Object.const_get(dd_name)  # Joyous hack! Convert string to constant reference

Perhaps useful also, check if constant present using string name:

Object.constants.include? dd_name.to_sym

Constant name as symbol:

sym = Object.constants.find { |c| Object.const_get(c).equal? MyConst }

Yes, crazy inefficient, but if the goal is to use it for code generation, it's fine with me.

11 Javascript

12 Git

12.1 Links

12.2 Use vim as editor for commits

> git config --global core.editor "vim"

12.3 Permanently authenticating with Git repositories

Avoid username/password on every push/pull to/from github. Source

> git config credential.helper store

12.4 Minimal repo clone

Used this to get only a part of the linux repo (which is huge)

Sources:

> git clone --depth 1 --no-checkout --filter=blob:none https://github.com/torvalds/linux.git
> cd linux/

Also did following, prob not necessary, does nothing (root prefix likely wrong!):

> git config core.sparseCheckout true
> echo "root/drivers/gpu/drm/vc4" >> .git/info/sparse-checkout
> echo "root/drivers/gpu/drm/v3d" >> .git/info/sparse-checkout

Continuing first (this fetches per file):

> git checkout master -- drivers/gpu/drm/vc4/
> git checkout master -- drivers/gpu/drm/v3d/

12.5 Folder relocation

Or, extract folder from git repository and put it in a separate repo.

Source

Base command:

> git filter-branch --subdirectory-filter <folder> -- -- all

This removes everything that is not `<folder>` and moves it up to base path in repo (exactly what I needed).

The rest to make a new repo is common sense.

12.5.1 Remove file from staging

> git reset <file>

12.5.2 Update branch list, local and remote

> git remote update –prune origin

  • Appears to fetch the whole shebang !??

12.5.3 checkout remote branch

  • Do regular checkout on repo
  • > git fetch origin remote_branch:local_branch_name
  • > git checkout local_branch_name

Also '–depth=1' works with fetch

12.5.4 calculate revision number from logs

This is an idea from online, not used but worth considering

revisioncount=`git log --oneline | wc -l`

13 Doxygen

13.1 Indicate code sections

#+BEGIN_SRC C++

/**

  • @name My Code Section
  • @{

*/

// Methods with header comments here

///@}

#+END_SRC C++

14 MariaDB

14.1 Repair table

When error like this occurs:

#145 - Table './DB_NAME/wp_posts' is marked as crashed and should be repaired
  • Run query:
REPAIR TABLE `<table name>`;

15 Parsing

15.1 Parsing with derivates

16 Scientific Maths

17 Other

17.1 Distributed

Related:

  • SPMD - Intel, 'Single Program, Multiple Data'; abstraction language for SIMD

17.2 IEEE 745 float

Following for 32-bit floats.

  • bit 31: sign; if 1, negative
  • bits 23-30: exponent; 127 offset, e.g. 2^127 -> 1.0
  • bits 0-22 (23 bits): mantissa, top bit implicit
  • +0 and -0 both exist; +0 has bitwise representation 0x0
  • Special values: inf (+/-), NaN (s-, q-)
    • All have exponent set to max (0b11111111)
    • Inf: mantissa zero
    • NaN: mantissa non-zero, sNaN has top bit of mantissa set (not sure, may be reverse)
  • Denormalized: if exponent == 0, implicit leading 1 is dropped

17.3 TLA+

17.4 Python

17.4.1 CTypes

17.4.1.1 Demo usage

Python:

        >>> from ctypes import *
        >>> bellend = CDLL("./libBellend.so")    # in Bellend/Debug
        >>> bellend.convert_image(123)
...123

17.4.2 Use gdb to debug python

> gdb python > (gdb) run /path/to/script.py

wait for segfault

> (gdb) backtrace

stack trace of the c code

Using python debugger module:

> python -m pdb myscript.py 

17.4.3 Boost python

History:

32  sudo zypper in boost-devel    # Cancelled, bloody huge
41  sudo zypper in python-devel   # For patchlevel.h

Zip from: https://github.com/boostorg/python

Example code from: http://www.shocksolution.com/python-basics-tutorials-and-examples/linking-python-and-c-with-boostpython/

17.5 Typescript

When taking the leap, consider following:

17.6 Linters and Checkers

17.6.1 clang-tidy

17.6.1.1 Install

LLVM downloads Source

  • Download and unpack LLVM source list
  • Download Clang source code, unpack in previous at subdir llvm/tools/clang
  • Download clang-tools-extra, unpack in previous at subdir llvm/tools/clang/extra
  • Make build dir build-llvm next to top-level dir of llvm
  • In build dir: cmake -DCMAKE_EXPORT_COMPILE_COMMANDS\=ON ../llvm-7.0.0= (takes a looong time)
17.6.1.2 To use
  • In the build dir of the project to analyze:
> make clean
> bear make all
  • To run clint-tidy (One source file at a time! Can't do dir's):
> <path-to>/clang-tidy <path-to-sourrce-file-in-project> -p <project-build-dir>

To get config data for .clan-tidy: append to previous cmd: --dump-config

17.6.2 OCLint

Looks promising; uses LLVM.

Won't compile (olivia):

.../oclint/oclint-driver/lib/Driver.cpp:(.text+0x17a3): undefined reference to `clang::tooling::getClangStripOutputAdjuster()'
 etc.

Author: Wim Rijnders

Created: 2024-02-06 Tue 15:44

Validate